Visual C++ and MFC Fundamentals Chapter 8 GDI Orientation and Transformations
To create a pen, you must specify the desired characteristics. This can be done with
another CPen constructor declared as follows:
CPen(int nPenStyle, int nWidth, COLORREF crColor);
Alternatively, if you want to use a variable declared using the default constructor, you
can then call the CPen::CreatePen() method. Its syntax is:
BOOL CreatePen(int nPenStyle, int nWidth, COLORREF crColor);
The arguments of the second constructor and the CreatePen() method are used to specify
the properties that the pen should have:
The Style: This characteristic is passed as the nPenStyle argument. The possible values of
this argument are:
Value Illustration Description
PS_SOLID A continuous solid line^
PS_DASH (^)
A continuous line with dashed
interruptions
PS_DOT A line with a dot interruption at every
other pixel
PS_DASHDOT A combination of alternating dashed and
dotted points
PS_DASHDOTDOT A combination of dash and double dotted
interruptions
PS_NULL No visible line^
PS_INSIDEFRAME (^)
A line drawn just inside of the border of a
closed shape
To specify the type of pen you are creating, as cosmetic or geometric, use the bitwise OR
operator to combine one of the above styles with one of the following:
?? PS_COSMETIC: used to create a cosmetic pen
?? PS_GEOMTERIC: used to create a geometric pen
If you are creating a cosmetic pen, you can also add (bitwise OR) the PS_ALTERNATE
style to to set the pen at every other pixel.
The Width: The nWidth argument is the width used to draw the lines or borders of a
closed shape. A cosmetic pen can have a width of only 1 pixel. If you specify a higher
width, it would be ignored. A geometric pen can have a width of 1 or more pixels but the
line can only be solid or null. This means that, if you specify the style as PS_DASH,
PS_DOT, PS_DASHDOT, or PS_DASHDOTDOT but set a width higher than 1, the
line would be drawn as PS_SOLID.
The Color: The default color of pen on the device context is black. If you want to control
the color, specify the desired value for the crColor argument.
Based this, using the second constructor, you can declare and initialize a CPen variable as
follows:
CPen NewPen(PS_DASHDOTDOT, 1, RGB(255, 25, 5));