Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绘制、选择并拖动矩形WinApi_Winapi_Draw_Drag - Fatal编程技术网

绘制、选择并拖动矩形WinApi

绘制、选择并拖动矩形WinApi,winapi,draw,drag,Winapi,Draw,Drag,我在拖动矩形和选择矩形时遇到问题 我必须画多个矩形(用鼠标左键),然后选择其中一个并在窗口周围拖动它(用鼠标右键)。但当我选择它时,它不会移动 选择的问题-当我尝试按降序(绘制它们的方式)选择它们时,它们全部被选中 这是我的密码: POINT LeftButtonDown; POINT ptCurrent; POINT ptClicked; vector<CRect> vRect; bool isRubberBand = false; bool IsClicked(CRect r,

我在拖动矩形和选择矩形时遇到问题

我必须画多个矩形(用鼠标左键),然后选择其中一个并在窗口周围拖动它(用鼠标右键)。但当我选择它时,它不会移动

选择的问题-当我尝试按降序(绘制它们的方式)选择它们时,它们全部被选中

这是我的密码:

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);
   }
指向LeftButtonDown;
点电流;
点击点;
矢量vRect;
bool-isRubberBand=false;
bool IsClicked(正确的r,int x,int y);
无效选择(HWND HWND,正确r);
无效取消选择(HWND HWND,正确r);
空心橡胶带(HWND HWND);
void MoveFromTo(HDC HDC,选择正确的矩形,点定位,点现在);
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
正确添加;
正确的选择;
HDC-HDC;
静态点锚;
开关(信息)
{
case WM_命令:
{
int wmId=低ORD(wParam);
//解析菜单选项:
交换机(wmId)
{
案例IDM_关于:
对话框(hInst、MAKEINTRESOURCE(IDD_ABOUTBOX)、hWnd、About);
打破
案例IDM_退出:
窗口(hWnd);
打破
违约:
返回DefWindowProc(hWnd、message、wParam、lParam);
}
}
打破
案例WM_油漆:
{
PAINTSTRUCT-ps;
HDC HDC=开始喷漆(hWnd和ps);
//TODO:在此处添加任何使用hdc的图形代码。。。
用于(自动和rc:vRect)
{
矩形(hdc,
左图,
rc.top,
对,
钢筋混凝土底层
);
}
端漆(hWnd和ps);
}
打破
案例WM_LBUTTONDOWN:
{
LeftButtonDown.x=LOWORD(lParam);//起点
LeftButtonDown.y=HIWORD(LPRAM);
isRubberBand=true;
ptCurrent.x=LOWORD(lParam);//当前点
ptCurrent.y=HIWORD(lpram);
DrawRubberBand(hWnd);//绘制矩形
}
打破
案例WM_LBUTTONUP://添加到向量
{
如果(!isRubberBand)
{
返回0;
}
isRubberBand=false;
//无效(hWnd,NULL,TRUE);
add.SetRect(LeftButtonDown.x,LeftButtonDown.y,ptCurrent.x,ptCurrent.y);//设置rect的坐标
vRect.push_back(add);//添加rect
更新窗口(hWnd);
}
打破
案例WM_MOUSEMOVE:
if(wParam&MK_LBUTTON)//绘制矩形
{
hdc=GetDC(hWnd);
如果(!isRubberBand)
{
打破
}
拉丝橡胶带(hWnd);
ptCurrent.x=低ORD(LPRAM);
ptCurrent.y=HIWORD(lpram);
拉丝橡胶带(hWnd);
}
if(wParam&mkrbutton)//拖动矩形
{
hdc=GetDC(hWnd);
现在的点=生成点(LPRAM);
SetROP2(hdc,R2_NOTXORPEN);
MoveFromTo(hdc、选定、锚定、现在);
无效(hWnd,NULL,TRUE);
锚点=制造点(LPRAM);
释放DC(hWnd、hdc);
}
打破
案例WM_RBUTTONDOWN:
{
intx{LOWORD(lParam)},y{HIWORD(lParam)};
用于(自动和rc:vRect)
{
if(单击(rc,x,y))
{
选择(hWnd,rc);
rect=rc;
锚点=制造点(LPRAM);
打破
}
否则{
取消选择(hWnd、rc);
}
}
//选择取消选择
}
打破
案例WM_RBUTTONUP:
打破
案例WM_销毁:
PostQuitMessage(0);
打破
违约:
返回DefWindowProc(hWnd、message、wParam、lParam);
}
返回0;
}
布尔被勾选(正确的r,整数x,整数y)
{
点pt{x,y};
返回(bool)PtInRect(&r,pt);
}
无效选择(HWND HWND,正确r){
HDC HDC=GetDC(hWnd);
HPEN selectPen=CreatePen(PS_点,0,RGB(255,0,0));
选择对象(hdc,选择笔);
矩形(hdc,右左,右上,右右下);
删除对象(选择笔);
}
无效取消选择(HWND HWND,更正r){
HDC HDC=GetDC(hWnd);
矩形(hdc,右左,右上,右右下);
}
空心拉丝橡胶带(HWND HWND){
HDC HDC=GetDC(hWnd);
SetROP2(hdc,R2_非);
选择对象(hdc、GetStockObject(NULL_笔刷));
矩形(hdc,
LeftButtonDown.x,
LeftButtonDown.y,
ptCurrent.x,
电流
);
释放DC(hWnd、hdc);
}
void MoveFromTo(HDC HDC,选择正确的矩形,点定位,点现在)
{
矩形(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
矩形(hdc、rectSelected.left、rectSelected.top、rectSelected.right、rectSelected.bottom);
}

之所以按降序选择所有矩形,是因为在
WM_RBUTTONDOWN
中找到了所选矩形,并且循环被中断,导致随后不执行矩形的取消选择

无法拖动矩形的原因有几个:

  • MoveFromTo
    函数应传递
    rectSelected
    的引用,否则函数中修改的矩形坐标无法应用于实际矩形

  • 您需要在拖动之前找到矩形,将其从
    vRect
    中删除,然后将拖动的矩形添加到
    vRect
    ,以确保在
    WM\u 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);
    }
    
    指向LeftButtonDown;
    点电流;
    点击点;
    矢量vRect;
    bool-isRubberBand=false;
    bool IsClicked(正确的r,int x,int y);
    无效选择(HWND HWND,正确r);
    无效取消选择(HWND HWND,正确r);
    空心橡胶带(HWND HWND);
    void MoveFromTo(HDC HDC,选择正确和正确,点定位,点现在);
    LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
    {
    正确添加