清除和重新绘制图形对象时闪烁 [英] flickering when clearing and repainting graphics object

查看:58
本文介绍了清除和重新绘制图形对象时闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图根据通过RS232读取的传感器信号在屏幕上绘制一个填充的椭圆.当前,我使用ReceivedData Handler调用绘制方法,在该方法中,我首先清除表单,然后根据当前信号值绘制椭圆.这会导致大量闪烁.
通过阅读有关闪烁效果的文章,我不确定是否应该采用这种方式.
关于应该如何做的任何建议?

在此先多谢!

Michael

Hi,
i''m trying to draw a filled ellipse on the screen according to a sensor signal which is read in via RS232. Currently I use a ReceivedData Handler to call my draw-method in which i first clear the form and then draw the ellipse according to the current signal value. This results in exessive flickering.
From reading through posts concerning flicker effects, I''m not sure if this way is how I should do it.
Any suggestions of how this should be done?

Thanks a lot in advance!

Michael

推荐答案

不要清除!
不用在表单上绘图,而是创建一个简单的Panel类:
Don''t do the clear!
Instead of drawing on your form, create a simple Panel class:
public class MyPanel : Panel
    {
    protected override void OnPaintBackground(PaintEventArgs e)
        {
        // Prevent background clearing
        }
    }

的唯一目的是禁用后台清除.
将MyPanel添加到您的表单.
现在,在Paint事件中,在面板上绘制椭圆.
我要做的是创建一个椭圆形的区域:

The only purpose of this is to disable background clearing.
Add the MyPanel to your form.
Now draw your ellipse on the panel, in the Paint event.
What I would do is to create a region which is the elipse:

GraphicsPath path = new GraphicsPath();
Rectangle rect = new Rectangle(0, 0, 100, 100);
path.AddEllipse(rect);
Region myClearBit = new Region();
myClearBit.MakeEmpty();
myClearBit.Union(path);
e.Graphics.ExcludeClip(myClearBit);
e.Graphics.FillRectangle(Brushes.Green,e.Graphics.ClipBounds);
e.Graphics.SetClip(myClearBit, CombineMode.Replace);
e.Graphics.FillPath(Brushes.Red, path);

这将在绿色背景上绘制一个红色圆圈,并且在调整大小时不应闪烁.

This will draw a red circle on a green background, and shouldn''t flicker as it re-sizes.


Griff的建议可能不足以避免闪烁所有情况;而且并不总是需要这种技术.

在许多甚至大多数情况下,您都需要设置双缓冲.考虑您有一个控件类.您确实需要根据需要保护的方法System.Windows.Forms.Control.SetStyle创建一个类,请参见 http://msdn. microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx [ http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx [
The advice by Griff may be not enough to avoid flicker in all situations; and this technique is not always needed.

In many or even in most cases you need to setup double buffering. Consider you have a control class. You really need to create a class as you need the method System.Windows.Forms.Control.SetStyle, which is protected, see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx[^].

You will need to add the styles System.Windows.Forms.ControlStyles.AllPaintingInWmPaint.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles., see http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^].

However, if your control is Form, you can simply its property DoubleBuffered, see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx[^].

—SA


这篇关于清除和重新绘制图形对象时闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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