双缓冲技术的一个冲突,请大家帮忙
最近在做一个小游戏,为了避免闪烁,我在OnEraseBkgnd()和OnDraw()都使用了双缓冲技术,这两者同时使用的话会引起很大的闪烁,单独使用不会,有没有什么解决方案?下面是源码:
-----OnDraw(CDC *pDC)-------
void GameView::OnDraw(CDC* pDC)
{
//ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rc;
GetClientRect(&rc);
CBitmap* m_pOldBitmap;
CBitmap m_pMemBitmap; //声明内存中承载临时图象的位图
CDC m_pMemDC; //声明用于缓冲作图的内存m_pMemDC
m_pMemDC.CreateCompatibleDC(pDC);
m_pMemBitmap.CreateCompatibleBitmap(pDC, rc.Width(), rc.Height());
m_pOldBitmap = m_pMemDC.SelectObject(&m_pMemBitmap);
CBrush brush1;
brush1.CreateSolidBrush(RGB(255,255,255));
m_pMemDC.FillRect(rc,&brush1);
………………
pDC->BitBlt(0,0,rc.Width(),rc.Height(),&m_pMemDC,0,0,SRCCOPY); m_pMemDC.SelectObject(m_pOldBitmap);
m_pMemBitmap.DeleteObject();
m_pMemDC.DeleteDC();
}
-----------OnEraseBkgnd------------
BOOL GameView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
int nWidth;
int nHeight;
CRect rect;
GetWindowRect(&rect);
nWidth = rect.Width();
nHeight= rect.Height();
CDC MemDC;
CBitmap MemBitmap;
MemDC.CreateCompatibleDC (pDC);
MemBitmap.CreateCompatibleBitmap(pDC,nWidth,nHeight);
CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
MemDC.FillSolidRect(0,0,nWidth,nHeight,RGB(255,255,255));
GetClientRect(&rect);
BITMAP bm;
CDC dcMem;
//game.back是Cbitmap对象
VERIFY(game.back.GetObject(sizeof(bm),(LPVOID)&bm));
dcMem.CreateCompatibleDC(pDC);
CBitmap *pOldBMP =( CBitmap *)dcMem.SelectObject(&game.back);
MemDC.BitBlt( 0,0,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
dcMem.SelectObject(pOldBMP);
pDC->BitBlt(0,0,nWidth,nHeight,&MemDC,0,0,SRCCOPY);
MemBitmap.DeleteObject();
MemDC.DeleteDC();
return true;//CView::OnEraseBkgnd(pm_pMemDC);;
}