C++ 在MFC单个文档中传递参数
我正在用MFC做一个小的绘图工具 首先,我创建一个对话框,从四个形状(矩形、直线、圆、椭圆)中选择一个形状,然后绘制它 其次,我创建了一个无模式对话框来显示形状的坐标(C++ 在MFC单个文档中传递参数,c++,mfc,C++,Mfc,我正在用MFC做一个小的绘图工具 首先,我创建一个对话框,从四个形状(矩形、直线、圆、椭圆)中选择一个形状,然后绘制它 其次,我创建了一个无模式对话框来显示形状的坐标(startpoint.x,startpoint.y,width,height) 坐标对话框如下所示: void CDrawToolView::OnLButtonUp(UINT nFlags, CPoint point) { m_startRect=FALSE; ::ClipCursor(NULL);
startpoint.x
,startpoint.y
,width
,height
)
坐标对话框如下所示:
void CDrawToolView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_startRect=FALSE;
::ClipCursor(NULL);
CClientDC dc(this);
dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(CRect(m_startPoint,m_OldPoint)); // draw rectangle
dc.Rectangle(CRect(m_startPoint,point));
}
void CPropertyDlg::OnBnClickedOk()
{
UpdateData();
CDrawToolView coordinate;
origin_x = coordinate.m_startPoint.x;
origin_y = coordinate.m_startPoint.y;
width = coordinate.m_OldPoint.x-coordinate.m_startPoint.x;
height = coordinate.m_OldPoint.y-coordinate.m_startPoint.y;;
OnOK();
}
最后,我创建了一个对话框来选择其他参数。单击“确定”按钮时,形状的坐标将传递到void CPropertyDlg::OnBnClickedOk()
。但是我发现所有的坐标都是零,是因为对话框和坐标是即时的吗?关闭对话框后,坐标将自动设置为零
在DrawToolView.cpp
中获取坐标的代码如下:
void CDrawToolView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_startRect=FALSE;
::ClipCursor(NULL);
CClientDC dc(this);
dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(CRect(m_startPoint,m_OldPoint)); // draw rectangle
dc.Rectangle(CRect(m_startPoint,point));
}
void CPropertyDlg::OnBnClickedOk()
{
UpdateData();
CDrawToolView coordinate;
origin_x = coordinate.m_startPoint.x;
origin_y = coordinate.m_startPoint.y;
width = coordinate.m_OldPoint.x-coordinate.m_startPoint.x;
height = coordinate.m_OldPoint.y-coordinate.m_startPoint.y;;
OnOK();
}
将坐标传递给void CPropertyDlg::OnBnClickedOk()
的代码,如下所示:
void CDrawToolView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_startRect=FALSE;
::ClipCursor(NULL);
CClientDC dc(this);
dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(CRect(m_startPoint,m_OldPoint)); // draw rectangle
dc.Rectangle(CRect(m_startPoint,point));
}
void CPropertyDlg::OnBnClickedOk()
{
UpdateData();
CDrawToolView coordinate;
origin_x = coordinate.m_startPoint.x;
origin_y = coordinate.m_startPoint.y;
width = coordinate.m_OldPoint.x-coordinate.m_startPoint.x;
height = coordinate.m_OldPoint.y-coordinate.m_startPoint.y;;
OnOK();
}
有人能帮我吗?在从
CDialog
或CDialogEx>创建的对话框中,您通常会声明连接到对话框中控件的成员变量——请参阅MSDN文章
调用UpdateData()
后,已连接控件的值将在对话框成员变量中可用。在调用函数中,您可以执行以下操作
CPropertyDlg dlg;
dlg.m_origin_x = m_startPoint.x;
dlg.m_origin_y = m_startPoint.y;
dlg.m_width = coordinate.m_OldPoint.x-coordinate.m_startPoint.x;
dlg.m_height = coordinate.m_OldPoint.y-coordinate.m_startPoint.y;
if (dlg.DoModal == IDOK)
{ m_startPoint.x = dlg.m_origin_x;
m_startPoint.y = dlg.m_origin_y;
coordinate.m_OldPoint.x = m_startPoint.x + dlg.m_width;
coordinate.m_OldPoint.y = m_startPoint.y + dlg.m_height;
// take action
}