在孙鑫《vc++深入详解》中主要讲述了以下几种绘图方法,我稍稍总结了一下。
1.在MFC中调用API函数实现绘图:
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{HDC hdc;
hdc=::GetDC(m_hWnd);
//m_hWnd:是CWnd类的一个成员变量,保存了窗口句柄
//GetDC():通过调用该全局函数获得当前窗口句柄
MoveToEx(hdc,10,10,NULL);LineTo(hdc,100,100);
::ReleaseDC(m_hWnd,hdc);
//此处GetDC()函数和Release()函数均为全局函数
CView::OnLButtonDown(nFlags, point);
}
2.通过调用MFC类库函数绘图:
(1).void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
CDC *pDC=GetDC();
//获得当前窗口设备描述表对象指针
pDC->TextOut(100,100,"这样也可以!!");
ReleaseDC(pDC);
//GetDC()函数和Release()函数均为CWnd类的类成员函数
CView::OnLButtonDown(nFlags, point);
}
(2).void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
//CClientDC 类派生于CDC类,CDC类在定义时调用了GetDC函数,在析构时调用了ReleaseDC函数
dc.MoveTo(100,100);dc.LineTo(200,200);
CView::OnLButtonDown(nFlags, point);
}
(3).void CMy3View::OnLButtonDown(UINT nFlags, CPoint point)
{
CWindowDC dc(GetParent());
//CWindowDc类也派生于CDC类
//该类可以访问整个窗口区域,但必须调用GetParent()函数获取指向父类窗口的指针
dc.MoveTo(0,0);
dc.LineTo(point);
CView::OnLButtonDown(nFlags, point);
}