画刷的使用方式和画笔是一样的
通过HBRUSH来创建画刷
系统预定义的画刷如下:
#define WHITE_BRUSH 0 //白画刷
#define LTGRAY_BRUSH 1 //亮画刷
#define GRAY_BRUSH 2 //灰画刷
#define DKGRAY_BRUSH 3
#define BLACK_BRUSH 4
#define NULL_BRUSH 5
#define HOLLOW_BRUSH NULL_BRUSH
画刷的创建:
static HBRUSH hBrush;
hBrush = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //GetStockObject返回值时,要转型
画刷除了系统预定义画刷以外,还有彩色画刷和阴影画刷:
CreateSolidBrush() //彩色画刷
HBRUSH CreateSolidBrush(
_In_ COLORREF crColor
);
//参数是RGB颜色xi
阴影画刷:
CreateHatchBrush()
HBRUSH CreateHatchBrush(
_In_ int fnStyle,
_In_ COLORREF clrref
);
//参数为阴影风格与RGB颜色
画刷风格:
#define HS_HORIZONTAL 0 /* ----- */
#define HS_VERTICAL 1 /* ||||| */
#define HS_FDIAGONAL 2 /* \\\\\ */
#define HS_BDIAGONAL 3 /* / */
#define HS_CROSS 4 /* +++++ */
#define HS_DIAGCROSS 5 /* xxxxx */
具体使用案例:
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
hBrush = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //GetStockObject返回值时,要转型
SelectObject(hdc, hBrush);
SelectObject(hdc, GetStockObject(NULL_PEN));
Rectangle(hdc, 100, 100, 400, 400); //无边框矩形
//彩色画刷
hBrush = (HBRUSH)CreateSolidBrush(RGB(255,255,0));
SelectObject(hdc, hBrush);
Ellipse(hdc, 300, 300, 500, 500);
//创建阴影画刷
hBrush = (HBRUSH)CreateHatchBrush(HS_BDIAGONAL, RGB(255,0,255));
SelectObject(hdc, hBrush);
Rectangle(hdc, 400, 400, 600, 600);
EndPaint(hWnd, &ps);
}
break;
效果如下: