1.在View类的头文件里面定义一个 CBitmap *_pBufferBitmap和函数void PrepareBufferBitmap(CDC *pDC)
protected:
CBitmap *_pBufferBitmap;
protected:
void PrepareBufferBitmap(CDC *pDC);
CBitmap *_pBufferBitmap;
protected:
void PrepareBufferBitmap(CDC *pDC);
2.在View类的构造函数中将_pBufferBitmap初始化为NULL
3.实现PrepareBufferBitmap函数
void C***View::PrepareBufferBitmap(CDC *pDC)
{
CRect clientRect;
GetClientRect(&clientRect);
if (_pBufferBitmap != NULL)
{
BITMAP bitmap;
_pBufferBitmap->GetBitmap(&bitmap);
if (bitmap.bmWidth == clientRect.Width() && bitmap.bmHeight == clientRect.Height()) return;
delete _pBufferBitmap;
}
_pBufferBitmap = new CBitmap();
_pBufferBitmap->CreateCompatibleBitmap(pDC, clientRect.Width(), clientRect.Height());
}
{
CRect clientRect;
GetClientRect(&clientRect);
if (_pBufferBitmap != NULL)
{
BITMAP bitmap;
_pBufferBitmap->GetBitmap(&bitmap);
if (bitmap.bmWidth == clientRect.Width() && bitmap.bmHeight == clientRect.Height()) return;
delete _pBufferBitmap;
}
_pBufferBitmap = new CBitmap();
_pBufferBitmap->CreateCompatibleBitmap(pDC, clientRect.Width(), clientRect.Height());
}
4.在OnDraw函数里实现下面代码
PrepareBufferBitmap(pDC);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
ASSERT(_pBufferBitmap != NULL);
CBitmap *pOldBitmap = memDC.SelectObject(_pBufferBitmap);
Display(&memDC);
CRect clientRect;
GetClientRect(&clientRect);
pDC->BitBlt(0, 0, clientRect.Width(), clientRect.Height(), &memDC, 0, 0, SRCCOPY);
memDC.SelectObject(pOldBitmap);
memDC.DeleteDC();
CDC memDC;
memDC.CreateCompatibleDC(pDC);
ASSERT(_pBufferBitmap != NULL);
CBitmap *pOldBitmap = memDC.SelectObject(_pBufferBitmap);
Display(&memDC);
CRect clientRect;
GetClientRect(&clientRect);
pDC->BitBlt(0, 0, clientRect.Width(), clientRect.Height(), &memDC, 0, 0, SRCCOPY);
memDC.SelectObject(pOldBitmap);
memDC.DeleteDC();
其中Display是一个你自定义的画图函数,将你所有的画图动作东在Display函数中实现