views:

739

answers:

4

I've seen the following code to enable double buffering on a winform:

// Activates double buffering 
this.SetStyle(ControlStyles.DoubleBuffer |
   ControlStyles.OptimizedDoubleBuffer |
   ControlStyles.UserPaint |
   ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();

Is this different in any way from simply setting Form.DoubleBuffering = true?

+2  A: 

Setting a form's DoubleBuffering will set double buffering for that form. It's the same as calling

form.SetStyle(ControlStyles.OptimizedDoubleBuffer, value);

The other flags like UserPaint and AllPaintingInWmPaint are styles that aren't set by simply setting control.DoubleBuffering = true

Judah Himango
+3  A: 

Control.DoubleBuffering performs

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

so your code sets ControlStyles.UserPaint as well (which probably has no effect at this point).

Sunlight
+1  A: 

In .NET 1.x, there was no DoubleBuffered property on controls, so SetStyle was the only way to enable it. Code your see that uses SetStyle is probably either still around from 1.x days, or from developers who just haven't changed their habits since then.

P Daddy
+1  A: 

From Stackoverflow: How to double buffer .NET controls on a form?:

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); 
}
Ian Boyd