1、在CxxxView类:OnPaint中添加如下代码,主要作用是利用缓存一次性完成绘图(现在缓存DC中绘制,然后再拷贝),其中OnDrawMem为另外添加的成员函数
void CChessView::OnDraw(CDC* pDC)
{
CChessDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
CRect rc;
this->GetClientRect(&rc);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC,rc.Width(),rc.Height());
CBitmap* pOldBmp = (CBitmap*)memDC.SelectObject(&bmp);
OnDrawMem(&memDC);
pDC->BitBlt(0,0,rc.Width(),rc.Height(),&memDC,0,0,SRCCOPY);
memDC.SelectObject(pOldBmp);
bmp.DeleteObject();
memDC.DeleteDC();
}
2、OnDrawMem中添加的代码,主要作用添加背景位图
void CChessView::OnDrawMem(CDC* pDC)
{
CRect rc;
this->GetClientRect(&rc);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.LoadBitmapA(IDB_BITMAP1);
BITMAP b;
bmp.GetBitmap(&b);
CBitmap* pOldBmp = (CBitmap*)memDC.SelectObject(&bmp);
pDC->StretchBlt(0,0,rc.Width(),rc.Height(),&memDC,0,0,b.bmWidth,b.bmHeight,SRCCOPY);
memDC.SelectObject(pOldBmp);
bmp.DeleteObject();
memDC.DeleteDC();
//此处添加GDI绘图代码
}
3、另外一种添加背景位图的方法:
在CxxxView类中添加WM_ERASEBKGND消息响应函数:
BOOL CTypeGameView::OnEraseBkgnd(CDC* pDC)
{
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap bmp;
bmp.LoadBitmapA(IDB_BITMAP1);
BITMAP b;
bmp.GetBitmap(&b);
CBitmap* pOldBitmap = (CBitmap*)memDC.SelectObject(&bmp);
CRect rc;
this->GetClientRect(&rc);
pDC->StretchBlt(0,0,rc.Width(),rc.Height(),&memDC,0,0,b.bmWidth,b.bmHeight,SRCCOPY);
memDC.SelectObject(pOldBitmap);
bmp.DeleteObject();
memDC.DeleteDC();
return FALSE;//CView::OnEraseBkgnd(pDC);
}