Name

ControlPaint

Synopsis

This class encapsulates a variety of static methods to help you paint your own custom controls and maintain consistency with the appearance of the rest of the UI. Unfortunately, none of these methods support Windows XP themes.

Most common UI elements are supported, including caption buttons, resizing grips, buttons, edit fields, comboboxes, various kinds of flat and 3D borders and frames, focus rectangles, and grids.

In addition, there is a class of methods that will automatically generate the Light(), LightLight(), Dark(), and DarkDark() versions of a specific System.Drawing.Color. If your control is, say, Red instead of Control, you don’t want to use System.Drawing.SystemColors.ControlDark to paint a dark edge. Instead, use Dark() to retrieve a dark version of Red instead.

Probably the most frequently asked question in Windows.Forms development is “How do I draw a rubberband box/line?” ControlPaint offers DrawReversibleLine(), DrawReversibleFrame(), and FillReversibleRectangle(). Again, it is worth noting that Windows XP doesn’t use XOR-ed rectangles any more, because of their limitations when painting over a mid-level color, so you should consider an alternative drawing strategy. One possible approach is to paint a semitransparent rectangle to represent your selection instead, calling Control.Invalidate() to restore the previous background between updates. This works best (and flicker free!) if your selection rectangle is painted as part of your Control.OnPaint() ...

Get .NET Windows Forms in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.