Click here to Skip to main content
15,886,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

Don't do the clear!
Instead of drawing on your form, create a simple Panel class:
C#
public class MyPanel : Panel
    {
    protected override void OnPaintBackground(PaintEventArgs e)
        {
        // Prevent background clearing
        }
    }
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:
C#
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Dec-11 11:24am    
Griff, this is important information, but as the solution of the problem in question is not 100% accurate. This technique is neither always required nor it is sufficient and all cases.

Please see my solution.
--SA
Member 8404471 14-Dec-11 4:05am    
Thank you so much, you saved me a lot of time! I'm using your solution painting on a form instead of a panel. but it works like charm!
OriginalGriff 14-Dec-11 4:34am    
You're welcome!
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
 
Share this answer
 
Comments
Member 8404471 14-Dec-11 4:04am    
Thanks a lot, but I tried it now with and without using the DoubleBuffer Option, but it doesn't make a difference in my case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900