拖放功能在我的图形-基于对话框的MFC
Drag and Drop functionality in my graphic - Dialog Based MFC
我有一个基于对话框的MFC应用程序,它从文本文件中读取高度和半径的坐标,并将其显示为图片控制窗口上的点的绘图。现在,在绘制点之后,我需要能够将点拖放到窗口中的任何特定位置,以便我将点的坐标更改到新的位置。所有这些都应该通过鼠标右键拖放来完成。我确实明白,我应该使用的事件将是OnRButtonDown()和OnRButtonUp(),但我无法理解如何在我的应用程序中包括拖放功能。供您参考,我已经完成了点的绘制,我只需要了解拖放功能的实现。
关于拖拽&下降:
- 在OnRButtonDown()中,你需要确定你要拾取的点,将RButtonDown标志设置为true。
- 检查标志,如果为真,在OnMouseMove()中根据点的新位置动态绘制绘图,以使其尽可能平滑(不闪烁),不要使所有无效,而是使无效并重新绘制特定区域。 在OnRButtonUp()中,将标志更新为false。
你可能还需要使用SetCapture/releaseccapture在你的OnRButtonDown()/OnRButtonUp()的情况下,你拖动和移动鼠标出你的对话框窗口。
您需要从CWnd
或CStatic
继承并自己进行绘画。当拖动完成时,你需要自己移动绘图对象。与设备上下文(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);
}
…
我已经运行了这段代码,它工作得非常好。这段代码中的变量与我在程序中使用的变量有关。如果你有不明白的地方,尽管问我。
相关文章:
- 如何在 MFC 对话框中使用 OnDraw
- "最小化"按钮不会出现在 MFC 对话框中
- MFC:与对话框的视图通信
- 在网格视图中显示数据 - 基于 MFC c++ 对话框的应用程序
- 对话框 MFC 资源度量的单位是什么
- MFC 对话框属性表用法在 VS 2017 中产生错误,适用于 VS 2013
- 如何在MFC对话框应用程序中默认将复选框设置为选中?
- 基于 MFC 对话框的应用程序无法调用对话框两次
- 如何在另一个线程中关闭 MFC 模式对话框并获取对话框返回值?
- 是否可以将 MFC 对话框资源附加到 CChildView
- MFC 对话框控件的"Accept Files"选项如何工作?
- 从模式对话框中退出 MFC 应用
- MFC在切换视图时如何关闭无模型对话框
- 使用MFC在对话框上拖放文件
- 添加新对话框后,MFC 应用程序启动失败并"DLL Initialization Failed"
- 如何将.wav文件保存在MFC中(使用文件对话框)
- MFC对话框数据验证失败时还原数据
- 如何隐藏对话框 MFC
- 选项卡控件中的嵌入式对话框无法在第二个对话框 MFC 中工作
- 更改对话框mfc的背景颜色