CreatePen Function

Declare Function CreatePen Lib "gdi32.dll" (ByVal fnPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long

Platforms

Description & Usage

CreatePen creates a pen object. The shape of the pen created by the function is always a square having a side length equal to nWidth. After your program is finished using the pen, it must be deleted via the DeleteObject function.

Return Value

If an error occured, the function returns 0 (Windows NT, 2000: use GetLastError to get the error code). If successful, the function returns a handle to the newly created pen.

Visual Basic-Specific Issues

None.

Parameters

fnPenStyle
One of the following flags specifying the style of the pen to create:
PS_SOLID
The pen is solid.
PS_DASH
The pen is dashed. nWidth must be less than or equal to one.
PS_DOT
The pen is dotted. nWidth must be less than or equal to one.
PS_DASHDOT
The pen has alternating dashes and dots. nWidth must be less than or equal to one.
PS_DASHDOTDOT
The pen has alternating dashes followed by two dots. nWidth must be less than or equal to one.
PS_NULL
The pen is invisible.
PS_INSIDEFRAME
The pen is solid. Whenever a drawing function draws a figure inside a bounding rectangle, the dimensions of the figure are shrunk so that the entire figure, including the width of the pen, fits entirely within the bounding rectangle.
nWidth
The width of the pen. If this is 0, the pen is always exactly one pixel wide no matter what.
crColor
The RGB value of the color to give the pen.

Constant Definitions

Const PS_SOLID = 0
Const PS_DASH = 1
Const PS_DOT = 2
Const PS_DASHDOT = 3
Const PS_DASHDOTDOT = 4
Const PS_NULL = 5
Const PS_INSIDEFRAME = 6

Example

' This code is licensed according to the terms and conditions listed here.

' Draw an ellipse on window Form1 using a one-pixel-wide
' square dashed green pen.
Dim hPen As Long  ' handle to the pen created
Dim hOldPen As Long  ' handle to Form1's previously selected pen
Dim retval As Long  ' return value

' Create the square dashed green pen with a width of zero (always one pixel).
hPen = CreatePen(PS_DASH, 0, RGB(0, 255, 0))
' Select the pen for use by window Form1.
hOldPen = SelectObject(Form1.hDC, hPen)

' Draw an ellipse with bounding rectangle (100,150)-(350,300).
retval = Ellipse(Form1.hDC, 100, 150, 350, 300)

' Select the old pen for use by Form1.
retval = SelectObject(Form1.hDC, hOldPen)
' Delete the pen we created to free up resources.
retval = DeleteObject(hPen)

See Also

CreatePenIndirect

Category

Pens

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: October 16, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/createpen.html