您的位置:首页 > 编程语言

Windows编程-windows GDI基本概念 画笔 画刷

2013-02-17 14:23 453 查看
其中关于windows GDI的描述如下: Purpose The Microsoft Windows graphics device interface (GDI) enables applications to use graphics and formatted text on both the video display and the printer. Windows-based applications do not access the graphics hardware directly. Instead, GDI interacts with device drivers on behalf of applications.

Where applicable GDI can be used in all Windows-based applications.

GDI使用画刷和画笔来完成绘制图形工作,一般同时只使用一个画刷和画笔。

(1) 画笔

首先,画笔的句柄为

HPEN white_pen = NULL;


这里,white_pen是一个画笔的句柄。可以通过上面的两种方法来创建。

A. 方法一,使用系统默认的存储对象。

The GetStockObject function retrieves a handle to one of the stock pens, brushes, fonts, or palettes.

HGDIOBJ GetStockObject(
_In_  int fnObject
);


Parameters
fnObject [in]   The type of stock object. This parameter can be one of the following values.

ValueMeaning
BLACK_BRUSH

Black brush.

DKGRAY_BRUSH

Dark gray brush.

DC_BRUSH

Solid color brush. The default color is white. The color can be changed by using the SetDCBrushColor function. For more information, see the Remarks section.

GRAY_BRUSH

Gray brush.

[align=left]HOLLOW_BRUSH[/align]
Hollow brush (equivalent to NULL_BRUSH).

LTGRAY_BRUSH

Light gray brush.

NULL_BRUSH

Null brush (equivalent to HOLLOW_BRUSH).

WHITE_BRUSH

White brush.

BLACK_PEN

Black pen.

WHITE_PEN

White pen.

DC_PEN

Solid pen color. The default color is white. The color can be changed by using the SetDCPenColor function. For more information, see the Remarks section.

NULL_PEN

Null pen. The null pen draws nothing.

ANSI_FIXED_FONT

Windows fixed-pitch (monospace) system font.

ANSI_VAR_FONT

Windows variable-pitch (proportional space) system font.

DEVICE_DEFAULT_FONT

Device-dependent font.

DEFAULT_GUI_FONT

Default font for user interface objects such as menus and dialog boxes. It is not recommended that you use DEFAULT_GUI_FONT or SYSTEM_FONT to obtain the font used by dialogs and windows; for more information, see the remarks section.

The default font is Tahoma.

OEM_FIXED_FONT

Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font.

SYSTEM_FONT

System font. By default, the system uses the system font to draw menus, dialog box controls, and text. It is not recommended that you use DEFAULT_GUI_FONT or SYSTEM_FONT to obtain the font used by dialogs and windows; for more information, see the remarks section.

The default system font is Tahoma.

SYSTEM_FIXED_FONT

Fixed-pitch (monospace) system font. This stock object is provided only for compatibility with 16-bit Windows versions earlier than 3.0.

DEFAULT_PALETTE

Default palette. This palette consists of the static colors in the system palette.

Return value

If the function succeeds, the return value is a handle to the requested logical object.

If the function fails, the return value is NULL.

例,创建一个白色的画笔。

HPEN white_pen = NULL;
white_pen = GetStockObject(WHITE_PEN);


B. 方法二,创建用户自定义的画笔。

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(
_In_  int fnPenStyle,
_In_  int nWidth,
_In_  COLORREF crColor
);


Parameters
fnPenStyle [in]  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]

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]
A color reference for the pen color. To generate a COLORREF structure, use the RGB macro.

Return value

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

If the function fails, the return value is NULL.

例,自定义创建一只白色虚线画笔。

HPEN white_dashed_pen = CreatePen( PS_DASHED, 1, RGB(255,255,255));


(2) 画刷

画刷创建的方法同画笔类似,也具有两种方法。前面已经讨论过了GetStockObject的方法了。这里我们主要讨论CreateSolidBrush和CreateHatchBrush的方法。

A. The CreateSolidBrush function creates a logical brush that has the specified solid color.

HBRUSH CreateSolidBrush(
_In_  COLORREF crColor
);


Parameters
crColor [in]
The color of the brush. To create a COLORREF color value, use the RGB macro.

Return value

If the function succeeds, the return value identifies a logical brush.

If the function fails, the return value is NULL.

 

B. The CreateHatchBrush function creates a logical brush that has the specified hatch pattern and color.

HBRUSH CreateHatchBrush(
_In_  int fnStyle,
_In_  COLORREF clrref
);


Parameters

fnStyle [in]     The hatch style of the brush. This parameter can be one of the following values.

Value

Meaning
HS_BDIAGONAL45-degree upward left-to-right hatch
HS_CROSSHorizontal and vertical crosshatch
HS_DIAGCROSS45-degree crosshatch
HS_FDIAGONAL45-degree downward left-to-right hatch
HS_HORIZONTALHorizontal hatch
HS_VERTICALVertical hatch
clrref [in]   The foreground color of the brush that is used for the hatches. To create a COLORREF color value, use the RGB macro.

Return value

If the function succeeds, the return value identifies a logical brush.

If the function fails, the return value is NULL.

(3)选择GDI对象到图形设备描述表

下面讨论如何利用设备描述表选中画笔。利用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(
_In_  HDC hdc,
_In_  HGDIOBJ hgdiobj
);


Parameters
hdc [in]
A handle to the DC.

hgdiobj [in]
A 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 only be selected into memory DC's. A single bitmap cannot be selected into more than one DC at the same time.

BrushCreateBrushIndirect, CreateDIBPatternBrush, CreateDIBPatternBrushPt, CreateHatchBrush, CreatePatternBrush, CreateSolidBrush

FontCreateFont, CreateFontIndirect
PenCreatePen, CreatePenIndirect
RegionCombineRgn, CreateEllipticRgn, CreateEllipticRgnIndirect, CreatePolygonRgn, CreateRectRgn, CreateRectRgnIndirect

Return value

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.

(4)销毁资源

采用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(
_In_  HGDIOBJ hObject
);


Parameters
hObject [in]
A handle to a logical pen, brush, font, bitmap, region, or palette.

Return value

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.

(5)例子:使用画笔和画刷

下面的例子是在图形描述表中选定一个画笔并保存旧画笔的例子:

HDC hdc;
HPEN blue_pen = CreatePen(PS_SOLID, 1, RGB(0,0,255));
HPEN old_pen = NULL;
old_pen = SelectObject(hdc, blue_pen);
// do drawing...
// restore the old pen
SelectObject(hdc, old_pen);
// delete the pens
DeleteObject(blue_pen);
DeleteObject(old_pen);


下面的例子为创建一个画刷,画刷的风格为交叉阴影线。

HBRUSH  red_hbrush = CreateHatchBrush( HS_CROSS, RGB(255, 0, 0));
// select the red brush and save the old brush.
HBRUSH old_brush = SelectObject(hdc, red_hbrush);
// restore the brush
SelectObject(hdc, old_brush);
// delete the brush
DeleteObject(red_hbrush);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: