问题
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?
回答1:
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).
回答2:
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
回答3:
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.
回答4:
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);
}
来源:https://stackoverflow.com/questions/302614/enabling-double-buffering