Combating flicker

F

Frank Rizzo

Hello, I inherited a large Winforms project that is suffering from
excessive flicker when switching between portions of the application.

I've noticed that most parts of the application (user controls and
forms) have DoubleBuffered set to true. In addition to that, in the
constructor of every form and user control, there is this line:

SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);

I am not quite sure why it's needed (particularly when DoubleBuffered
property is set to true). Does it help? Does it hurt?

Thanks.
 
L

Linda Liu [MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Michael Rubinstein

Linda, can double-buffering or some other technique improve redrawing of
tab pages? Redrawing of a tab page holding nothing fancy - just a bunch of
text boxes, check boxes, radio buttons, labels, is quite visible. I have
virtually identical applications for .NET in C# and older Win32 versions.
The Win32 application refreshes tab pages holding same controls faster.

Michael
 
L

Linda Liu[MSFT]

Hi Frank,

How about the problem now?

If the problem is still not solved, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
N

not_a_commie

As far as I'm concerned, unless you are drawing the control and its
background yourself, double buffering will avail you nothing. The
majority of the Winforms controls have flicker issues and setting the
DoubleBuffer property on them won't change a thing unless you custom
draw the control. Many controls are listed with a flicker bug in MS's
feedback. Those bugs are all closed with a "won't fix" simply because
of MS's move to WPF. WPF is vastly improved in the flicker arena,
although it relies OS-level flicker control, which means you can still
get some pretty bad flicker on WinXP, especially when animating 3D
data. I consider the Winforms flicker issues a fundamental failing
of .NET 2.0 that should have been resolved. I'm hoping that they can
continue to improve it in WPF on WinXP. I say this because the SWT
team managed to do such a good and thorough job of removing flicker on
their controls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top