如何消除闪烁? [英] How would I smooth out flickering?

查看:116
本文介绍了如何消除闪烁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计时器间隔设置为50毫秒;蛇快速,平稳地移动,但往往会忽悠.我不确定什么对您最有帮助,所以这是我的绘图方法和一个MoveSnake()method:

My timer interval is set to 50 milliseconds; the snake moves quickly and smoothly, but it tends to flicker. I am not sure what would help you most, so here are my drawing methods and one MoveSnake() meethod:

<pre lang="cs">public void DrawSnake(Graphics g)
{
    foreach (Rectangle rec in mySnake)
    {
        g.FillRectangle(myBrush, rec);
    }
}

public void DrawSnake()
{
    for (int i = mySnake.Length - 1; i > 0; i--)
    {
        mySnake[i] = mySnake[i - 1];
    }
}

public void MoveDown()
{
    DrawSnake();
    mySnake[0].Y += 16;
}


推荐答案

这将取决于许多因素.如果您的屏幕很小,那么您可以只在Form属性上设置DoubleBuffered即可使用它-如果您不满意,则移动速度会变慢.

如果发生这种情况,则会变得更加复杂:
重写OnPaintBackground,如果这是定时绘制,则不要执行此操作. (如果是正常移动,调整大小或其他操作的结果,则必须擦除背景.
That''s going to depend on a number of things. If your screen is small, then you may be able just to set DoubleBuffered on your Form properties, and get away with it - if you don''t get away with it, then the movement will all slow down.

If that happens, then it gets a little more complex:
Override OnPaintBackground, and don''t do it, if this is a timed paint. (if it is the result of a normal move, re-size or whatever, you must erase the background.
protected override void OnPaintBackground(PaintEventArgs e)
    {
    if (!timedPainting)
        {
        // Only do the background if not drawing our screen
        base.OnPaintBackground(e);
        }
    timedPainting = false;
    }

然后,您必须自己擦除尾巴.

听起来很简单?是的.要做很多工作.

Then you must erase the tail block yourself.

Sounds simple? It is, sort of. It''s just a lot of work to get right.


我不知道您使用什么控件来渲染动画图形.如果这是Form,则可以使用属性System.Windows.Forms.Form.DoubleBuffered并按照Griff的建议进行操作.通常,这是对面板的一些自定义控件.

要制作动画,您需要访问System.Windows.Forms.Control.SetStyle.您需要包括以下样式:System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint.由于此方法受到保护,因此只能通过对控件进行子类化来实现,这不是问题.无论如何您都必须创建自己的控件,最好是从System.Windows.Forms.Control派生它并提供所需的背景.

参见:
http://msdn.microsoft.com/en-us/library/system. windows.forms.controlstyles.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.form.aspx [ ^ ].

—SA
I don''t know what control do you use to render your animated graphics. If this is Form, you can use the property System.Windows.Forms.Form.DoubleBuffered and do it as Griff suggested. Very often this is some custom control of just panel.

To do animation, you need to access System.Windows.Forms.Control.SetStyle. You need to include the following styles: System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint. As this method is protected, you can only do by sub-classing a control, which is not a problem. As you have to create your own control anyway, it''s the best to derive it from System.Windows.Forms.Control and provide the background you need.

See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx[^].

—SA


尝试一下:
Try this:
try {
    this.BeginUpdate();
    //Do your drawings here
}
finally {
    this.EndUpdate();
}


这篇关于如何消除闪烁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