一、画矩形框
在视图(view)类里面的重绘(ondraw)添加
win32版:
HDC dc = ::GetDC(this->m_hWnd);
::Rectangle(dc,0,0,100,100);
::ReleaseDC(this->m_hWnd,dc);
MFC版:
CDC *pDC = this->GetDC();
pDC->Rectangle(0,0,100,100);
this->ReleaseDC(pDC);
封装版
CClientDC dc(this);
dc.Rectangle(0,0,100,100);
类CClientDC派生于CDC,在构造时调用了Windows函数GetDC,在析构时调用了ReleaseDC。这意味着和CClientDC对象相关的设备上下文是窗口的客户区。
二、画直线
在view里面添加鼠标左键按下和抬起的消息
在头文件里面定义鼠标按下的点:
public:
CPoint PointMouseDown;
在鼠标按下消息里面给按下的点赋值:
PointMouseDown = point;
在鼠标抬起消息里面画线:
CDC *pDC = this->GetDC();
pDC->MoveTo(PointMouseDown);
pDC