建立项目,采用单文档结构
HDC:
首先在CYourClassView类中add windows message handler, 一个是LButtonDown, 另一个是LButtonUp. 添加成员变量:m_ptOrigin用来记录初始点位置。
在view结构体中初始化m_ptOrigin变量:
CYourClassView:: CYourClassView ()
{
// TODO: add construction code here
m_ptOrigin=0;
}
在LButtonDown函数里面添加代码:
void CYourClassView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptOrigin=point;
CView::OnLButtonDown(nFlags, point);
}
在LButtonUp中添加代码,首先使用HDC:
void CYourClassView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
HDC hdc;
hdc=::GetWindowDC(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);
}
使用CDC:
void CYourClassView::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);
}
使用CClientDC:
void CYourClassView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
CView::OnLButtonUp(nFlags, point);
}
使用CWindowDC:
void CYourClassView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CWindowDC dc(this);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
CView::OnLButtonUp(nFlags, point);
}