The Control Paint Class

The ControlPaint class offers methods for drawing standard Windows interface elements, like scroll buttons, borders, focus rectangles, and check boxes.

For example, if you want to create a special control that contains a list of items with check boxes, you have limited options. You can use control composition (and create contained check-box controls), but this limits the ways that you can use the check boxes and tailor the interface. Alternatively, you could attempt to draw your own and probably end up with a rather crude-looking square. With the ControlPaint class, however, you can use the DrawCheckBox() method, and end up with the perfectly shaded Windows standard for free. You can even create a check box of any size you like. Similarly, if you want to create a scroll button, or a button that displays a focus rectangle, you also can turn to the ControlPaint class.

The ControlPaint class consists entirely of static methods, as described in Table 7-6. Here's a line of code that uses it draw a check box:

ControlPaint.DrawCheckBox(e.Graphics, new Rectangle(10, 10, 50, 50), ButtonState.Checked);

And here's one that draws the familiar dotted focus rectangle:

ControlPaint.DrawFocusRectangle(e.Graphics, new Rectangle(130, 80, 20, 20));

Table 7-6. Basic ControlPaint Methods



DrawBorder() and DrawBorder3D()

DrawButton() and DrawCaptionButton()





DrawImageDisabled() and DrawStringDisabled()

DrawLockedFrame() and DrawSelectionFrame()


Draws a border like on a button-style control Draws a standard command button control Draws a check-box control

Draws the drop-down button for a combo box control

Draws a dotted rectangular outline for a focus rectangle

Draws a grid of one-pixel dots with the specified spacing, within the specified bounds, and in the specified color

Draw an image or string of text in a disabled ("greyed-out") state

Draw a standard selection frame in the specified state, with the specified inner and outer dimensions, and with the specified background color

Draws a menu glyph on a menu item control (for example, a check mark)


Draws a three-state check-box control

Table 7-6. Basic ControlPaintMethods Method Description

DrawRadioButton() Draws a standard radio button control

DrawScrollButton Draws a scroll button on a scroll bar control

DrawSizeGrip() Draws the sizing grip that appears on the bottom right of some windows

Figure 7-27 shows the sample output for several ControlPaint methods, including check boxes of different sizes and states.

Figure 7-27. Drawing pictures with ControlPaint

Remember, this is a picture of a check box, not a check box! If you want it to change its state when the user clicks it, you need to manually repaint a new check box in a different state.

+2 0

Post a comment