绘制、Select 并拖动矩形 WinApi

Draw, Select and Drag Rectangle WinApi

我在拖动矩形和 select 处理它们时遇到问题。

我必须绘制多个矩形(使用鼠标左键),然后 select 其中一个并将其拖到 window 周围(使用鼠标右键)。但是当我select它的时候,它不动。

selection 的问题 - 当我尝试 select 它们按降序排列(按照绘制它们的方式)时,它们全部 select。

这是我的代码:

POINT LeftButtonDown;
POINT ptCurrent;
POINT ptClicked;
vector<CRect> vRect;
bool isRubberBand = false;
bool IsClicked(CRect r, int x, int y);

void select(HWND hWnd, CRect r);
void deselect(HWND hWnd, CRect r);
void DrawRubberBand(HWND hWnd);
void MoveFromTo(HDC hdc, CRect rectSelected, POINTS anchor, POINTS now);

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
CRect add;
CRect rectSelected;

HDC             hdc;
static POINTS   anchor;

switch (message)
{
case WM_COMMAND:
{
    int wmId = LOWORD(wParam);
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_ABOUT:
        DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
        break;
    case IDM_EXIT:
        DestroyWindow(hWnd);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
}
break;

case WM_PAINT:
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code that uses hdc here...
    for (auto& rc : vRect)
    {
        Rectangle(hdc,
            rc.left,
            rc.top,
            rc.right,
            rc.bottom
        );
    }
    EndPaint(hWnd, &ps);
}
break;

case WM_LBUTTONDOWN:
{
    LeftButtonDown.x = LOWORD(lParam); //Start point
    LeftButtonDown.y = HIWORD(lParam);

    isRubberBand = true;

    ptCurrent.x = LOWORD(lParam); //current point
    ptCurrent.y = HIWORD(lParam);

    DrawRubberBand(hWnd); //draw the rect
}
break;

case WM_LBUTTONUP: // adding to a vector
{
    if (!isRubberBand)
    {
        return 0;
    }
    isRubberBand = false;

    //InvalidateRect(hWnd, NULL, TRUE);
    add.SetRect(LeftButtonDown.x, LeftButtonDown.y, ptCurrent.x, ptCurrent.y); //setting the coords of the rect
    vRect.push_back(add); //add the rect
    UpdateWindow(hWnd);
}
break;

case WM_MOUSEMOVE:

    if (wParam & MK_LBUTTON) //drawing the rectangle
    {
        hdc = GetDC(hWnd);
        if (!isRubberBand)
        {
            break;
        }
        DrawRubberBand(hWnd);

        ptCurrent.x = LOWORD(lParam);
        ptCurrent.y = HIWORD(lParam);

        DrawRubberBand(hWnd);

    }

    if (wParam & MK_RBUTTON) //dragging the rectangle
    {
        hdc = GetDC(hWnd);

        POINTS now = MAKEPOINTS(lParam);
        SetROP2(hdc, R2_NOTXORPEN);
        MoveFromTo(hdc,rectSelected,anchor,now);
        InvalidateRect(hWnd, NULL, TRUE);

        anchor = MAKEPOINTS(lParam);

        ReleaseDC(hWnd, hdc);

    }
    break;

case WM_RBUTTONDOWN:
{
    int x{ LOWORD(lParam) }, y{ HIWORD(lParam) };

    for (auto& rc : vRect)
    {
        if (IsClicked(rc, x, y))
        {
            select(hWnd, rc);
            rectSelected= rc;
            anchor = MAKEPOINTS(lParam);

            break;
        }
        else {
            deselect(hWnd, rc);
        }
    }
    //select-deselect

}
break;
case WM_RBUTTONUP:
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


bool IsClicked(CRect r, int x, int y)
{
    POINT pt{ x, y };
    return (bool)PtInRect(&r, pt);
}
void select(HWND hWnd, CRect r) {
    HDC hdc = GetDC(hWnd);
    HPEN selectPen = CreatePen(PS_DOT, 0, RGB(255, 0, 0));
    SelectObject(hdc, selectPen);
    Rectangle(hdc, r.left, r.top, r.right, r.bottom);
    DeleteObject(selectPen);
}
void deselect(HWND hWnd, CRect r) {
    HDC hdc = GetDC(hWnd);
    Rectangle(hdc, r.left, r.top, r.right, r.bottom);
}

void DrawRubberBand(HWND hWnd) {
    HDC hdc = GetDC(hWnd);

    SetROP2(hdc, R2_NOT);
    SelectObject(hdc, GetStockObject(NULL_BRUSH));
    Rectangle(hdc,
        LeftButtonDown.x,
        LeftButtonDown.y,
        ptCurrent.x,
        ptCurrent.y
    );
    ReleaseDC(hWnd, hdc);
  }
  void MoveFromTo(HDC hdc, CRect rectSelected, POINTS anchor, POINTS now) 
  {
    Rectangle(hdc, rectSelected.left, rectSelected.top, rectSelected.right, rectSelected.bottom);
    rectSelected.left = rectSelected.left + now.x - anchor.x; //x
    rectSelected.top = rectSelected.top + now.y - anchor.y;    //y
    rectSelected.right = rectSelected.right + now.x - anchor.x; //x
    rectSelected.bottom = rectSelected.bottom + now.y - anchor.y; //y
    Rectangle(hdc, rectSelected.left, rectSelected.top, rectSelected.right, rectSelected.bottom);
   }

之所以选择所有的矩形都是按降序排列的,是因为在WM_RBUTTONDOWN中找到了选中的矩形,循环被打破,导致后面的矩形取消选择没有执行。

