There are two techniques for creating the graphic objects of the Microsoft Foundation Class Library. The first technique uses a single-stage constructor that also initializes the object and performs any necessary memory allocation for the object. The other technique uses a two-stage contruction strategy in which the object is first constructed and then initialized with a separate function.
Summary: Two-stage construction is generally safer than single-stage construction.
The advantage of single-stage construction is that it involves only a single programming statement, but the disadvantage is that the constructor may throw an exception if incorrect arguments are provided or memory allocations fail. On the other hand, two-stage construction will not throw an exception, but it is slightly more bothersome in that you must write two program statements to create the object. In either case, the method for destroying the object is the same.
The following two sections show examples of single-stage and two-stage construction of graphic objects, as well as examples of destruction.
Single-stage construction of a graphic object
·To create a pen object
Specify the pen style, the pen width, and the pen color to the CPen constructor.
Possible pen styles are the same as those used for traditional Windows programs and listed in the file WINDOWS.H. The pen color is specified as an RGB color whose three components represent the relative saturation of red, green, and blue in the color.
The following code example shows how to create a black CPen object with a single-stage constructor.
CPen myPen( PS_DOT, 5, RGB( 0, 0, 0 ) );
If the object is created on the frame, as in the above example, its destructor will automatically be invoked when the object goes out of scope. If you allocate the object on the heap with the new operator, use the delete operator to deallocate it.
Other graphic object classes have similar constructors with different arguments to specify the attributes necessary to construct them. Before you can use the graphic object to draw, it must be selected into a device context. For more information on how to do this, see “Selecting a Drawing Object in a Device Context” later in this chapter.
Two-stage construction of a graphic object
·To construct a CPen object:
Call the constructor with no arguments.
This constructs the object and initializes it just enough so that it can be deleted if necessary. You cannot use a pen constructed in this way until it has been initialized.
·To initialize this CPen object:
Use the CreatePen and CreatePenIndirect functions.
The following example shows how to construct the empty CPen object and then initialize it with CreatePen. The Boolean return value from CreatePen allows you to detect if the pen object was not successfully initialized.
CPen myPen;
if( pPen.CreatePen( PS_DOT, 5, RGB( 0, 0, 0 ));
//... use the pen
Other graphic object classes have similar two-stage constructors and initialization functions, with different arguments to specify the attributes necessary to construct and initialize each type of object. The next section describes how to select a graphic object into a device context, which is necessary before you can use the object to do any drawing.