制作无闪烁动画 C# 自定义控件的最佳方法是什么?

发布于 2024-07-07 15:27:14 字数 393 浏览 5 评论 0原文

我目前正在创建一个需要在 C# 项目中处理动画的自定义控件。 它基本上是一个列表框,包含固定数量的可移动元素。 元素(具有背景图像和几个生成的标签的另一个用户控件)可以向上、向下移动或从列表中取出。

我想创建动画运动,因为元素在容器自定义控件内移动,但在我看来,使用诸如

myCustomControl.left -= m_iSpeed;

计时器事件中触发之类的线来移动控件会闪烁并且渲染效果很糟糕,即使打开了双缓冲也是如此。

所以问题是:实现无闪烁动画 C# 控件的最佳方法是什么?我是否应该不创建自定义控件并处理我生成的面板背景图像中的所有绘图? 难道还有超级动画的方法我没发现吗? :)

谢谢!

I am currently creating a custom control that needs to handle animation in a C# project. It is basically a listbox that contains a fixed number of elements that are subject to move. An element (another user control with a background image and a couple of generated labels) can move upwards, downwards or be taken out of the list.

I would like to create animated movement as the elements get moved around within the container custom control but it seems to me that moving controls around using lines such as

myCustomControl.left -= m_iSpeed;

triggered within a timer event is flickery and has a terrible rendering, even with double buffering turned on.

So here's the question : What is the best way to achieve a flicker-free animated C# control? Should I just not create custom controls and handle all of the drawing within a panel's background image that I generate? Is there a super animation method that I have not discovered? :)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仄言 2024-07-14 15:27:14

对于无闪烁动画,最好的选择是自己绘制(使用 Paint 事件处理程序中的 Graphics 对象)并使用双缓冲。 在您的自定义控件中,您将需要在构造函数中使用如下代码:

this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | 
    ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor,
    true);

your best bet for flicker-free animation is to do the painting yourself (use the Graphics object in the Paint event handler) and use double-buffering. In your custom control you will need code like this in the constructor:

this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | 
    ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor,
    true);
初吻给了烟 2024-07-14 15:27:14

今天上午就这个问题进行了类似的讨论。 Visual C# 表单更新导致闪烁。 所以我会偷懒,给出与我在那里给出的相同答案:

您可以尝试在开始移动之前调用 this.SuspendLayout(); ,并在完成后调用 this.ResumeLayout(false);移动所有控件。 通过这种方式,所有控件都应该立即绘制,并且闪烁应该更少。

顺便说一句,我试图在工作中重现这一点,但似乎失败了。 你能提供一些我可以修复的示例代码吗?

A similar discussion took place this morning on this question. visual c# form update results in flickering. so I will be lazy and give the same answer I gave there:

You could try to call this.SuspendLayout(); before you start your move and this.ResumeLayout(false); when you have finished moving all of the controls. In this way all controls should draw at once and you should have less of a flicker.

On a side note I have tried to reproduce this here at work, but seem to be failing. Can you give some more sample code that I can fix maybe?

黑凤梨 2024-07-14 15:27:14

获得无闪烁动画的正常方法是实现双缓冲。 看看这篇代码项目文章

http://www.codeproject.com/ KB/GDI-plus/flickerFreeDrawing.aspx

在准备好之前尽量减少对绘图的调用也是一个好主意。

The normal way to get flicker-free animation is to implement double-buffering. Take a look at this Code Project article

http://www.codeproject.com/KB/GDI-plus/flickerFreeDrawing.aspx

Minimizing calls to paint until you are ready is also a good idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文