description | title | ms.date | f1_keywords | helpviewer_keywords | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: CPen Class |
CPen Class |
11/04/2016 |
|
|
Encapsulates a Windows graphics device interface (GDI) pen.
class CPen : public CGdiObject
Name | Description |
---|---|
CPen::CPen |
Constructs a CPen object. |
Name | Description |
---|---|
CPen::CreatePen |
Creates a logical cosmetic or geometric pen with the specified style, width, and brush attributes, and attaches it to the CPen object. |
CPen::CreatePenIndirect |
Creates a pen with the style, width, and color given in a LOGPEN structure, and attaches it to the CPen object. |
CPen::FromHandle |
Returns a pointer to a CPen object when given a Windows HPEN. |
CPen::GetExtLogPen |
Gets an EXTLOGPEN underlying structure. |
CPen::GetLogPen |
Gets a LOGPEN underlying structure. |
Name | Description |
---|---|
CPen::operator HPEN |
Returns the Windows handle attached to the CPen object. |
For more information on using CPen
, see Graphic Objects.
CPen
Header: afxwin.h
Constructs a CPen
object.
CPen();
CPen(
int nPenStyle,
int nWidth,
COLORREF crColor);
CPen(
int nPenStyle,
int nWidth,
const LOGBRUSH* pLogBrush,
int nStyleCount = 0,
const DWORD* lpStyle = NULL);
nPenStyle
Specifies the pen style. This parameter in the first version of the constructor can be one of the following values:
-
PS_SOLID
Creates a solid pen. -
PS_DASH
Creates a dashed pen. Valid only when the pen width is 1 or less, in device units. -
PS_DOT
Creates a dotted pen. Valid only when the pen width is 1 or less, in device units. -
PS_DASHDOT
Creates a pen with alternating dashes and dots. Valid only when the pen width is 1 or less, in device units. -
PS_DASHDOTDOT
Creates a pen with alternating dashes and double dots. Valid only when the pen width is 1 or less, in device units. -
PS_NULL
Creates a null pen. -
PS_INSIDEFRAME
Creates a pen that draws a line inside the frame of closed shapes produced by the Windows GDI output functions that specify a bounding rectangle (for example, theEllipse
,Rectangle
,RoundRect
,Pie
, andChord
member functions). When this style is used with Windows GDI output functions that don't specify a bounding rectangle (for example, theLineTo
member function), the drawing area of the pen isn't limited by a frame.
The second version of the CPen
constructor specifies a combination of type, style, end cap, and join attributes. The values from each category should be combined by using the bitwise "or" (|
) operator. The pen type can be one of the following values:
-
PS_GEOMETRIC
Creates a geometric pen. -
PS_COSMETIC
Creates a cosmetic pen.The second version of the
CPen
constructor adds the following pen styles fornPenStyle
: -
PS_ALTERNATE
Creates a pen that sets every other pixel. (This style is applicable only for cosmetic pens.) -
PS_USERSTYLE
Creates a pen that uses a styling array supplied by the user.The end cap can be one of the following values:
-
PS_ENDCAP_ROUND
End caps are round. -
PS_ENDCAP_SQUARE
End caps are square. -
PS_ENDCAP_FLAT
End caps are flat.The join can be one of the following values:
-
PS_JOIN_BEVEL
Joins are beveled. -
PS_JOIN_MITER
Joins are mitered when they are within the current limit set by theSetMiterLimit
function. If the join exceeds this limit, it's beveled. -
PS_JOIN_ROUND
Joins are round.
nWidth
Specifies the width of the pen.
-
For the first version of the constructor, a value of 0 will be treated similarly to a value of 1, except that the width will not be affected by scale-transform operations that are in effect for the Graphics object that the pen is used for; the width will always be 1 pixel.
-
For the second version of the constructor, if
nPenStyle
isPS_GEOMETRIC
, the width is given in logical units. IfnPenStyle
isPS_COSMETIC
, the width must be set to 1.
crColor
Contains an RGB color for the pen.
pLogBrush
Points to a LOGBRUSH
structure. If nPenStyle
is PS_COSMETIC
, the lbColor
member of the LOGBRUSH
structure specifies the color of the pen and the lbStyle
member of the LOGBRUSH
structure must be set to BS_SOLID
. If nPenStyle
is PS_GEOMETRIC
, all members must be used to specify the brush attributes of the pen.
nStyleCount
Specifies the length, in doubleword units, of the lpStyle
array. This value must be zero if nPenStyle
isn't PS_USERSTYLE
.
lpStyle
Points to an array of doubleword values. The first value specifies the length of the first dash in a user-defined style, the second value specifies the length of the first space, and so on. This pointer must be NULL
if nPenStyle
isn't PS_USERSTYLE
.
If you use the constructor with no arguments, you must initialize the resulting CPen
object with the CreatePen
, CreatePenIndirect
, or CreateStockObject
member functions.
If you use the constructor that takes arguments, then no further initialization is necessary. The constructor with arguments can throw an exception if errors are encountered, while the constructor with no arguments will always succeed.
[!code-cppNVC_MFCDocView#99]
Creates a logical cosmetic or geometric pen with the specified style, width, and brush attributes, and attaches it to the CPen
object.
BOOL CreatePen(
int nPenStyle,
int nWidth,
COLORREF crColor);
BOOL CreatePen(
int nPenStyle,
int nWidth,
const LOGBRUSH* pLogBrush,
int nStyleCount = 0,
const DWORD* lpStyle = NULL);
nPenStyle
Specifies the style for the pen. For a list of possible values, see the nPenStyle
parameter in the CPen
constructor.
nWidth
Specifies the width of the pen.
-
For the first version of
CreatePen
, a value of 0 will be treated similarly to a value of 1, except that the width will not be affected by scale-transform operations that are in effect for the Graphics object that the pen is used for; the width will always be 1 pixel. -
For the second version of
CreatePen
, ifnPenStyle
isPS_GEOMETRIC
, the width is given in logical units. IfnPenStyle
isPS_COSMETIC
, the width must be set to 1.
crColor
Contains an RGB color for the pen.
pLogBrush
Points to a LOGBRUSH
structure. If nPenStyle
is PS_COSMETIC
, the lbColor
member of the LOGBRUSH
structure specifies the color of the pen and the lbStyle
member of the LOGBRUSH
structure must be set to BS_SOLID
. If nPenStyle
is PS_GEOMETRIC
, all members must be used to specify the brush attributes of the pen.
nStyleCount
Specifies the length, in doubleword units, of the lpStyle
array. This value must be zero if nPenStyle
isn't PS_USERSTYLE
.
lpStyle
Points to an array of doubleword values. The first value specifies the length of the first dash in a user-defined style, the second value specifies the length of the first space, and so on. This pointer must be NULL
if nPenStyle
isn't PS_USERSTYLE
.
Nonzero if successful, or zero if the method fails.
The first version of CreatePen
initializes a pen with the specified style, width, and color. The pen can be subsequently selected as the current pen for any device context.
Pens that have a width greater than 1 pixel should always have either the PS_NULL
, PS_SOLID
, or PS_INSIDEFRAME
style.
If a pen has the PS_INSIDEFRAME
style and a color that doesn't match a color in the logical color table, the pen is drawn with a dithered color. The PS_SOLID
pen style can't be used to create a pen with a dithered color. The style PS_INSIDEFRAME
is identical to PS_SOLID
if the pen width is less than or equal to 1.
The second version of CreatePen
initializes a logical cosmetic or geometric pen that has the specified style, width, and brush attributes. The width of a cosmetic pen is always 1; the width of a geometric pen is always specified in world units. After an application creates a logical pen, it can select that pen into a device context by calling the CDC::SelectObject
function. After a pen is selected into a device context, it can be used to draw lines and curves.
-
If
nPenStyle
isPS_COSMETIC
andPS_USERSTYLE
, the entries in thelpStyle
array specify lengths of dashes and spaces in style units. A style unit is defined by the device in which the pen is used to draw a line. -
If
nPenStyle
isPS_GEOMETRIC
andPS_USERSTYLE
, the entries in thelpStyle
array specify lengths of dashes and spaces in logical units. -
If
nPenStyle
isPS_ALTERNATE
, the style unit is ignored and every other pixel is set.
When an application no longer requires a given pen, it should call the CGdiObject::DeleteObject
member function or destroy the CPen
object so the resource is no longer in use. An application shouldn't delete a pen when the pen is selected in a device context.
[!code-cppNVC_MFCDocView#100]
Initializes a pen that has the style, width, and color given in the structure pointed to by lpLogPen
.
BOOL CreatePenIndirect(LPLOGPEN lpLogPen);
lpLogPen
Points to the Windows LOGPEN
structure that contains information about the pen.
Nonzero if the function is successful; otherwise 0.
Pens that have a width greater than 1 pixel should always have either the PS_NULL
, PS_SOLID
, or PS_INSIDEFRAME
style.
If a pen has the PS_INSIDEFRAME
style and a color that doesn't match a color in the logical color table, the pen is drawn with a dithered color. The PS_INSIDEFRAME
style is identical to PS_SOLID
if the pen width is less than or equal to 1.
[!code-cppNVC_MFCDocView#101]
Returns a pointer to a CPen
object given a handle to a Windows GDI pen object.
static CPen* PASCAL FromHandle(HPEN hPen);
hPen
HPEN
handle to Windows GDI pen.
A pointer to a CPen
object if successful; otherwise NULL
.
If a CPen
object isn't attached to the handle, a temporary CPen
object is created and attached. This temporary CPen
object is valid only until the next time the application has idle time in its event loop, at which time all temporary graphic objects are deleted. In other words, the temporary object is only valid during the processing of one window message.
[!code-cppNVC_MFCDocView#105]
Gets an EXTLOGPEN
underlying structure.
int GetExtLogPen(EXTLOGPEN* pLogPen);
pLogPen
Points to an EXTLOGPEN
structure that contains information about the pen.
Nonzero if successful; otherwise 0.
The EXTLOGPEN
structure defines the style, width, and brush attributes of a pen. For example, call GetExtLogPen
to match the particular style of a pen.
See the following topics in the Windows SDK for information about pen attributes:
The following code example demonstrates calling GetExtLogPen
to retrieve a pen's attributes, and then create a new, cosmetic pen with the same color.
[!code-cppNVC_MFCDocView#102]
Gets a LOGPEN
underlying structure.
int GetLogPen(LOGPEN* pLogPen);
pLogPen
Points to a LOGPEN
structure to contain information about the pen.
Nonzero if successful; otherwise 0.
The LOGPEN
structure defines the style, color, and pattern of a pen.
For example, call GetLogPen
to match the particular style of pen.
See the following topics in the Windows SDK for information about pen attributes:
The following code example demonstrates calling GetLogPen
to retrieve a pen character, and then create a new, solid pen with the same color.
[!code-cppNVC_MFCDocView#103]
Gets the attached Windows GDI handle of the CPen
object.
operator HPEN() const;
If successful, a handle to the Windows GDI object represented by the CPen
object; otherwise NULL
.
This operator is a casting operator, which supports direct use of an HPEN
object.
For more information about using graphic objects, see the article Graphic Objects in Windows SDK.
[!code-cppNVC_MFCDocView#104]