Get flicker free animation in MFC/GDI+

Why still flicker in my animation(GDI+,MFC)?
Many articles talk about using double buffer to eliminate filker, but I found their methodes don’t work.Then I found even I draw one single line in CView::OnDraw method, the window flickers on mouse moving which causes CView::Invalidate(true).


Why flicker?
Flicker is NOT caused by slow painting, but background altering. In MFC, CView’s background erasing causes the flicker, no matter how fast the painting goes. If we:
BOOL CMyView::OnEraseBkgnd(CDC* pDC){ //Thanks, I’ll erase bkgnd myself. //return CView::OnEraseBkgnd(pDC); return TRUE;} then flicker disappears, but our window will in mess.
Indeed the background need erasing or, covering. We choose the second way by double buffer..
What can we benefit from double buffer?
MSDN says:
Creates a CachedBitmap object based on a Bitmap object and a Graphics object. The cached bitmap takes the pixel data from the Bitmap object and stores it in a format that is optimized for the display device associated with the Graphics object.You can display a cached bitmap by passing the address of a CachedBitmap object to the Graphics::DrawCachedBitmap method of a Graphics object. Use the Graphics object that was passed to the CachedBitmap constructor or another Graphics object that represents the same device.
So, double buffer make “block drawing” immediately, fine, that’s what we want:we swap two frame quickly enough, because the two frame differs little, there’s no flicker.
Conclusion
Double buffer without background erasing, we can get flicker free but slow animation.
P.S.
Use ::new/::delete instead of new/delete operators with GDI+ class, otherwise comes “error C2660: ‘Gdiplus::GdiplusBase::operator new’ : function does not take 3 arguments”.(VC2005)

博主友情提示:

如在您的评论中需要提及如QQ号、微信号、电子邮件地址或其他隐私敏感信息,欢迎通过>>博主专用加密工具v2<<加密后发布,原文只有博主可以看到。