HTML clipboard
This article has been
excerpted from book "Graphics
Programming with GDI+".
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.Controller, 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 clicker. 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 default to false. |
ContainerControl DoubleBuffer | 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. |
EnableNotifyMessage | If true, the OnNotifyMessage method is called for every message sent to the control's WindProc 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(ControlStyle.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'sapply the SetStyle method to achieve double buffering. Double buffering can
be enabled programmatically with the following code:
//Activate 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
can 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.
The Quality and Performance of Drawing
Drawing performance is inversely proportional to drawing quality. GDI+ provides
several ways to set the quality of images and text. The SmoothingMode and
TextRenderingHint properties are used to set image and text quality,
respectively. The HighQuality and AntiAlias options provide slow drawing
performance and better quality; the HighSpeed and None options provide poor
quality and fast performance. Before using these options, we must decide if we
really want to draw anti-aliased objects.
Sometimes anti-aliasing won't affect the quality of a drawing, and it is bad
programming practice to use this processor-intensive feature when it is not
required. In other cases we might need to set anti-aliasing for just one object
out of 50. In these cases it is better to set the anti-alias option for that
object only, instead of the entire canvas.
Section 13.4.1 through 13.4.6 describe some more tips and tricks that may help
improve an application's performance.
Repaint Only the Required Area
Avoiding unwanted repainting is a good technique to increase painting
performance. GDI+ provides many techniques for painting only required objects.
Using regions and clipping rectangle may help in some cases. If you need to draw
a single object with anti-aliasing on, just set anti-aliasing for that object
instead of for the entire surface (form). Using regions is one of the best
techniques for repainting only a required area. For better performance, you
should know what area you need to redraw and invalidate only that area, thereby
using regions instead of repainting the entire form.