Win32绘制一个拖动矩形

时间:2012-11-11 22:38:18

标签: c++ visual-c++ gdi

我正在尝试绘制一个矩形,当您左键单击一个点然后向右拖动时,您在矩形中拖动会获得表单,以显示矩形的外观预览。这工作正常,除非我拖回到一个较小的矩形,旧的矩形仍然存在。

    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code here...

    if(painting)
    {



            HPEN greenPen=CreatePen(PS_SOLID, 1, RGB(0,255,0));
            SelectObject(hdc,greenPen);
            Rectangle(hdc, x1, y1, x2, y2);

    }


    EndPaint(hWnd, &ps);
    break;
case WM_LBUTTONDOWN:
    painting=true;
    x1=LOWORD(lParam);
    y1=HIWORD(lParam);
    InvalidateRect(hWnd, NULL ,false);
    break;
case WM_MOUSEMOVE:



        x2=LOWORD(lParam);
        y2=HIWORD(lParam);

        InvalidateRect(hWnd, NULL ,false);


    break;
case WM_LBUTTONUP:

    painting=false;
    break;

1 个答案:

答案 0 :(得分:1)

@ user1788175 - 只需使用RECT结构。当绘图开始时,你设置左和右;顶部成员到鼠标的x,y pos。释放鼠标时,设置右侧,底部成员。如有必要,向左,向右交换,以确保向左

这是从我编写的用于处理选择矩形的类中删除的一些代码。您可以忽略规范化代码 - 它只是将像素坐标转换为[0..1]范围内的像素,因此我可以在图像的缩小版本上绘制选区,但如果图像仍然选择相同的区域以不同的比例显示。我的图像是4944x6992,因此我必须显示它们的缩小版本。

LRESULT CALLBACK cSelBoxImg::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    RECT mappedRect, tmpRect, myRect;
    HBRUSH myBrush;
    BITMAP bm;
    PAINTSTRUCT ps;
    HDC mDC;
    HBITMAP tmpBmp;

    switch (uMsg)
    {
    case WM_LBUTTONDOWN:
        if (bMouseSelectionEnabled)
        {
            bRubberBanding = true;
            mouseClickDownPos.x = LOWORD(lParam);
            mouseClickDownPos.y = HIWORD(lParam);

            curMousePos = mouseClickDownPos;
            selectionRect.left = min(curMousePos.x, mouseClickDownPos.x);
            selectionRect.right = max(curMousePos.x, mouseClickDownPos.x);
            selectionRect.top = min(curMousePos.y, mouseClickDownPos.y);
            selectionRect.bottom = max(curMousePos.y, mouseClickDownPos.y);
            normalizeSelRect();
            InvalidateRect(mHwnd, NULL, isBkgTransparent);
            PostMessage(GetParent(hWnd),  WM_COMMAND, GetDlgCtrlID(hWnd), (LPARAM)hWnd);
        }
        return 1;

    case WM_LBUTTONUP:
        if (bMouseSelectionEnabled)
            if (bRubberBanding)
            {
                bRubberBanding = false;
                mouseClickUpPos.x = LOWORD(lParam);
                mouseClickUpPos.y = HIWORD(lParam);

                selectionRect.left = min(mouseClickUpPos.x, mouseClickDownPos.x);
                selectionRect.right = max(mouseClickUpPos.x, mouseClickDownPos.x);
                selectionRect.top = min(mouseClickUpPos.y, mouseClickDownPos.y);
                selectionRect.bottom = max(mouseClickUpPos.y, mouseClickDownPos.y);

                normalizeSelRect();
                InvalidateRect(mHwnd, NULL, isBkgTransparent);
                PostMessage(GetParent(hWnd),  WM_COMMAND, GetDlgCtrlID(hWnd), (LPARAM)hWnd);
            }
        return 1;

    case WM_MOUSEMOVE:
        if (bMouseSelectionEnabled)
            if (bRubberBanding)
            {
                curMousePos.x = LOWORD(lParam);
                curMousePos.y = HIWORD(lParam);

                selectionRect.left = min(curMousePos.x, mouseClickDownPos.x);
                selectionRect.right = max(curMousePos.x, mouseClickDownPos.x);
                selectionRect.top = min(curMousePos.y, mouseClickDownPos.y);
                selectionRect.bottom = max(curMousePos.y, mouseClickDownPos.y);
                //                UpdateWindow(hWnd);
                //RedrawWindow(hWnd, NULL, NULL, RDW_UPDATENOW);
                normalizeSelRect();
                InvalidateRect(hWnd, NULL, false);
                PostMessage(GetParent(hWnd),  WM_COMMAND, GetDlgCtrlID(hWnd), (LPARAM)hWnd);
                //       printf("Message posted\n");
            }
        return 0;

    case WM_PAINT:

        mDC = BeginPaint(hWnd, &ps);

        GetClientRect(hWnd, &tmpRect);
    //        GetObject(mBmp, sizeof(bm), &bm);
        mappedRect.left = mLeft * tmpRect.right;
        mappedRect.right = mRight * tmpRect.right;
        mappedRect.top = mTop * tmpRect.bottom;
        mappedRect.bottom = mBottom * tmpRect.bottom;

        displayImage();
        if  (mBmp) drawRect(mDC, mappedRect, RGB(0,0,255));
        DeleteObject(tmpBmp);
        EndPaint(hWnd, &ps);

        return 0;

    case WM_ERASEBKGND:
        if (isBkgTransparent)
        {
            GetClientRect(hWnd, &myRect);
            myBrush = (HBRUSH) GetWindowLong(hWnd, GCL_HBRBACKGROUND);
            FillRect((HDC)wParam, &myRect, myBrush);
            printf("background cleared\n");
        }
        return true;

    case WM_SETCURSOR:
        SetCursor(mCursor);
        return true;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}