Graphics Device Interface (GDI) Classes

The CWinApp class and the CWnd derivatives are by far the most important Windows-oriented classes in the library. Most are intended for derivation. The GDI Windows classes are included as a convenience for the C++ programmer. Each corresponds almost exactly to a Windows data structure and is generally not used for derivation.

The Device Context Classes

CDC, CPaintDC, CWindowDC, CClientDC, and CMetaFileDC are “device context” classes because they are C++ wrappings of the Windows device context. A device context is a Windows data structure associated with a physical device. It is the Windows method of rendering graphics in a hardware-independent manner. In order to draw or print in a window, you must first get access to a display device context object.

The base class, CDC, can be used directly to access the entire display or a nondisplay context. A “nondisplay context” is a hardware device, such as a printer or plotter, that has a Windows driver.

A CWindowDC context is a “display” context that is “clipped” (by Windows) to include only the area of its associated window. A CClientDC context includes only the window's “client” area (exclusive of title bar and scroll bars). A CPaintDC context is like a CClientDC context except that it is enhanced (by the Microsoft Foundation Class Library) to work in an OnPaint member function without the need for the BeginPaint and EndPaint function calls.

The most frequently used device context is CPaintDC. A typical OnPaint member function obtains and uses a device context as shown:

void CMainWindow::OnPaint()

{ // BeginPaint function call not required

CPaintDC dc( this ); // The device context for this window

dc.TextOut( 0, 0, "hello", 5 ); // Top left of the client rectangle

} // EndPaint function call not required

The GDI Object Classes

The Windows GDI employs various drawing tools, including pens, brushes, palettes, fonts, bit maps, and regions. Many device context operations, such as drawing and painting, depend on a specific drawing tool being linked to the device context. In the native Windows environment, this operation is known as “selecting a GDI object into a device context.” In the Microsoft Foundation classes, a tool type is represented by a class derived from CGdiObject.

The base class of the device context classes, CDC, has a SelectObject member function overloaded for each GDI-object-derived class. This function selects a GDI object to a device context (and returns the previously selected object of that type). Thus you can attach a brush to a paint display context (and use it) as follows:

// Create a device context for this window

CPaintDC dc( this );

// Construct a crosshatch filling brush

CBrush brush( HS_DIAGCROSS, 0L );

// Select the brush into the device context

CBrush* pOldBrush = dc.SelectObject( &brush );

// Paint the ellipse with crosshatching

dc.Ellipse( 0, 20, 40, 60 );

// Restore the original brush

dc.SelectObject( pOldBrush );

The last statement disconnects brush from dc at the Windows level. This permits the Windows brush to be deleted by the CBrush destructor (when the brush object goes out of scope). It is very important to delete Windows GDI objects; otherwise their memory will not be reclaimed, even after the Windows application terminates. Windows GDI objects cannot be deleted as long as they are selected in a valid device context.