如题所述
建立一个单文档的MFC 添加三个消息响应,WM_LButtonDown WM_LButtonUp WM_MouseMove
在消息响应函数里面添加代码如下即可:
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Origin_Point=point;
flag=1;
CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
flag=0;
CView::OnLButtonUp(nFlags, point);
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CPen pen(PS_SOLID,2,RGB(0,255,0));
CPen *oldpen=dc.SelectObject(&pen);
if(flag==1)
{
dc.MoveTo(Origin_Point);
dc.LineTo(point);
Origin_Point=point;
}
dc.SelectObject(oldpen);
CView::OnMouseMove(nFlags, point);
}
在消息响应函数里面添加代码如下即可:
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Origin_Point=point;
flag=1;
CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
flag=0;
CView::OnLButtonUp(nFlags, point);
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
CPen pen(PS_SOLID,2,RGB(0,255,0));
CPen *oldpen=dc.SelectObject(&pen);
if(flag==1)
{
dc.MoveTo(Origin_Point);
dc.LineTo(point);
Origin_Point=point;
}
dc.SelectObject(oldpen);
CView::OnMouseMove(nFlags, point);
}
温馨提示:答案为网友推荐,仅供参考