(一) HDC hdc;
hdc=::GetDC(m_hWnd); //从CWin派生的类均有一个与该窗口相关的句柄m_hWnd
MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);
LineTo(hdc,point.x,point.y);
::ReleaseDC(m_hWnd,hdc);
(二) CDC *pDC=GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);
(三) CClientDC dc(this); //View类窗口位画线区
//CClientDC dc(GetParent()); //窗口的画线区域为父窗口
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
(四) CWindowDC dc(this); //可以访问整个屏幕区域,包括客户区和非客户区
//CWindowDC dc(GetParent());
//CWindowDC dc(GetDesktopWindow()); //画线区域为整个Windows桌面区域
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
CPen pen(PS_SOLID,1,RGB(0,255,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);
bitmap.LoadBitmap(IDB_BITMAP1);
CBrush brush(&bitmap);
CClientDC dc(this);
dc.FillRect(CRect(m_ptOrigin,point),&brush);
//该方法画出的矩形,后面的矩形会覆盖前面的矩形
CClientDC dc(this);
dc.Rectangle(m_ptOrigin,point); //参数为矩形的左上角和右下角
CClientDC dc(this);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush=dc.SelectObject(pBrush);
dc.Rectangle(CRect(m_ptOrigin,point));
dc.SelectObject(pOldBrush);
首先添加OnMouseMove事件,在该事件的相应函数中实现。先定义一个变量BOOL m_bDraw。因为画图
CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(0,255,0)); //创建绿色的画笔
CPen *pOldPen=dc.SelectObject(&pen);
if(m_bDraw==TRUE)
{
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
m_ptOrigin=point; 更改原始点,以便连续作图
}
dc.SelectObject(pOldPen);