Double Buffered Graphics

       Flicker is a common problem when programming graphics. Graphics operations that require multiple complex painting operations can cause the rendered images to appear to flicker or have an otherwise unacceptable appearance. To address these problems, the .NET Framework provides access to double buffering.

     Double buffering uses a memory buffer to address the flicker problems associated with multiple paint operations. When double buffering is enabled, all paint operations are first rendered to a memory buffer instead of the drawing surface on the screen. After all paint operations are completed, the memory buffer is copied directly to the drawing surface associated with it. Because only one graphics operation is performed on the screen, the image flickering associated with complex painting operations is eliminated.

Double Buffered Graphics_第1张图片

我的代码:
void CTestView::DoubleBuffering(CDC* pDC)
{
CRect rect;
GetClientRect(&rect);
pDC->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowExt(rect.Width(),rect.Height());
pDC->SetViewportExt(rect.Width(),-rect.Height());
pDC->SetViewportOrg(rect.Width()/2,rect.Height()/2);
CDC MemDC;//内存DC
CBitmap NewBitmap,*pOldBitmap;//内存中承载的临时位图
MemDC.CreateCompatibleDC(pDC);//建立与屏幕pDC兼容的MemDC 
NewBitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());//创建兼容位图 
pOldBitmap=MemDC.SelectObject(&NewBitmap);//将兼容位图选入MemDC 
MemDC.FillSolidRect(rect,pDC->GetBkColor());//按原来背景填充客户区,否则是黑色
MemDC.SetMapMode(MM_ANISOTROPIC);//MemDC自定义坐标系
MemDC.SetWindowExt(rect.Width(),rect.Height());
MemDC.SetViewportExt(rect.Width(),-rect.Height());
MemDC.SetViewportOrg(rect.Width()/2,rect.Height()/2);
DrawObject(&MemDC);
pDC->BitBlt(-rect.Width()/2,-rect.Height()/2,rect.Width(),rect.Height(),&MemDC,-rect.Width()/2,- 
                          rect.Height()/2,SRCCOPY);//将内存位图拷贝到屏幕
MemDC.SelectObject(pOldBitmap);
NewBitmap.DeleteObject();
}  

你可能感兴趣的:(Double Buffered Graphics)