矩形无法拖动的原因有以下几种:

  1. MoveFromTo函数要传递rectSelected的引用,否则函数中修改矩形的坐标不能应用到实际的矩形上

  2. 拖动前需要先找到矩形,将其从vRect中删除,然后将拖动的矩形添加到vRect中,这样才能保证每次在 WM_PAINT.

    中更新矩形

这是代码:

POINT LeftButtonDown;
POINT ptCurrent;
POINT ptClicked;
vector<CRect> vRect;
bool isRubberBand = false;
bool IsClicked(CRect r, int x, int y);

void select(HWND hWnd, CRect r);
void deselect(HWND hWnd, CRect r);
void DrawRubberBand(HWND hWnd);
void MoveFromTo(HDC hdc, CRect &rectSelected, POINTS anchor, POINTS now);

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    CRect add;
    static CRect rectSelected;
    TCHAR s[100] = L"";
    HDC             hdc;
    static POINTS   anchor;

    switch (message)
    {
    case WM_COMMAND:
    {
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    break;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code that uses hdc here...
        for (auto& rc : vRect)
        {
            Rectangle(hdc,
                rc.left,
                rc.top,
                rc.right,
                rc.bottom
            );
        }
        EndPaint(hWnd, &ps);
    }
    break;

    case WM_LBUTTONDOWN:
    {
        LeftButtonDown.x = LOWORD(lParam); //Start point
        LeftButtonDown.y = HIWORD(lParam);

        isRubberBand = true;

        ptCurrent.x = LOWORD(lParam); //current point
        ptCurrent.y = HIWORD(lParam);

        DrawRubberBand(hWnd); //draw the rect
    }
    break;

    case WM_LBUTTONUP: // adding to a vector
    {
        if (!isRubberBand)
        {
            return 0;
        }
        isRubberBand = false;

        //InvalidateRect(hWnd, NULL, TRUE);
        add.SetRect(LeftButtonDown.x, LeftButtonDown.y, ptCurrent.x, ptCurrent.y); //setting the coords of the rect
        vRect.push_back(add); //add the rect
        UpdateWindow(hWnd);
    }
    break;

    case WM_MOUSEMOVE:

        if (wParam & MK_LBUTTON) //drawing the rectangle
        {
            hdc = GetDC(hWnd);
            if (!isRubberBand)
            {
                break;
            }
            DrawRubberBand(hWnd);

            ptCurrent.x = LOWORD(lParam);
            ptCurrent.y = HIWORD(lParam);

            DrawRubberBand(hWnd);

        }
        if (wParam & MK_RBUTTON) //dragging the rectangle
        {
            hdc = GetDC(hWnd);
            POINTS now = MAKEPOINTS(lParam);
            if (PtInRect(&rectSelected, POINT{ now.x,now.y }))
            {
                SetROP2(hdc, R2_NOTXORPEN);
                MoveFromTo(hdc, rectSelected, anchor, now);
                InvalidateRect(hWnd, NULL, TRUE);
                anchor = MAKEPOINTS(lParam);
            }

            ReleaseDC(hWnd, hdc);
        }
        break;

    case WM_RBUTTONDOWN:
    {
        bool isFind = false;
        int x{ LOWORD(lParam) }, y{ HIWORD(lParam) };
        for (auto& rc : vRect)
        {
            if (!isFind && IsClicked(rc, x, y))
            {
                select(hWnd, rc);
                rectSelected = rc;
                anchor = MAKEPOINTS(lParam);
                isFind = TRUE;
            }
            else {
                deselect(hWnd, rc);
            }
        }
        //select-deselect
    }
    break;
    case WM_RBUTTONUP:
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}


bool IsClicked(CRect r, int x, int y)
{
    POINT pt{ x, y };
    return (bool)PtInRect(&r, pt);
}
void select(HWND hWnd, CRect r) {
    HDC hdc = GetDC(hWnd);
    HPEN selectPen = CreatePen(PS_DOT, 0, RGB(255, 0, 0));
    SelectObject(hdc, selectPen);
    Rectangle(hdc, r.left, r.top, r.right, r.bottom);
    DeleteObject(selectPen);
}
void deselect(HWND hWnd, CRect r) {
    HDC hdc = GetDC(hWnd);
    Rectangle(hdc, r.left, r.top, r.right, r.bottom);
}

void DrawRubberBand(HWND hWnd) {
    HDC hdc = GetDC(hWnd);

    SetROP2(hdc, R2_NOT);
    SelectObject(hdc, GetStockObject(NULL_BRUSH));
    Rectangle(hdc,
        LeftButtonDown.x,
        LeftButtonDown.y,
        ptCurrent.x,
        ptCurrent.y
    );
    ReleaseDC(hWnd, hdc);
}
void MoveFromTo(HDC hdc, CRect &rectSelected, POINTS anchor, POINTS now)
{
    Rectangle(hdc, rectSelected.left, rectSelected.top, rectSelected.right, rectSelected.bottom);
    for (auto it = vRect.begin();it != vRect.end();it++)
    {
        if (*it == rectSelected)
        {
            vRect.erase(it);
            break;
        }
    }
    rectSelected.left = rectSelected.left + now.x - anchor.x; //x
    rectSelected.top = rectSelected.top + now.y - anchor.y;    //y
    rectSelected.right = rectSelected.right + now.x - anchor.x; //x
    rectSelected.bottom = rectSelected.bottom + now.y - anchor.y; //y
    Rectangle(hdc, rectSelected.left, rectSelected.top, rectSelected.right, rectSelected.bottom);
    vRect.push_back(rectSelected);
}

对我有用: