学习windows编程 day4 之 设置画刷

时间:2023-03-09 05:44:14
学习windows编程 day4 之 设置画刷
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static HBRUSH hBrush, hOldBrush;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
//系统画刷数共有11个
//画刷是使用8X8像素的位图进行填充的,而不是像素点。效率高
//获取系统画刷(不要用deleteObject删除)
//hBrush=GetStockObject(LTGRAY_BRUSH);//浅灰色
//hBrush = GetStockObject(NULL_BRUSH);//不设置画刷,则是不进行填充 //设置自定义画刷createSolidBrush,createHatchBrush,createBrushIndirect
//hBrush=CreateSolidBrush(RGB(255,255,0));
//hBrush = CreateHatchBrush(HS_BDIAGONAL, RGB(0, 255, 0));//数字是样式 查手册
LOGBRUSH bh[];
for (int i = ; i < ; i++)
{
bh[i].lbStyle = BS_HATCHED;
bh[i].lbHatch = i;
bh[i].lbColor = RGB(i * 4, , );
} //最后一个是系统画刷,不需要删除
bh[].lbStyle = BS_SOLID;
bh[].lbColor = RGB(, , ); //用于记录所需要删除的画刷
HBRUSH TotBrush[]; //绘制6个矩形,填充不同
for (int i = ; i < ;i++)
{
hBrush=CreateBrushIndirect(&bh[i]);
if (i < )
TotBrush[i] = hBrush;
SelectObject(hdc, hBrush);
Rectangle(hdc, + * i, , + * i, );
} for (int i = ; i < ;i++)
{
DeleteObject(TotBrush[i]);
} //替换画刷
// hOldBrush = SelectObject(hdc, hBrush);
//中间写字
// SetTextAlign(hdc,TA_CENTER);
// TextOut(hdc, rect.right / 2, rect.bottom / 2, L"this a test", 11);
// SetTextAlign(hdc, TA_LEFT);
// Ellipse(hdc, rect.right / 4, rect.bottom / 4 , rect.right * 3 / 4, rect.bottom * 3 / 4); SelectObject(hdc, hOldBrush);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
//DeleteObject(hBrush);//必须是自定义画刷
PostQuitMessage();
return ;
} return DefWindowProc(hwnd, message, wParam, lParam);
}