1,首先在OnLButtonDown()中保存第一个点:
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(Start==true) //开始标志
{
CClientDC ClientDC(this);
OnPrepareDC(&ClientDC);
ClientDC.DPtoLP(&point);
PointA=point; //第一个点.
OldPoint=point; //鼠标移动过程中的上一个点.
First=true;
}
CView::OnLButtonDown(nFlags, point);
}
2.其次,在void OnMouseMove(UINT nFlags, CPoint point) 中加入以下画图分支结构
void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(First&&Start){
CClientDC ClientDC(this);//设备描述表
OnPrepareDC(&ClientDC);
ClientDC.DPtoLP(&point);
if(DrawStartWhat==0)
{
ClientDC.SetROP2(R2_NOTXORPEN);//生成逆转当前屏幕颜色来画线的绘图方式以擦去之前画的线
ClientDC.MoveTo(PointA);//画反颜色线,就擦去了原来的颜色
ClientDC.LineTo(OldPoint);
ClientDC.MoveTo(PointA); //画新的线,以下同理.
ClientDC.LineTo(point);
}
else if(DrawStartWhat==1)
{
//love you! 很多人用的是R2_NOT,但是自我感这儿用R2_NOTXORPEN较好.
ClientDC.SetROP2(R2_NOTXORPEN);//生成逆转当前屏幕颜色来画线的绘图方式以擦去之前画的圆,
OldPoint.y=OldPoint.x-PointA.x+PointA.y;//画圆的坐标设定,注意它的斜率与PointA.y/PointA.x相同.
ClientDC.Ellipse(CRect(PointA,OldPoint));
OldPoint.x=point.x;
OldPoint.y=point.x-PointA.x+PointA.y;
ClientDC.Ellipse(CRect(PointA,OldPoint));
}
else if(DrawStartWhat==2)
{
ClientDC.SetROP2(R2_NOTXORPEN);//生成逆转当前屏幕颜色来画线的绘图方式以擦去之前画的椭圆
ClientDC.Ellipse(CRect(PointA,OldPoint));
ClientDC.Ellipse(CRect(PointA,point));
}
else if(DrawStartWhat==3)
{
ClientDC.SetROP2(R2_NOTXORPEN);//生成逆转当前屏幕颜色来画线的绘图方式以擦去之前画的矩形
ClientDC.Rectangle(CRect(PointA,OldPoint));
ClientDC.Rectangle(CRect(PointA,point));
}
OldPoint=point;
CView::OnMouseMove(nFlags, point);
}
3.最好在OnLButtonUP()中,回到初值,即可>
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(First)
{
First=false;
}
CView::OnLButtonUp(nFlags, point);
}