一.画鼠标按下到弹起之间的直线
1.创建 CView 的私有成员变量 CPoint m_ptOrigin;
2.在构造函数中初始化 m_ptOrigin = 0;
3.添加 LButtonDown(鼠标左键按下)消息函数并在鼠标左键按下的事件中记录m_ptOrigin点的坐标;
4.添加 LButtonUp(鼠标左键弹起)消息函数并在鼠标左键弹起的事件中进行画线
/*HDC hdc;
hdc=::GetDC(m_hWnd); //HDC方式创建
MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL); //线的初始点
LineTo(hdc,point.x,point.y); //线的终点
::ReleaseDC(m_hWnd,hdc);*/ //释放dc
/*
CDC *pDC=GetDC(); //CDC方式创建
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);*/
/*
CClientDC dc(this); //ClientDC 方式创建
//CClientDC dc(GetParent());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
*/
/*
//CWindowDC dc(this); //CWindowDC 方式创建
//CWindowDC dc(GetParent());//父窗口
CWindowDC dc(GetDesktopWindow());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
*/
/*
CPen pen(PS_SOLID,1,RGB(255,0,0)); //定义画笔
CClientDC dc(this); //画板
CPen *pOldPen=dc.SelectObject(&pen);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
dc.SelectObject(pOldPen);*/
/* 用笔刷画矩形
CBrush brush(RGB(255,0,0));//笔刷颜色
CClientDC dc(this);
dc.FillRect(CRect(m_ptOrigin,point),&brush);
*/
/* 使用资源图填充笔刷画矩形
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);//载入图片资源
CBrush brush(&bitmap);
CClientDC dc(this);
dc.FillRect(CRect(m_ptOrigin,point),&brush);
*/
/* 画透明背景的矩形框
CClientDC dc(this);
//dc.Rectangle(CRect(m_ptOrigin,point));
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//透明笔刷
CBrush *pOldBrush=dc.SelectObject(pBrush);
dc.Rectangle(CRect(m_ptOrigin,point));
dc.SelectObject(pOldBrush);
*/
二.连续画线
1.在CView 中创建私有成员变量 BOOL m_bDraw;
2.在构造函数中初始化为 FALSE 即 m_bDraw = FALSE;
3.添加MouseMove(鼠标移动)事件响应函数,并添加
CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(255,0,0));
CPen *oldPen = dc.SelectObject(&pen);
if(m_bDraw)
{
//dc.SetROP2(R2_BLACK);
dc.MoveTo(m_ptOrigin);
dc.LineTo(m_ptOld);
dc.MoveTo(m_ptOld);
dc.LineTo(point);
//m_ptOrigin=point;
m_ptOld=point;
}
dc.SelectObject(oldPen);
画出来为扇形