Home | Overview | How Do I | Tutorial
Windows provides a variety of drawing tools to use in device contexts. It provides pens to draw lines, brushes to fill interiors, and fonts to draw text. MFC provides graphic-object classes equivalent to the drawing tools in Windows. The table below shows the available classes and the equivalent Windows graphics device interface (GDI) handle types.
The general literature on programming for the Windows GDI applies to the Microsoft Foundation classes that encapsulate GDI graphic objects. This article explains the use of these graphic-object classes:
Classes for Windows GDI Objects
Class | Windows handle type |
CPen | HPEN |
CBrush | HBRUSH |
CFont | HFONT |
CBitmap | HBITMAP |
CPalette | HPALETTE |
CRgn | HRGN |
Each graphic-object class in the class library has a constructor that allows you to create graphic objects of that class, which you must then initialize with the appropriate create function, such as CreatePen.
Each graphic-object class in the class library has a cast operator that will cast an MFC object to the associated Windows handle. The resulting handle is valid until the associated object detaches it. Use the object’s Detach member function to detach the handle.
The following code casts a CPen object to a Windows handle:
CPen myPen;
myPen.CreateSolidPen( PS_COSMETIC, 1, RGB(255,255,0) );
HPEN hMyPen = (HPEN) myPen;
The following four steps are typically used when you need a graphic object for a drawing operation:
Note If you will be using a graphic object repeatedly, you can allocate it once and select it into a device context each time it is needed. Be sure to delete such an object when you no longer need it.