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

Member

Description

AllPaintingInWmPaint

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.

CacheText

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.

ContainerControl

The control is a container.

DoubleBuffer

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.

EnableNotifyMessage

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

FixedHeight

The control has a fixed height.

FixedWidth

The control has a fixed width.

Opaque

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

ResizeRedraw

The control is redrawn when it is resized.

Selectable

The control can receive focus.

StandardClick

The control implements standard click behavior.

StandardDoubleClick

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

SupportsTransparentBackColor

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);

UserMouse

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

UserPaint

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.

Note

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.

Категории