473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Combating flicker

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.
Oct 8 '07 #1
4 10733
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.

Oct 9 '07 #2
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

"Linda Liu [MSFT]" <v-****@online.microsoft.comwrote in message
news:4S**************@TK2MSFTNGHUB02.phx.gbl...
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.

Oct 9 '07 #3
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

Oct 11 '07 #4
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.

Oct 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael | last post by:
Here is my problem: I have a MDI application and when I load my child forms I get alot of flicker. I have tried to implement double buffering : Public Sub New() MyBase.New() 'This call is...
4
by: Marek Mänd | last post by:
This seems an IE issue only: 4253 bytes testcase: http://www.hot.ee/idaliiga/testcases/ieselect/bnlinkingselectinmsie.htm Can one have 1) a mouseover/mouseout element on TBODY 2) change in...
0
by: mp3boss | last post by:
I am updating a string in the format MM:SS every second using the On_Timer event in Access97 by changing the caption of a label. Even though I'm using 8point text, the box sometimes flickers...
5
by: Ian Stiles | last post by:
I have tried everything under the sun to get rid of horrible flashing and flickering that occurs on a CSharp form when the form hosts a TreeView or WebBrowser control and then you resize the form....
3
by: Per Dunberg | last post by:
Hi all, I have to develop a "skinned" application and I have a problem with the graphics. When a form is loaded and displayed there's aways a flicker where all the controls are located on the...
3
by: seamlyne | last post by:
The first method I ever used for multiple state buttons was to create a graphic for each button for each state: AboutUs_on, AbooutUs_over, AboutUs_out, etc. That works great when there are just a...
17
by: pigeonrandle | last post by:
Hi, I have seen loads of different ways to do this, but the all seem to yield the same result - text that doesn't flicker when it's moving too slowly! Does anyone know 'the best way' to make text...
1
by: Wayne | last post by:
I've noticed some screen flicker when using Access 2003 under Vista and I'm curious as to whether this is a bug or peculiar to my machine. In design view, if I make changes to a form and then...
0
by: Rainer Queck | last post by:
Hello NG, I had/have a bad flicker Problem with my Application. On starting some applications, while my app was running, the whole Display started to flicker. Even the desktop Icons! Looking...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.