Windows provides a variety of drawing tools to draw within device contexts. It provides pens to draw lines, brushes to fill interiors, and fonts to draw text. The Microsoft Foundation classes provide graphic object classes that are equivalent to the drawing tools in Windows. The following list shows the available Foundation graphic object classes and the equivalent Windows GDI handle types:
Foundation Classes | Windows Tools |
CPen | HPEN |
CBrush | HBRUSH |
CFont | HFONT |
CBitmap | HBITMAP |
CPalette | HPALLETE |
CRgn | HRGN |
Each of the Microsoft Foundation graphic object classes has a constructor that allows you to create graphic objects of that class.
The following four steps are typically used when a graphic object is needed:
1.Define a graphic object on the frame, athough you can also use the new operator. Perform initialization of the object as necessary.
2.Select the object into the current device context, saving the old graphic object that was selected before.
3.When done with the current graphic object, select the old graphic object back into the device context to restore state.
4.Allow the frame allocated graphic object to be automatically deleted when exiting scope ( or use the delete operator it if allocated with new.)
Note:
If you have graphic objects in your program that will be used repeatedly, it is often a good idea to allocate the objects just one time, in the CWinApp::InitInstance function for example, and select them into a device context whenever you need to use them. Delete these objects when you no longer need them, which is typically just before the program terminates.