Hi Frank,
Double buffering is a technique used to make drawing faster and appear
smoother by reducing flicker. The basic idea is to take the drawing
operations used to paint your control and apply them to an off-screen
buffer. Once all of the drawing operations have finished, this buffer is
drawn as a single image onto the control. This usually reduces flicker and
makes the application seem faster.
This behavior can be achieved in the .NET Framework 2.0 by setting a
control style to OptimizedDoubleBuffer, which is equivalent to setting the
DoubleBuffer and UserPaint styles in previous versions of the Framework.
To fully enable double buffering, you must also set AllPaintingInWmPaint to
true. When it is set, the window message WM_ERASEBKGRND is ignored, and
both OnPaintBackground and OnPaint methods are called directly from the
WM_PAINT message. If you set DoubleBuffering and AllPaintingInWmPaint to
true, then OnPaintBackground and OnPaint will be called with the same
buffered graphics object and everything will paint off-screen together and
update all at once.
To benefit from double buffering you have to set both these styles to true
or set the DoubleBuffered property on your control, like this:
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
// OR
DoubleBufferred = true; // sets both flags
I am not quite sure why it's needed (particularly when DoubleBuffered
property is set to true). Does it help? Does it hurt?
If you have set the DoubleBuffered property to true, you needn't set the
AllPaintingInWmPaint and OptimizedDoubleBuffer styles.
It is important to mention here that setting the DoubleBuffered property is
not always the optimal solution to painting problems. This property should
be used with care, depending on your application and targeted scenarios.
The main drawback of setting DoubleBuffered is that it results in a
significant amount of memory being allocated for painting.
In addition, The quick and dirty way to avoid most flicker is to use a
transparency key. Pick a color that won't affect your program such as lime
green, or some other ugly color that is not used by anything on your form.
The reason a form will sometimes flicker, often has to do with a bug in
WinForms and how it deals with transparent windows. WinForms is in fact
just a wrapper to the Windows API. When dealing with opacity, WinForms will
use a Windows API to convert your form to what is known as a layered window.
"Using a layered window can significantly improve performance and visual
effects for a window that has a complex shape, animates its shape, or
wishes to use alpha blending effects." ¨C MSDN
However, WinForms does not make a window a layered window until it feels it
is necessary. The flicker you are seeing is WinForms trying to convert a
non layered window into a layered one.
The reason why setting a transparency key works is because WinForms will
force any window with a transparency key to be of the type layered. Set the
transparency key at design time, and at runtime, the window will always be
a layered window. With no need to convert, the flicker goes away.
Hope this helps.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.