画笔和画刷是作图的基础,本章介绍的是静态的描画,下一章介绍用鼠标动态地作画。
画笔的使用和上一章的字体类似,首先用CreatePen()函数创建一个画笔对象,然后与设备文本绑定,同时得到旧的画笔。用完后删除新画笔,并且恢复旧画笔。画笔的种类有7种,参照下表及下面的例子。
值 | 说明 |
---|---|
PS_SOLID | 实线 |
PS_DASH | 破折线(只有宽度为1,或小于等于设备的基本象素时有效) |
PS_DOT | 点(只有宽度为1,或小于等于设备的基本象素时有效) |
PS_DASHDOT | 实线和点间隔(只有宽度为1,或小于等于设备的基本象素时有效) |
PS_DASHDOTDOT | 实线和点和点间隔(只有宽度为1,或小于等于设备的基本象素时有效) |
PS_NULL | 不显示线 |
PS_INSIDEFRAME | The 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. |
VC中并不提供指定2点画线的功能,LineTo()函数从上一次画笔所在位置作为起点,一直画到LineTo()函数指定位置终止。如果要从指定位置画到另一指定位置这就需要用到MoveToEx()函数。MoveToEx()函数只移动画笔的位置,并不画线。
BOOL MoveToEx( BOOL LineTo( HPEN CreatePen( HDC hdc, //HDC HDC hdc, //HDCint
fnPenStyle, //画笔类型int
X, //x座标int
X, //x座标int
nWidth, //画笔宽度int
Y, //y座标int
Y, //y座标 COLORREF crColor //画笔颜色 LPPOINT lpPoint //返回先前位置 ); ); );
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {; HDC hdc; HPEN hNewPen, hOldPen; PAINTSTRUCT ps;switch
(message) {case
WM_PAINT: hdc = BeginPaint(hWnd, &ps); hNewPen = CreatePen(PS_SOLID,1,0x0000FF); hOldPen = (HPEN)SelectObject(hdc, hNewPen); MoveToEx(hdc, 10, 10, NULL); LineTo(hdc, 200, 10); DeleteObject(hNewPen); hNewPen = CreatePen(PS_DASH,1,0x00FF00); SelectObject(hdc, hNewPen); MoveToEx(hdc, 10, 20, NULL); LineTo(hdc, 200, 20); DeleteObject(hNewPen); hNewPen = CreatePen(PS_DOT,1,0xFFFF00); SelectObject(hdc, hNewPen); MoveToEx(hdc, 10, 30, NULL); LineTo(hdc, 200, 30); DeleteObject(hNewPen); hNewPen = CreatePen(PS_DASHDOT,1,0xFF0000); SelectObject(hdc, hNewPen); MoveToEx(hdc, 10, 40, NULL); LineTo(hdc, 200, 40); DeleteObject(hNewPen); hNewPen = CreatePen(PS_DASHDOTDOT,1,0xFF00FF); SelectObject(hdc, hNewPen); MoveToEx(hdc, 10, 50, NULL); LineTo(hdc, 200, 50); SelectObject(hdc, hOldPen); DeleteObject(hNewPen); EndPaint(hWnd, &ps);break
;case
WM_DESTROY: PostQuitMessage(0);break
;default
:return
DefWindowProc(hWnd, message, wParam, lParam); }return
0; }
画刷的使用和画笔相同,创建画刷有两个函数,CreateHatchBrush()函数创建带图案的画刷,而CreateSolidBrush()函数不带图案。画刷图案主要有6种,而空画刷不需要创建。
BOOL Rectangle( BOOL RoundRect( HBRUSH CreateHatchBrush( HDC hdc, //HDC HDC hdc, //HDCint
fnStyle, //HDCint
nLeftRect, //左上角x座标int
nLeftRect, //左上角x座标 COLORREF clrref //背景色int
nTopRect, //左上角y座标int
nTopRect, //左上角y座标 );int
nRightRect, //右下角x座标int
nRightRect, //右下角x座标int
nBottomRect //右下角y座标int
nBottomRect //右下角y座标 );int
nWidth //圆角宽度 HBRUSH CreateSolidBrush(int
nHeight //圆角高度 COLORREF clrref //背景色 ); );
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc;switch
(message) {case
WM_PAINT: hdc = BeginPaint(hWnd, &ps); DrawGr(hdc); EndPaint(hWnd, &ps);break
;case
WM_DESTROY: PostQuitMessage(0);break
;default
:return
DefWindowProc(hWnd, message, wParam, lParam); }return
0; } //画6个方框 int DrawGr(HDC hdc) { HPEN hPen, hOldPen; //画笔 HBRUSH hBrush, hOldBrush; //画刷 hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //画笔 hOldPen = (HPEN)SelectObject(hdc, hPen); hBrush = CreateHatchBrush(HS_CROSS, RGB(0, 255, 0)); //方格刷 hOldBrush = (HBRUSH)SelectObject(hdc, hBrush); Rectangle(hdc, 10, 10, 210, 110); //画矩形 DeleteObject(hPen); //删除新笔 DeleteObject(hBrush); //删除新刷 hPen = CreatePen(PS_DASH, 1, RGB(0, 255, 0)); //画笔 SelectObject(hdc, hPen); hBrush = CreateHatchBrush(HS_BDIAGONAL, RGB(0, 0, 255)); //45度斜线刷 SelectObject(hdc, hBrush); Rectangle(hdc, 40, 40, 240, 140); //画矩形 DeleteObject(hPen); //删除新笔 DeleteObject(hBrush); //删除新刷 hPen = CreatePen(PS_DOT, 1, RGB(0, 0, 255)); //画笔 SelectObject(hdc, hPen); SelectObject(hdc, GetStockObject(NULL_BRUSH)); //注意:选用空画刷 Rectangle(hdc, 70, 70, 270, 170); //画矩形 DeleteObject(hPen); //删除新笔 hPen = CreatePen(PS_DASHDOTDOT, 1, RGB(0, 255, 0)); //画笔 SelectObject(hdc, hPen); hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 0, 255)); //交叉斜线刷 SelectObject(hdc, hBrush); Rectangle(hdc, 100, 100, 300, 200); //画矩形 DeleteObject(hPen); //删除新笔 DeleteObject(hBrush); //删除新刷 hPen = CreatePen(PS_NULL, 1, NULL); //注意:无边框画笔 SelectObject(hdc, hPen); hBrush = CreateHatchBrush(HS_FDIAGONAL, RGB(0, 154, 154)); //45度反斜线刷 SelectObject(hdc, hBrush); Rectangle(hdc, 130, 130, 330, 230); //画矩形 DeleteObject(hPen); //删除新笔 DeleteObject(hBrush); //删除新刷 hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); //画笔 SelectObject(hdc, hPen); hBrush = CreateSolidBrush(RGB(255, 255, 217)); //实心画刷 SelectObject(hdc, hBrush); Rectangle(hdc, 160, 160, 360, 260); //画矩形 SelectObject(hdc, hOldPen); //恢复旧笔 SelectObject(hdc, hOldBrush); //恢复旧刷 DeleteObject(hPen); //删除新笔 DeleteObject(hBrush); //删除新刷return
0; }
将Rectangle()函数换成RoundRect()函数便可画出圆角方框,注意多了2个参数。
int DrawGr(HDC hdc)
{
HPEN hPen, hOldPen; //画笔
HBRUSH hBrush, hOldBrush; //画刷
hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //画笔
hOldPen = (HPEN)SelectObject(hdc, hPen);
hBrush = CreateHatchBrush(HS_CROSS, RGB(0, 255, 0)); //方格刷
hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
RoundRect(hdc, 10, 10, 210, 110, 5, 5); //画矩形
DeleteObject(hPen); //删除新笔
DeleteObject(hBrush); //删除新刷
hPen = CreatePen(PS_DASH, 1, RGB(0, 255, 0)); //画笔
SelectObject(hdc, hPen);
hBrush = CreateHatchBrush(HS_BDIAGONAL, RGB(0, 0, 255)); //45度斜线刷
SelectObject(hdc, hBrush);
RoundRect(hdc, 40, 40, 240, 140, 10, 10); //画矩形
DeleteObject(hPen); //删除新笔
DeleteObject(hBrush); //删除新刷
hPen = CreatePen(PS_DOT, 1, RGB(0, 0, 255)); //画笔
SelectObject(hdc, hPen);
SelectObject(hdc, GetStockObject(NULL_BRUSH)); //注意:选用空画刷
RoundRect(hdc, 70, 70, 270, 170, 15, 15); //画矩形
DeleteObject(hPen); //删除新笔
hPen = CreatePen(PS_DASHDOTDOT, 1, RGB(0, 255, 0)); //画笔
SelectObject(hdc, hPen);
hBrush = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 0, 255)); //交叉斜线刷
SelectObject(hdc, hBrush);
RoundRect(hdc, 100, 100, 300, 200, 20, 20); //画矩形
DeleteObject(hPen); //删除新笔
DeleteObject(hBrush); //删除新刷
hPen = CreatePen(PS_NULL, 1, NULL); //注意:无边框画笔
SelectObject(hdc, hPen);
hBrush = CreateHatchBrush(HS_FDIAGONAL, RGB(0, 154, 154)); //45度反斜线刷
SelectObject(hdc, hBrush);
RoundRect(hdc, 130, 130, 330, 230, 25, 25); //画矩形
DeleteObject(hPen); //删除新笔
DeleteObject(hBrush); //删除新刷
hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); //画笔
SelectObject(hdc, hPen);
hBrush = CreateSolidBrush(RGB(255, 255, 217)); //实心画刷
SelectObject(hdc, hBrush);
RoundRect(hdc, 160, 160, 360, 260, 30, 30); //画矩形
SelectObject(hdc, hOldPen); //恢复旧笔
SelectObject(hdc, hOldBrush); //恢复旧刷
DeleteObject(hPen); //删除新笔
DeleteObject(hBrush); //删除新刷
return
0;
}