学习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[7];
        for (int i = 0; i < 6; i++)
        {
            bh[i].lbStyle = BS_HATCHED;
            bh[i].lbHatch = i;
            bh[i].lbColor = RGB(i * 40, 0, 0);
        }

        //最后一个是系统画刷,不需要删除
        bh[6].lbStyle = BS_SOLID;
        bh[6].lbColor = RGB(255, 255, 0);

        //用于记录所需要删除的画刷
        HBRUSH TotBrush[6];

        //绘制6个矩形,填充不同
        for (int i = 0; i < 7;i++)
        {
            hBrush=CreateBrushIndirect(&bh[i]);
            if (i < 6)
                TotBrush[i] = hBrush;
            SelectObject(hdc, hBrush);
            Rectangle(hdc, 100 + 110 * i, 100, 200 + 110 * i, 200);
        }

        for (int i = 0; i < 6;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(0);
        return 0;
    }


    return DefWindowProc(hwnd, message, wParam, lParam);
}

 

posted @ 2018-03-05 16:19  山上有风景  阅读(428)  评论(0编辑  收藏  举报