创建变量
...View.h 头文件
private:
CPoint m_ptOrigin;
int n_r;
在构造函数赋默认值
在...View.cpp添加
CTest4View::CTest4View()
{
// TODO: add construction code here
m_ptOrigin=0;
n_r=0;
}
消息函数
OnLButtonDown
void CTest4View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptOrigin =point;
n_r=1;
CView::OnLButtonDown(nFlags, point);
}
OnLButtonUp
void CTest4View::OnLButtonUp(UINT nFlags, CPoint point)
{
n_r=0;
CView::OnLButtonUp(nFlags, point);
}
OnMouseMove
void CTest4View::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(255,0,0));
CPen *pOldPen =dc.SelectObject(&pen);
if(n_r==1){
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
m_ptOrigin = point;
}
CView::OnMouseMove(nFlags, point);
}