Reputation: 173
I am doing a small drawing tool with MFC.
When button down capture the original point, when button up capture the new point, and then draw a line from the original point to the new point.
I have already created a dialog. But I don't know how to display both the original point and the new point on it while button up.
Code of drawing line and showing dialog as below:
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CDC *pDC = GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);
CArgDlg object; // Jump out a dialog
object.DoModal();
CView::OnLButtonUp(nFlags, point);
}
Can some one help me?
Upvotes: 0
Views: 391
Reputation: 5142
Move the drawing code from the button handlers out to OnDraw().
I assume you want to just display the values of the two points in the dialog?
Declare two member variables m_pt1
and m_pt2
in the dialog class and fill your static/edit controls from these values in OnInitDialog()
.
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{ m_ptEnd = point; // new member variable
CRect rc(m_ptOrigin, m_ptEnd);
InvalidateRect(&rc); // will invoke OnDraw()
CView::OnLButtonUp(nFlags, point);
CArgDlg object; // Jump out a dialog
object.m_pt1 = ptOrigin;
object.m_pt2 = m_ptEnd;
object.DoModal();
}
Upvotes: 2
Reputation: 17444
Override OnDraw(), don't start drawing inside button handlers. The point is that the underlying win32 framework keeps track of when and what needs to be drawn and you draw it when it asks you to draw (i.e. in OnDraw()).
BTW: I'm not sure what you want to achieve with the dialog, because you are at the moment drawing the line on the view that contains the button handlers, not in the dialog.
Upvotes: 0