1>在CDrawView类中增加2个消息响应函数void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) 与void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
再增加1个成员对象用来保存点CPoint m_ptOrigin;
/////////////////////////////////////////////////////////////////////////////
// CDrawView message handlers
//鼠标按下时调用如下方法
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//MessageBox("view clicked");
m_ptOrigin = point;
CView::OnLButtonDown(nFlags, point);
}
//弹起时调用如下方法
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
HDC hdc;
hdc = ::GetDC(m_hWnd);
MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);
LineTo(hdc,point.x,point.y);
::ReleaseDC(m_hWnd,hdc);
CView::OnLButtonUp(nFlags, point);
}
2>按下的方法不变,在弹起的方法中重新实现也可实现划线
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC = GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);
CView::OnLButtonUp(nFlags,point);
}
3>按下的方法不变,在弹起的方法中重新实现也可实现划线
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CClientDC dc(GetParent());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
}
4>按下的方法不变,在弹起的方法中重新实现也可实现划线
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//CWindowDC dc(this);
CWindowDC dc(GetParent());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
}