简单的动画中的WinForms画中、简单、WinForms

2023-09-02 01:40:13 作者:只配当配角

假设你想在一个WinForm动画的一些对象。您设置一个计时器,更新状态或模型,并覆盖窗体的Paint事件。但是,从那里,什么是不断重新绘制表格动画的最佳方式是什么?

Imagine you want to animate some object on a WinForm. You setup a timer to update the state or model, and override the paint event of the Form. But from there, what's the best way to continually repaint the Form for the animation?

只要你完成绘制?无效的形式 在安装第二个定时器和形式固定时间间隔无效? 或许有一个共同的模式这个东西? 是否有任何有用的.NET类来帮帮忙?

每当我需要做的这一点,我发现了一个新的方法,新的缺陷。什么是从SO社区的经验和建议?

Each time I need to do this I discover a new method with a new drawback. What are the experiences and recommendations from the SO community?

推荐答案

在某些情况下,它的速度更快,更方便地不画使用油漆事件,但得到的图形从控制/表格和绘画的对象,该对象。这可能会给一些麻烦与不透明/抗锯齿/文本等,但可能是值得不必重绘整个shabang方面的麻烦。线沿线的东西:

In some situations, it's faster and more convenient to not draw using the paint event, but getting the Graphics object from the control/form and painting "on" that. This may give some troubles with opacity/anti aliasing/text etc, but could be worth the trouble in terms of not having to repaint the whole shabang. Something along the lines of:

private void AnimationTimer_Tick(object sender, EventArgs args)
{
    // First paint background, like Clear(Control.Background), or by
    // painting an image you have previously buffered that was the background.
    animationControl.CreateGraphics().DrawImage(0, 0, animationImages[animationTick++])); 
}

我用这个在一些控制自己,并有缓冲的图像,以清的背景,当被删除感兴趣的移动对象或需要。

I use this in some Controls myself, and have buffered images to "clear" the background with, when the object of interest moves or need to be removed.