Many GDI
drawing operations are accomplished using a series of
GDI objects, such as pens, brushes, or fonts. The Microsoft
Foundation Classes Library provides a series of wrapper
classes that encapsulate the functionality of these GDI
objects.
All GDI
object classes are derived from the class CGdiObject.
Figure 24.5 illustrates GDI object classes in
MFC.
GDI object classes.
The
CGdiObject class provides generic support for GDI objects
in the form of a series of member functions. The Attach
and Detach member functions can be used to attach a CGdiObject-derived
MFC object to a GDI object or detach it
from the GDI object. The handle of the object, stored in
the m_hObject member variable, can be retrieved through
the "safe" function GetSafeHandle. (This
function can also be used with null CGdiObject pointers.)
A pointer to a CGdiObject that corresponds to a Windows
GDI object handle can be obtained by calling the static
member function FromHandle. To obtain a GDI object's type,
use the GetObjectType member function.
The
CreateStockObject member function can be used to crate a
stock pen, brush, font, or palette. Note that this
function should be called with a CGdiObject-derived object
that is of the appropriate class (CPen, CBrush, CFont, or
CPalette).
The
UnrealizeObject member function can be used to reset the
origin of a brush or reset a palette. Do not use this
member function for objects of any other type.
The
DeleteObject member function deletes the GDI object that
the CGdiObject-derived MFC object is attached to. The
DeleteTempMap function, called usually from the idle-time
handler of your application's CWinApp object, is used to
delete any temporary CGdiObject objects that were created
by the FromHandle member function.
Support for
GDI pen objects in MFC is provided through the CPen class.
A pen can be created either in a single step or in two
steps. If you wish to create a pen in a single step, you
can utilize overloaded versions the CPen constructor for
this purpose. For example, to create a dashed black pen,
you can declare the pen object as follows:
CPen myPen(PS_DASH, 0, RGB(0, 0, 0));
Alternatively,
pens can be created in a two-step operation, by creating
first the MFC CPen object using a parameterless
constructor, and then calling the CreatePen or
CreatePenIndirect member functions to create a
corresponding GDI pen object. For example:
CPen *pPen;
pPen = new CPen;
pPen->CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
To obtain a
LOGPEN structure from a CPen object, use the GetLogPen
function. You can also use a CPen object in any GDI
function calls that require a pen handle of type HPEN
because the CPen class defines the operator HPEN operator
function.hes
The MFC
supports GDI brushes through the CBrush class. Like pens,
brushes can also be created in either a single step or a
two-step process.
To create a
GDI brush while constructing the CBrush object, use one of
the overloaded versions of the CBrush constructor. For
example, to create a solid yellow brush, you could
construct the CBrush object as follows:
CBrush *pBrush;
pBrush = new CBrush(RGB(255, 255, 0));
Alternatively,
you can first create the CBrush MFC object through the
parameterless constructor and then create the GDI brush
object by calling the CreateSolidBrush, CreateHatchBrush,
CreatePatternBrush, CreateDIBPatternBrush,
CreateSysColorBrush, or CreateBrushIndirect member
functions. For example:
CBrush cyanBrush;
cyanBrush.CreateSolidBrush(RGB(0, 255, 255));
To obtain a
LOGBRUSH structure from a CBrush object, use the
GetLogBrush member function. You can also use CBrush
objects in place of handles of type HBRUSH in GDI function
calls because the CBrush class defines the operator HBRUSH
operator function.
MFC
Example
Note:
UCanCode Advance E-XD++
CAD Drawing and Printing Solution
Source Code Solution for C/C++, .NET V2024 is released!
Contact UCanCode Software
To buy the source code or learn more about with: