admin管理员组

文章数量:829186

画笔和画刷的种类和使用方法

画笔的使用和上一章的字体类似,首先用CreatePen()函数创建一个画笔对象,然后与设备文本绑定,同时得到旧的画笔。用完后删除新画笔,并且恢复旧画笔。画笔的种类有7种,参照下表及下面的例子。

说明
PS_SOLID实线
PS_DASH破折线(只有宽度为1,或小于等于设备的基本象素时有效)
PS_DOT点(只有宽度为1,或小于等于设备的基本象素时有效)
PS_DASHDOT实线和点间隔(只有宽度为1,或小于等于设备的基本象素时有效)
PS_DASHDOTDOT实线和点和点间隔(只有宽度为1,或小于等于设备的基本象素时有效)
PS_NULL不显示线
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.

VC中并不提供指定2点画线的功能,LineTo()函数从上一次画笔所在位置作为起点,一直画到LineTo()函数指定位置终止。如果要从指定位置画到另一指定位置这就需要用到MoveToEx()函数。MoveToEx()函数只移动画笔的位置,并不画线。

BOOL MoveToEx(                            BOOL LineTo(                  HPEN CreatePen(HDC hdc,         //HDC                     HDC hdc,  //HDC                 int 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,        //HDC                       int 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;
}

本文标签: 画笔和画刷的种类和使用方法