CreatePen

2.x

  HPEN CreatePen(fnPenStyle, nWidth, clrref)    
  int fnPenStyle; /* style of pen, */  
  int nWidth; /* width of pen, */  
  COLORREF clrref; /* color of pen, */  

The CreatePen function creates a pen having the specified style, width, and color. The pen can subsequently be selected as the current pen for any device.

Parameters

fnPenStyle

Specifies the pen style. This parameter can be one of the following values:

Value Meaning

PS_SOLID Creates a solid pen.
PS_DASH Creates a dashed pen. (Valid only when the pen width is 1.)
PS_DOT Creates a dotted pen. (Valid only when the pen width is 1.)
PS_DASHDOT Creates a pen with alternating dashes and dots. (Valid only when the pen width is 1.)
PS_DASHDOTDOT Creates a pen with alternating dashes and double dots. (Valid only when the pen width is 1.)
PS_NULL Creates a null pen.
PS_INSIDEFRAME Creates a pen that draws a line inside the frame of closed shapes produced by graphics device interface (GDI) output functions that specify a bounding rectangle (for example, the Ellipse, Rectangle, RoundRect, Pie, and Chord functions). When this style is used with GDI output functions that do not specify a bounding rectangle (for example, the LineTo function), the drawing area of the pen is not limited by a frame.

nWidth

Specifies the width, in logical units, of the pen. If this value is zero, the width in device units is always one pixel, regardless of the mapping mode.

clrref

Specifies the color of the pen.

Return Value

The return value is the handle of the pen if the function is successful. Otherwise, it is NULL.

Comments

Pens whose width is greater than one pixel always have the PS_NULL, PS_SOLID, or PS_INSIDEFRAME style.

If a pen has the PS_INSIDEFRAME style and a color that does not match a color in the logical color table, the pen is drawn with a dithered color. The PS_SOLID pen style cannot be used to create a pen with a dithered color. The PS_INSIDEFRAME style is identical to PS_SOLID if the pen width is less than or equal to 1.

When it has finished using a pen created by CreatePen, an application should remove the pen by using the DeleteObject function.

The following illustration shows how the various system pens appear when used to draw a rectangle.

Example

The following example uses the CreatePen function to create a solid blue pen 6 units wide, selects the pen into a device context, and then uses the pen to draw a rectangle:

HPEN hpen, hpenOld;

hpen = CreatePen(PS_SOLID, 6, RGB(0, 0, 255));
hpenOld = SelectObject(hdc, hpen);

Rectangle(hdc, 10, 10, 100, 100);

SelectObject(hdc, hpenOld);
DeleteObject(hpen);

See Also

CreatePenIndirect, DeleteObject, Ellipse, Rectangle, RoundRect