一:响应WM_PAINT消息的OnPaint()函数中,只能处理一种设备描述表对象。
如:
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.MoveTo(rect.left,rect.top);
dc.LineTo(rect.right,rect.bottom);
dc.MoveTo(rect.right,rect.top);
dc.LineTo(rect.left,rect.bottom); //图画到这里为止
PAINTSTRUCT ps;
CDC* pDC=BeginPaint(&ps);
pDC->Draw3dRect(CRect(0,0,100,100),RGB(255,0,0),RGB(0,0,255));
EndPaint(&ps);
}
void CMainWindow::OnPaint()
{
PAINTSTRUCT ps;
CDC* pDC=BeginPaint(&ps);
pDC->Draw3dRect(CRect(0,0,100,100),RGB(255,0,0),RGB(0,0,255));
EndPaint(&ps); //图画到这里为止
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.MoveTo(rect.left,rect.top);
dc.LineTo(rect.right,rect.bottom);
dc.MoveTo(rect.right,rect.top);
dc.LineTo(rect.left,rect.bottom);
}
二:
在对于处理WM_PAINT消息的OnPaint()函数中,有且仅有下方法可以参数设备描述表实例对象:
方法一:
---------------------------------------------------------
PAINTSTRUCT ps;
CDC* pDC=BeginPaint(&ps);
画图...........
EndPaint(&ps);
---------------------------------------------------------
方法二:
--------------------------------------------------------
CPaintDC dc(this);
画图
--------------------------------------------------------
方法三:
------------------------------------------------------
CWnd::BeginPaint()
CWnd::EndPaint()
-------------------------------------------------------
在其他地方可以使用的方法有:
方法一:
--------------------------------------------------------
CDC* pDC=GetDC();
Do some drawing.......
ReleaseDC(pDC);
-------------------------------------------------------
方法二:
-------------------------------------------------------
CWnd::GetDC()
CWnd::ReleaseDC()
-------------------------------------------------------
它是对GetDC和ReleaseDC的封装。
-----------------------------------------------------
GetWindowDC();
ReleaseDC();
-----------------------------------------------------
设备描述表类:
-----------------------------------------------------
CPaintDC
CClientDC
CWindowDC
CMetaFileDC
-----------------------------------------------------
最好在栈上构造DC实例:
如:CClientDC dc(this);析构函数会自动释放DC
若在堆上创建DC实例:
如:CClientDC pDC=new CClientDC(this);则应该在画图后删除DC,释放内存。即delete pDC。
三:
在vs中新插入了资源,然后在程序中引用资源ID,却出现错误:error C2065: “IDI_ICON1”: 未声明的标识符
最终发现出错的原因是没有在程序中引用资源文件resouce.h,解决方法是在程序头添加#include "resource.h"