Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to double buffer .NET controls on a form?

How can I set the protected DoubleBuffered property of the controls on a form that are suffering from flicker?

like image 748
Ian Boyd Avatar asked Sep 16 '08 20:09

Ian Boyd


People also ask

What is double buffer mode?

A programming technique that uses two buffers to speed up a computer that can overlap I/O with processing. Data in one buffer are being processed while the next set of data is read into the other one.

How do I add different controls to my form?

Add the control by drawingSelect the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.

What is the advantage of a double buffered graphics application?

Well, the main advantages of double-buffering are: The user does not see every pixel modification (so if the same pixel gets modified 5 times, the user will only see the last one). This helps preventing 'flickering' (or screen flashes).


2 Answers

Here's a more generic version of Dummy's solution.

We can use reflection to get at the protected DoubleBuffered property, and then it can be set to true.

Note: You should pay your developer taxes and not use double-buffering if the user is running in a terminal services session (e.g. Remote Desktop) This helper method will not turn on double buffering if the person is running in remote desktop.

public static void SetDoubleBuffered(System.Windows.Forms.Control c) {    //Taxes: Remote Desktop Connection and painting    //http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx    if (System.Windows.Forms.SystemInformation.TerminalServerSession)       return;     System.Reflection.PropertyInfo aProp =           typeof(System.Windows.Forms.Control).GetProperty(                "DoubleBuffered",                 System.Reflection.BindingFlags.NonPublic |                 System.Reflection.BindingFlags.Instance);     aProp.SetValue(c, true, null);  } 
like image 199
Ian Boyd Avatar answered Sep 19 '22 08:09

Ian Boyd


Check this thread

Repeating the core of that answer, you can turn on the WS_EX_COMPOSITED style flag on the window to get both the form and all of its controls double-buffered. The style flag is available since XP. It doesn't make painting faster but the entire window is drawn in an off-screen buffer and blitted to the screen in one whack. Making it look instant to the user's eyes without visible painting artifacts. It is not entirely trouble-free, some visual styles renderers can glitch on it, particularly TabControl when its has too many tabs. YMMV.

Paste this code into your form class:

protected override CreateParams CreateParams {     get {         var cp = base.CreateParams;         cp.ExStyle |= 0x02000000;    // Turn on WS_EX_COMPOSITED         return cp;     }  } 

The big difference between this technique and Winform's double-buffering support is that Winform's version only works on one control at at time. You will still see each individual control paint itself. Which can look like a flicker effect as well, particularly if the unpainted control rectangle contrasts badly with the window's background.

like image 33
Hans Passant Avatar answered Sep 21 '22 08:09

Hans Passant