拖放功能在我的图形-基于对话框的MFC

Drag and Drop functionality in my graphic - Dialog Based MFC

本文关键字:对话框 MFC 功能 我的 图形 拖放      更新时间:2023-10-16

我有一个基于对话框的MFC应用程序,它从文本文件中读取高度和半径的坐标,并将其显示为图片控制窗口上的点的绘图。现在,在绘制点之后,我需要能够将点拖放到窗口中的任何特定位置,以便我将点的坐标更改到新的位置。所有这些都应该通过鼠标右键拖放来完成。我确实明白,我应该使用的事件将是OnRButtonDown()和OnRButtonUp(),但我无法理解如何在我的应用程序中包括拖放功能。供您参考,我已经完成了点的绘制,我只需要了解拖放功能的实现。

关于拖拽&下降:

    在OnRButtonDown()中,你需要确定你要拾取的点,将RButtonDown标志设置为true。
  1. 检查标志,如果为真,在OnMouseMove()中根据点的新位置动态绘制绘图,以使其尽可能平滑(不闪烁),不要使所有无效,而是使无效并重新绘制特定区域。
  2. 在OnRButtonUp()中,将标志更新为false。

你可能还需要使用SetCapture/releaseccapture在你的OnRButtonDown()/OnRButtonUp()的情况下,你拖动和移动鼠标出你的对话框窗口。

您需要从CWndCStatic继承并自己进行绘画。当拖动完成时,你需要自己移动绘图对象。与设备上下文(CDC, CClientDC)一起工作将进入画面。您需要使用CDC::SetROP2和其他方法来绘制图形对象

我已经弄清楚如何得到这个工作。所以,如果人们想知道如何在他们的程序中实现它,可以从这段代码中得到一个想法。

代码:

void CRangemasterGeneratorDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    GetCursorPos(&point);
    int mx = point.x;
    int my = point.y;
    float cursR, cursH;
    cursR = (mx - 312) / 7.2;// records the current cursor's radius(x) position
    cursH = (641 - my) / 5.3;// records the current cursor's height(y) position
    CString Hgt,Rds;
    Hgt.Format("%.3f",cursH);// Rounding off Height values to 3 decimal places
    Rds.Format("%.3f",cursR);// Rounding off Radius values to 3 decimal places
    curR = (float)atof(Rds);
    curH = (float)atof(Hgt);
    // I had limits on my grid from 0 - 100 on both x and y-axis
        if(curR < 0 || curR >100 || curH < 0 || curH > 100)  
        return;
    SetCapture();
    SetCursor(::LoadCursor(NULL, IDC_CROSS));
    //snap the point, compare the point with your array and save position on 'y'
    for(int i=0; i < 100; i++)
    {
      if(curH < m_Points[i+1].m_height_point && curH >m_Points[i-1].m_height_point)
        {
            curH = m_Points[i].m_height_point;
            curR = m_Points[i].m_radius_point;
            y = i;
        }
    }
    CDialog::OnRButtonDown(nFlags, point);
    UpdateData(false);
    Invalidate();
}
void CRangemasterGeneratorDlg::OnRButtonUp(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    ReleaseCapture();
    GetCursorPos(&point);
    int mx1 = point.x;
    int my1 = point.y;
    float curR1,curH1;
    curR1 = (mx1 - 312) / 7.2;// records the current cursor's radius(x) position
    curH1 = (641 - my1) / 5.3;// records the current cursor's height(y) position
    m_Points[y].m_radius_point = curR1;
    m_Points[y].m_height_point = curH1;
    Invalidate();
    CDialog::OnRButtonUp(nFlags, point);
    UpdateData(false);
}

我已经运行了这段代码,它工作得非常好。这段代码中的变量与我在程序中使用的变量有关。如果你有不明白的地方,尽管问我。