[RESOLVED] User control Flickering
I have a user control that I created using GDI+, and .Net 2005. But there is a lot of flickering. I turned on DoubleBuffering and the flicker got ten times worse. The control that I created is a level meter( the meter on some audio devices that show the audio levels. Usually a bar that is green to yellow to red). So the needs to refresh about every 100 milliseconds.
The way I designed it makes it look a bit like led lights. When the value lights up the led lights. And they are darened when not higher than the current value. I've attached what the control looks like. Anyone have an idea how I can get rid of the flickering, or reduse it? Thanks
Re: User control Flickering
I'm no guru at this stuff but I'd think that you'd want to redraw as little as possible at each stage. If you are just refreshing the control and handling the drawing on the Paint event then I'd guess that that is less efficient than it needs to be. Obviously you must redraw the whole thing on the Paint event, but I'd suggest that each time you check the level you only actually redraw the parts that need it. You would test the current level and the previous level, then only redraw the boxes that need to change colour, e.g. if last recorded level was 10 and new level is 12 just redraw boxes 11 and 12 in bright colour, or if last level was 7 and current level is 3 just redraw boxes 4, 5, 6 and 7 in dark colour.
Re: User control Flickering
You are not required to Paint the whole control in the Paint event, which you shouldn't use when you are creating a user control as it will occur after the underlying base control is painted, use the OnPaint override instead so you are in full control (possibly even the OnBackGroundPaint as well). You only have to redraw the invalidated region which is given to you by the PaintEventArgs class (e) (in some cases that will be the whole control)
e.ClipRectangle gives you the rectangle that has been invalidated.
You should invalidate only the rectangle that needs redrawing, then in the overriden OnPaint you should calculate which elements fit in that rectangle and only redraw them.
The actual drawing takes up the most time, so it's OK to have bulk load of mathematics in order to prevent the unneeded repainting.
Re: User control Flickering
Make sure you use the graphics object supplied by the paint event and do not use CreateGraphics
Supercharged
Re: User control Flickering
That's right, you shouldn't use CreateGraphics, but you also shouldn't use the Paint event as it will occur after the base control has already been painted. You must override the OnPaint sub for efficiency's sake.
Re: User control Flickering
In addition to using the OnPaint override you can benefit tremendously by creating a backbuffer.
Here is what I use in my controls to creat th buffer.
VB Code:
#Region " Constructors "
Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.UpdateStyles()
End Sub
#End Region
Re: User control Flickering
This is c#, but I was doing something similar, but with alot more calls to FillRect than it appears you have..
http://www.vbforums.com/showthread.php?t=370733
What I figured out, eventually, was that I nearly eliminated the flicker by doing all my individual rectangle draws to a bitmap object, and only calling DrawImage once with the completed bitmap..
Bill
Re: User control Flickering
Thanks for all the input. Right now I have somethign that takes precedence over this now, so I'm nto going to be able to try these fixes for a couple days. But from what I've read I think this is resolved.
Re: User control Flickering
Quote:
Originally Posted by
Supercharged
Make sure you use the graphics object supplied by the paint event and do not use CreateGraphics
Supercharged
You are a genius. This solved my problem completely! :D