0
votes

I am doing a small drawing tool with MFC.

Firstly, I create a dialog to select one shape from four shapes(rectangle, line, circle, ellipse), and draw it.

Second, I create a modeless dialog to display the coordinates of the shape(startpoint.x, startpoint.y, width, height).

Coordinate dialog as below:

enter image description here

Finally, I create a dialog to choose other parameters. When clicking OK button, coordinates of the shape will pass to void CPropertyDlg::OnBnClickedOk(). But I found all the coordinates are zero, is that because the dialog and coordinates are instant? Once closing the dialog, the coordinates are set zero automatically?

Code to get coordinates in DrawToolView.cpp as below:

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

Code to pass coordinates to void CPropertyDlg::OnBnClickedOk() as below:

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

Could some one help me ?

1

1 Answers

1
votes

In a dialog dervived from CDialog or CDialogEx`, you would normally declare member variables which are connected to the controls in the dialog box -- see MSDN article Dialog Data Exchange.

Once you call UpdateData(), the values from the connected controls are available in the dialog member variables. In your calling function, you would do something like

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
}