Click here to Skip to main content
13,006,530 members (63,076 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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!

Posted 13-Dec-11 3:20am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
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);
Region myClearBit = new Region();
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.
SAKryukov 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.
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!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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[^].

You will need to add the styles System.Windows.Forms.ControlStyles.AllPaintingInWmPaint.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles., see[^].

However, if your control is Form, you can simply its property DoubleBuffered, see[^].

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170628.1 | Last Updated 13 Dec 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100