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

时间:2011-08-12 09:01:47

标签: c++ visual-c++ mfc

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

提前致谢。

3 个答案:

答案 0 :(得分:2)

您需要继承自CWndCStatic并自行完成绘画。然后在完成拖动时,您需要自己移动绘图对象。使用设备上下文(CDCCClientDC)将会出现。您需要使用CDC::SetROP2和其他方法绘制图形对象。

答案 1 :(得分:2)

拖拉的几件事情下降:

  1. 在OnRButtonDown()中,您需要确定要拾取的点,将RButtonDown标志设置为true。
  2. 检查标志,如果为true,则发布绘制消息以根据OnMouseMove()中点的新位置动态绘制绘图,使其尽可能平滑(不闪烁),不要使除了无效和重绘之外的所有内容无效某个地区。
  3. 在OnRButtonUp()中,将标志更新为false。
  4. 对于拖动鼠标并将其移出对话框窗口的情况,您可能还需要在OnRButtonDown()/ OnRButtonUp()中使用SetCapture / ReleaseCapture

答案 2 :(得分:1)

我已经想出如何让这个工作。因此,如果人们想知道如何在他们的程序中实现这一点,可以从这段代码中获得一个想法。

代码:

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);
}

...

我运行了这段代码,效果非常好。这段代码中的变量与我在程序中使用的变量有关。万一你不明白,请随时问我。

相关问题