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.


我的代码:
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();
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值