创建、选择和删除画笔


     尽管使用备用对象中的画笔非常方便,但只能使用实心的黑色画笔、实心的白色画笔或者是没有画笔三种情况。如果想获得更丰富的效果,则必须创建自己的画笔。

     创建画笔的一般过程为:调用 CreatePen 或者 CreatePenIndirect 函数创建一个 “ 逻辑画笔 ”,这些函数会返回逻辑画笔的句柄。然后需要调用 SelectObject 函数将画笔选入设备环境中。接着就可以用使用这个新的画笔来绘制线条。一次只能有一支画笔被选入设备环境。释放设备环境之后 (或者将其他画笔选入设备环境之后),需要调用 DeleteObject 函数来删除你创建的逻辑画笔。此后,该逻辑画笔的句柄不在有效。


       逻辑画笔是一个“GDI对象” ,一个程序可以创建 6 中 GDI 对象,它是其中之一,其他 5 种分别是画刷、位图、区域、字体和调色板。除了调色板之外,所有这些对象都通过 SelectObject 函数选入设备环境。

      下面三条规则控制画笔等 GDI 对象的使用:

      * 最终应当删除你所创建的所有 GDI 对象。

      * 当 GDI 对象被选入一个有效的设备环境时,不要删除它。

      * 不要删除备用对象。


假定程序使用三种画笔(宽度为 1 的黑色画笔、宽度为 3 的红色画笔 和 黑色的点线画笔 )。可以首先定义存储这些画笔的句柄的静态变量:

      static  HPEN  hPen1,  hPen2,  hPen3 ;

在处理 WM_PAINT 消息时(或者在任何拥有有效的设备环境句柄时),可以将其任何一支画笔选入到设备环境,并且使用它来绘制线条):

      SelectObject ( hdc, hPen2 ) ;

        [line-drawing functions]

      SelectObject ( hdc, hPen1 ) ;

       [line-drawing functions]

在处理 WM_DESTROY 消息时,可以删除这三种画笔:

       DeleteObject ( hPen1 ) ;

       DeleteObject ( hPen2 ) ;

       DeleteObject ( hPen3 ) ;


可以随时创建画笔,还可以将 CreatePen 和 SelectObject 调用组合到一条语句中:

    SelectObject ( hdc, CreatePen ( PS_DASH, 0, RGB (255, 0, 0 ) ) ) ;

现在,在你绘制直线是,将使用一个红色的虚线画笔。红色虚线绘制完成后,可以删除画笔。糟糕,你没有保存画笔,怎么才能删除画笔呢? 前面介绍过,SelectObject 函数将返回先前被选入设备环境的画笔句柄。也就是说,可以通过将备用的 BLACK_PEN 选入到设备环境中来得到需要被删除的画笔句柄,然后删除它:

     DeleteObject ( SelectObject (hdc, GetStockObject ( BLACK_PEN) ) ) ;

下面给出令一种方法。当一支画笔选入到一个新创建的设备环境时,保存 SelectObject 返回的画笔句柄:
     hPen = SelectObject ( hdc , CreatePen ( PS_DASH, 0, RGB (255, 0, 0 ) ) ) ;

hPen 究竟是什么呢?如果这是在获得设备环境后第一次调用 Selectobject  函数,hPen 就是 BLACK_PEN 的句柄。现在可以在同一条语句中选择该画笔到设备环境,并且删除自己创建的画笔 (第二次 Selectobject 调用返回你创建的画笔的句柄 ):

     DeleteObject ( SelectObject ( hdc, hPen ) ) ;

如果有一个画笔的句柄,LOGPEN 结构中各个字段成员的值就是可以通过调用 GetObject 函数获得:

     GetObject ( hpen, sizeof (LOGPEN), (LPVOID) &logpen ) ;

如果需要获得当前被选入设备环境的画笔句柄,则调用:

     hPen = GetCurrentObject ( hdc,OBJ_PEN ) ;

另外,还有一个画笔创建函数:ExtCreatePen,详见MSDN。


附:

CreatePen

The CreatePen function creates a logical pen that has the specified style, width, and color. The pen can subsequently be selected into a device context and used to draw lines and curves.

HPEN CreatePen(
  int fnPenStyle,    // pen style
  int nWidth,        // pen width
  COLORREF crColor   // pen color
);
Parameters
fnPenStyle
[in] Specifies the pen style. It can be any one of the following values.
ValueMeaning
PS_SOLIDThe pen is solid.
PS_DASHThe pen is dashed. This style is valid only when the pen width is one or less in device units.
PS_DOTThe pen is dotted. This style is valid only when the pen width is one or less in device units.
PS_DASHDOTThe pen has alternating dashes and dots. This style is valid only when the pen width is one or less in device units.
PS_DASHDOTDOTThe pen has alternating dashes and double dots. This style is valid only when the pen width is one or less in device units.
PS_NULLThe pen is invisible.
PS_INSIDEFRAMEThe pen is solid. When this pen is used in any GDI drawing function that takes a bounding rectangle, the dimensions of the figure are shrunk so that it fits entirely in the bounding rectangle, taking into account the width of the pen. This applies only to geometric pens.

