If you have ever programmed a windows based application (level editor for instance) in .NET you probably encountered one serious problem - GUI flickering or some other artifacts with controls being not properly refreshed or painted when the form is being loaded. If not, probably your GUI was rather simple and you will come across it at some point :)
The common way of handling this is to enable double buffering of custom controls via following snippet:
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
This is how you can enable automatic double buffering in your application.
You can also try using:
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
However, it still doesn't solve the issue in all cases. The biggest problems is with forms having many GUI elements (buttons, checkboxes etc.) and with level editor that is so easy to achieve.
There is, however, a simple trick. Add following code to your form:
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
What it does is something very simple - it enables double buffering at the level of a form (remember that previous snippet was about controls). Although it should be rather considered a trick as it only delays showing the form and doesn't speed up its rendering, it should be sufficient in most cases. Word of warning though - it might produce some drawing issues under Windows XP (eg. when minimizing a form) but how to get around this is described here.
0 comments:
Post a Comment