WinAPI : The CreatePen Function
Getting ahead from where we ended in previous post Pen Object in Device Context, let’s see how to create a pen and associate it with the device context.
The first function that we will require is the CreatePen function that will create a new pen. The first parameter that it takes is the Pen Style that can be any one of the constants defined below. The next parameter is the width of the pen in pixels. The last parameter is the Color of the Pen. RGB() Macro should be used to get the value that’s to be passed for this parameter. If the function succeeds the return value is the handle to the Pen object. This return value should be stored in a variable as, while the Pen has been created, it’s not currently associated with the device context and this handle to the Pen object would be required to do that.
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, _
ByVal nWidth As Long, ByVal crColor As Long) As Long
Private Const PS_SOLID = 0
Private Const PS_DASH = 1
Private Const PS_DOT = 2
Private Const PS_DASHDOT = 3
Private Const PS_DASHDOTDOT = 4
Private Const PS_NULL = 5
Private Const PS_INSIDEFRAME = 6

The first five constants as defined above results in different line styles which can be viewed in the screenshot. The PS_NULL style results in drawing invisible lines. The PS_INSIDEFRAME style, if passed, specifies that the size of the figure being drawn should be shrunk if has a bounding rectangle associated with it and the width of the pen is wider than 1 pixel.
It should be noted here that :
If the width of the pen is passed 0 the pen created has a width of 1 pixel.
If the width of the pen is greater that 1 then the pen will be treated as a Geometric Pen and will have PS_SOLID style irrespective of the styles passed with the function.
The PS_INSIDEFRAME is applicable only for Geometric Pens.
Now, as we have created a Pen we will need to associate it to the device context so that the next time something is drawn requiring a pen, it’s done using the pen that we have created. This is done using the SelectObject function. The first parameter that the function takes is the handle to the device context and the second parameter is the handle to the Pen object that we have created. If the function succeeds the return value is the handle to the Pen object that was in use until we replaced it with ours.
Private Declare Function SelectObject Lib "gdi32" ( _
ByVal hdc As Long, ByVal hObject As Long) As Long
Now if we are too concerned about memory issues it’s advisable that if the Pen that’s not required any further be destroyed which we can do using DeleteObject function. The function takes a single parameter which is the handle to the object (here Pen) that we need to destroy.
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long



da
mijak
January 2, 2009 at 6:40 pm
[...] WinAPI : The CreatePen Function [...]
WinAPI functions consolidated « Jalaj P. Jha
December 11, 2008 at 1:08 am
[...] Now use the SelectObject function to associate this newly created pen with the device context. (already mention in previous post WinAPI : The CreatePen Function) [...]
Modifying Existing Pen in DC « Jalaj
February 26, 2007 at 11:32 am