Chapter 7. Redrawing and GDI+

Windows Forms applications are nothing if not visual—the display of information is central to most programs with a user interface. And although the framework ships with a useful range of built-in controls, some applications have presentation requirements that cannot be met by the standard controls or even by third-party components. Fortunately, controls can customize their appearance using GDI+, .NET’s powerful and feature-rich drawing library.

All custom controls must manage their own appearance, because the Control class does no drawing at all. So we will start off by looking at the model Windows Forms uses for letting controls draw themselves. Then we will look at the GDI+ library itself, examining the classes it defines and the drawing facilities it supplies.

Drawing and Controls

Every control owns an area of the screen, and it is responsible for drawing its visual representation onto that part of the display. So far we have not had to deal with this—we have relied on the fact that the built-in controls all draw themselves. But to customize the appearance of our applications, we must first understand the mechanism by which user interface elements are displayed.

The approach used by Windows Forms (and indeed by Windows itself) is that every time any part of a control becomes visible, it is asked to redraw itself. This usually happens for one of two reasons: either the control is being displayed for the first time (e.g., it is on a window that has ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.