学习windows编程 day4 之 矩形的操作

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    static RECT rect,rect2,rect3;
    static cxClient, cyClient;
    static HBRUSH hBrush, hOldBrush;
    POINT pt;

    switch (message)
    {
    case WM_SIZE:
        cxClient = LOWORD(lParam);
        cyClient = HIWORD(lParam);
        break;
    case WM_LBUTTONDOWN:
        pt.x = LOWORD(lParam);
        pt.y = HIWORD(lParam);
        if (PtInRect(&rect2,pt))
        {
            MessageBox(NULL, L"clicked", L"info", NULL);
        }

        break;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
//处理矩形
//         rect.left = cxClient / 2 - 50;
//         rect.right = cxClient / 2 + 50;
//         rect.top = cyClient / 2 - 50;
//         rect.bottom = cyClient / 2 + 50;

        hBrush = CreateSolidBrush(RGB(255,0,0));
//1.fillrect    填充矩形
        //FillRect(hdc, &rect, hBrush);
//2.framerect    改画笔为画刷来绘制边框
        //FrameRect(hdc, &rect, hBrush);
//3.invertrect    翻转矩形内所有的像素
        //InvertRect(hdc, &rect);

//4.生成矩形SetRect
        SetRect(&rect, cxClient / 2 - 50, cyClient / 2 - 50, cxClient/2 + 50, cyClient/2 + 50);
//5.偏移矩形OffsetRect
        OffsetRect(&rect, -150, -150);
        FillRect(hdc, &rect, hBrush);
        InvertRect(hdc, &rect);    //先显示图像,后翻转
//6.增大减小,长宽同时InflateRect
        SetRect(&rect, cxClient / 2 - 50, cyClient / 2 - 50, cxClient / 2 + 50, cyClient / 2 + 50);
        InflateRect(&rect, 50, 30);
        FillRect(hdc, &rect, hBrush);
//7.setrectEmpty    设置矩形为空
        FillRect(hdc, &rect, hBrush);    //不会显示
//8.copyrect    多用在映射中
        CopyRect(&rect2, &rect);    //将rect的坐标拷贝给rect2
        SetRectEmpty(&rect);//将矩形的各个坐标设为0,则会显示rect2
        hBrush = CreateSolidBrush(RGB(255, 255, 0));
        FillRect(hdc, &rect2, hBrush);
//9.intersectrect    两个矩形交集
//10.unionrect        两个矩形并集
//11.Isrectempty    判断是否为空
//12.ptinrect        判断点是否在矩形中(重要)


        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }


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

 

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