nWidth
[in] Specifies the width of the pen, in logical units. If nWidth is zero, the pen is a single pixel wide, regardless of the current transformation.

CreatePen returns a pen with the specified width bit with the PS_SOLID style if you specify a width greater than one for the following styles: PS_DASH, PS_DOT, PS_DASHDOT, PS_DASHDOTDOT.

crColor
[in] Specifies a color reference for the pen color. To generate a COLORREF structure, use the RGB macro.
Return Values

If the function succeeds, the return value is a handle that identifies a logical pen.

If the function fails, the return value is NULL.

Windows NT/2000: To get extended error information, call GetLastError.

Remarks

After an application creates a logical pen, it can select that pen into a device context by calling the SelectObject function. After a pen is selected into a device context, it can be used to draw lines and curves.

If the value specified by the nWidth parameter is zero, a line drawn with the created pen always is a single pixel wide regardless of the current transformation.

If the value specified by nWidth is greater than 1, the fnPenStyle parameter must be PS_NULL, PS_SOLID, or PS_INSIDEFRAME.

If the value specified by nWidth is greater than 1 and fnPenStyle is PS_INSIDEFRAME, the line associated with the pen is drawn inside the frame of all primitives except polygons and polylines.

If the value specified by nWidth is greater than 1, fnPenStyle is PS_INSIDEFRAME, and the color specified by the crColor parameter does not match one of the entries in the logical palette, the system draws lines by using a dithered color. Dithered colors are not available with solid pens.

When you no longer need the pen, call the DeleteObject function to delete it.

ICM: No color management is done at creation. However, color management is performed when the pen is selected into an ICM-enabled device context. 




CreatePenIndirect

The CreatePenIndirect function creates a logical cosmetic pen that has the style, width, and color specified in a structure.

HPEN CreatePenIndirect(
  CONST LOGPEN *lplgpn   // style, width, and color
);
Parameters
lplgpn
[in] Pointer to a LOGPEN structure that specifies the pen's style, width, and color.
Return Values

If the function succeeds, the return value is a handle that identifies a logical cosmetic pen.

If the function fails, the return value is NULL.

Windows NT/2000: To get extended error information, call GetLastError.

Remarks

After an application creates a logical pen, it can select that pen into a device context by calling the SelectObject function. After a pen is selected into a device context, it can be used to draw lines and curves.

When you no longer need the pen, call the DeleteObject function to delete it.





SelectObject

The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type.

HGDIOBJ SelectObject(
  HDC hdc,          // handle to DC
  HGDIOBJ hgdiobj   // handle to object
);
Parameters
hdc
[in] Handle to the DC.
hgdiobj
[in] Handle to the object to be selected. The specified object must have been created by using one of the following functions.
ObjectFunctions
BitmapCreateBitmap, CreateBitmapIndirect, CreateCompatibleBitmap, CreateDIBitmap, CreateDIBSection

(Bitmaps can be selected for memory DCs only, and for only one DC at a time.)

BrushCreateBrushIndirect, CreateDIBPatternBrush, CreateDIBPatternBrushPt, CreateHatchBrush, CreatePatternBrush, CreateSolidBrush
FontCreateFont, CreateFontIndirect
PenCreatePen, CreatePenIndirect
RegionCombineRgn, CreateEllipticRgn, CreateEllipticRgnIndirect, CreatePolygonRgn, CreateRectRgn, CreateRectRgnIndirect

Return Values

If the selected object is not a region and the function succeeds, the return value is a handle to the object being replaced. If the selected object is a region and the function succeeds, the return value is one of the following values.

ValueMeaning
SIMPLEREGIONRegion consists of a single rectangle.
COMPLEXREGIONRegion consists of more than one rectangle.
NULLREGIONRegion is empty.

If an error occurs and the selected object is not a region, the return value is NULL. Otherwise, it is GDI_ERROR.

Remarks

This function returns the previously selected object of the specified type. An application should always replace a new object with the original, default object after it has finished drawing with the new object.

An application cannot select a bitmap into more than one DC at a time.

ICM: If the object being selected is a brush or a pen, color management is performed. 





DeleteObject

The DeleteObject function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object. After the object is deleted, the specified handle is no longer valid.

BOOL DeleteObject(
  HGDIOBJ hObject   // handle to graphic object
);
Parameters
hObject
[in] Handle to a logical pen, brush, font, bitmap, region, or palette.
Return Values

If the function succeeds, the return value is nonzero.

If the specified handle is not valid or is currently selected into a DC, the return value is zero.

Windows NT/2000: To get extended error information, call GetLastError.

Remarks

Do not delete a drawing object (pen or brush) while it is still selected into a DC.

When a pattern brush is deleted, the bitmap associated with the brush is not deleted. The bitmap must be deleted independently.



    




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值