用dc画图时,总会出现闪烁,出现闪烁的原因是绘制背景和绘制图形之间绘制次数的比例的缘故,一般用来防止闪烁的方法,可以采用双缓存的方法,先将需要绘制的图象绘制到兼容DC上,然后,在将兼容DC上的东西帖到屏幕DC上,当然,要在绘制背景的时候,进行绘制,要不然也会和闪烁的,事例代码如下:
BOOL CMemDCView::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default int nWidth; int nHeight; //CView::OnEraseBkgnd(pDC); CMemDCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CRect rect; GetWindowRect(&rect); nWidth = rect.Width(); nHeight= rect.Height(); CDC MemDC; CBitmap MemBitmap; MemDC.CreateCompatibleDC (NULL); 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; VERIFY(m_bmp.GetObject(sizeof(bm),(LPVOID)&bm)); dcMem.CreateCompatibleDC(NULL); CBitmap *pOldBMP =( CBitmap *)dcMem.SelectObject(&m_bmp);
MemDC.BitBlt( (rect.right - bm.bmWidth)/2, (rect.bottom - bm.bmHeight)/2, bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY); dcMem.SelectObject(pOldBMP); /// 以上为画背景图 // /// CBrush brush(RGB(255, 0, 0)); CBrush * defbrush = MemDC.SelectObject(&brush); MemDC.Rectangle(50, 50, 100, 100); //绘制图形 MemDC.SelectObject(defbrush);
pDC->BitBlt(0,0,nWidth,nHeight,&MemDC,0,0,SRCCOPY);
MemBitmap.DeleteObject(); MemDC.DeleteDC(); return TRUE; //pDC->Rectangle(0, 0, 200, 200); //return true;
return CView::OnEraseBkgnd(pDC); // return false; }
// 构造函数里面:VERIFY(m_bmp.LoadBitmap(IDB_BITMAP3)); CBitmap m_bmp;