O'Reilly logo

.NET Windows Forms in a Nutshell by Matthew Adams, Ian Griffiths

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required