The ControlPaint Class
Technically, the ControlPaint class isn't a part of GDI+. However, it's an extremely useful tool for custom control developers who use GDI+. It is also a well kept .NET secret. Essentially, the ControlPaint class offers methods for drawing standard Windows interface elements, like scroll buttons, borders, focus rectangles, and check boxes.This functionality is tremendously useful. For example, if you want to create a special control that contains a list of items with check boxes, you ordinarily have limited options. You can use control composition (and create contained CheckBox 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 can also turn to the ControlPaint class.The ControlPaint class consists entirely of the static methods described in Table 12-5. Here's a line of code that uses it to draw a check box:
ControlPaint.DrawCheckBox(e.Graphics, new Rectangle(10, 10, 50, 50), ButtonState.Checked);
Table 12-5: Essential ControlPaint Methods
MethodDescription
DrawBorder() and DrawBorder3D()Draws a border on a button-style control.
DrawButton() and DrawCaptionButton()Draws a standard command button control.
DrawCheckBox()Draws a check box control.
DrawComboButton()Draws the drop-down button for a combo box control.
DrawFocusRectangleDraws a dotted rectangular outline for a focus rectangle.
DrawGrid()Draws a grid of one-pixel dots with the specified spacing, within the specified bounds, and in the specified color.
DrawImageDisabled()Draws an image or string of text in a disabled ("greyed out") and DrawStringDisabled() state.
DrawLockedFrame() and DrawSelectionFrame()Draws a standard selection frame in the specified state, with the specified inner and outer dimensions, and with the specified background color.
DrawMenuGlyph()Draws a menu glyph on a menu item control (for example, a check mark).
DrawMixedCheckBoxDraws a three-state check box control.
DrawRadioButton()Draws a standard radio button control.
DrawScrollButtonDraws a scroll button on a scroll bar control.
DrawSizeGrip()Draws the sizing grip that appears on the bottom right of some windows.
And here's one that draws the familiar dotted focus rectangle:
ControlPaint.DrawFocusRectangle(e.Graphics, New Rectangle(130, 80, 20, 20));
Figure 12-19 shows the sample output for several ControlPaint methods, including check boxes of different sizes and states. The next chapter develops a button control that uses ControlPaint to create its basic appearance.
Figure 12-19: 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.