创建从CWnd派生的类CMyWnd;
在MyWnd.h中定义:
CDC m_MemDC;
CBitmap m_Bitmap;
CRect m_rtClient;
之后用类向导添加或者手动添加下面的消息响应函数:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
在MyWnd.cpp中添加
int CMyWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
GetClientRect(&m_rtClient);
CClientDC dc(this);
m_MemDC.CreateCompatibleDC(&dc);
m_MemDC.SetStretchBltMode(HALFTONE);
m_Bitmap.CreateCompatibleBitmap(&dc,m_rtClient.Width(),m_rtClient.Height());
m_MemDC.SelectObject(&m_Bitmap);
return 0;
}
void CMyWnd::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GetClientRect(&m_rtClient);
CClientDC dc(this);
if(m_MemDC.m_hDC)
{
if(m_Bitmap.m_hObject)
m_Bitmap.DeleteObject();
m_Bitmap.CreateCompatibleBitmap(&dc,m_rtClient.Width(),m_rtClient.Height());
m_MemDC.SelectObject(&m_Bitmap);
Invalidate();
}
}
BOOL CMyWnd::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
return CWnd::OnEraseBkgnd(pDC);
}
void CMyWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
//画具体的内容
dc.BitBlt(m_rtClient.left,
m_rtClient.top,
m_rtClient.Width(),
m_rtClient.Height(),
&m_MemDC,
m_rtClient.left,
m_rtClient.top,
SRCCOPY);
}