Understanding the SetStyle Method

Windows Forms and controls provide built-in support for double buffering, and the SetStyle method of the Control class plays a vital role in this process. Before we discuss how to use SetStyle, let's take a look at this method and its members.

The SetStyle method is defined in System.Windows.Forms.Control, which sets the specified style of a control. This method takes two arguments. The first argument is of type ControlStyle enumeration, and it represents the style of the control. The second argument is true if we want to apply the specified style, false otherwise. The members of the ControlStyle enumeration are described in Table 13.1.

Table 13.1. ControlStyle members




The WM_ERASEBKGND window message is sent to the message queue whenever a control needs to redraw its background. This method tells Windows to ignore the message, reducing flicker. Both OnPaint and OnPaintBackground are called from the window message WM_PAINT.AllPaintingInWmPaint should be used only if UserPaint is set to true.


Applications can cache text using this option. The control keeps a copy of the text rather than getting it from the handle each time it is needed. This style defaults to false.


The control is a container.


This method provides built-in support for double buffering. When it is set to true, drawing is performed in a buffer and displayed only when complete. When using this option, you must also set the UserPaint and AllPaintingInWmPaint bits to true.


If true, the OnNotifyMessage method is called for every message sent to the control's WndProc method. This style defaults to false.


The control has a fixed height.


The control has a fixed width.


The control is drawn opaque, and the background is not painted.


The control is redrawn when it is resized.


The control can receive focus.


The control implements standard click behavior.


The control implements standard double-click behavior. When using this option, you must also set StandardClick to true.


The control accepts a Color object with alpha transparency for the background color. The UserPaint bit must be set to true, and the control must be derived from the Control class, like this: this.SetStyle(ControlStyles.UserPaint, true);


The control does its own mouse processing, and mouse events are not handled by the operating system.


The control paints itself rather than having the operating system do it. This option applies to classes derived from Control.

Let's apply the SetStyle method to achieve double buffering. Double buffering can be enabled programmatically with the following code:

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


We can also control the redrawing of controls when a control is resized. Setting ControlStyle.ResizeRedraw to true, as in the code snippet that follows, forces controls to be redrawn every time a control (or a form) is resized.

SetStyle(ControlStyles.ResizeRedraw, true);


Sometimes we will not want a control to be redrawn when it is resized. In this case we can set ResizeRedraw to false.


Many controls, such as PictureBox, are double-buffered automatically, which means we don't need to write any additional code when viewing images in a PictureBox control.

GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics



Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET

GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
Year: 2003
Pages: 145

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net