5,696,038 members and growing! (18,638 online)
Email Password   helpLost your password?
Multimedia » GDI » General     Intermediate

Do a flicker-free drawing using MFC methods

By lucy

How to achieve flicker-free drawing when using the methods MFC provides
VC6, C++Windows, Win2K, MFC, GDI, VS6, Visual Studio, Dev

Posted: 13 Jun 2002
Updated: 13 Jun 2002
Views: 67,290
Bookmarked: 21 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
31 votes for this Article.
Popularity: 4.63 Rating: 3.11 out of 5
1 vote, 6.3%
1
2 votes, 12.5%
2
0 votes, 0.0%
3
4 votes, 25.0%
4
9 votes, 56.3%
5

Introduction

I am quite new to VC++, and I found MFC support of drawing is quite easy to use at first. Just a few lines of code, and you can draw whatever basic shapes on the client area. But when it comes to flicker-free, I had a tough time to dig out the working sequence. Thanks to all CPians, especially Chris Losinger, Joaquín M López Muñoz, Tim Smith, Shog, Nish, Ernest Laurentin, Kemal OZLU, Ravi Bhavnani, vaithsr, for helping me out in my painful days. And I'd like to share what I have learnt so far with other newbies.

Three messages you need to handle

  • First of all, in order to get flicker-free drawing, you need to draw everything on a memory DC, then BitBlt it to the real DC. This is called double buffering, and it is addressed in many books and articles.
  • Secondly, you need to overwrite OnEraseBkgnd to really get rid of flickering.
  • Last but not least, you still want the background to be erased when WM_ERASEBKGND is sent by other event than your InvalidateRect( &rect_yourDrawArea ). So you need to overwrite the handler of WM_NCPAINT, to clear the flag of bypassing OnEraseBkgnd, so that the background will be paint properly.

Some tricks

Some tricks to achieve flicker-free while avoiding side effects are:

  1. When draw your picture on the memory DC, you can assume a rectangle, whose left top corner is at (0,0). Then when you BitBlt it to the real DC, you can specify the real left top corner by x and y argument. In this way, you can move your drawing area easily in the future, since the only place needs to be changed is BitBlt's x and y argument.
  2. Whenever you are updating your drawing by calling InvalidateRect(&rect_yourDrawArea), set the flag so that OnEraseBkgnd will be bypassed. And at the end of OnPaint, clear the flag, so that the background will be erased in other cases.
  3. When the frame is resized, or scroll bar is dragged, or window is reactivated, WM_NCPAINT will always be sent to your program. This is the case that you would clear your EraseBkgnd flag, so that the background will be erased, instead of giving you a see-through client area.

End

The detail is in the source code. Since it is quite self-explanatory, I won't waste my breath here. I hope you find this article useful. Thanks for reading.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

lucy


I love this place!
Occupation: Software Developer
Location: Canada Canada

Other popular GDI articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralThanks for this articlememberBryanInTokyo19:51 9 Aug '05  
GeneralI am looking for a simple MFC Control with Draw Areamemberp2002ad7:38 25 May '05  
GeneralUnable to pass CDC* to another functionsussC++Rules9:06 20 Sep '03  
GeneralThanksmemberMoak6:24 19 Feb '03  
GeneralSimple and to the PointmemberJEWeaver6:30 7 Nov '02  
Generalpicture...memberMario M.2:01 16 Jun '02  
GeneralRe: picture...memberlucy4:58 16 Jun '02  
GeneralRe: picture...memberShog95:22 16 Jun '02  
General说实话,您的文章实在没您本人亮丽memberAnonymous5:46 16 Jun '02  
GeneralSuck guysmemberMyronLi0:47 10 Sep '07  
General咳! 太可惜了member金牌王老五3:21 15 Jun '02  
GeneralRe: 咳! 太可惜了memberAnonymous14:24 15 Jun '02  
GeneralWooomemberAny12:16 14 Jun '02  
General...memberMario M.11:58 14 Jun '02  
GeneralRe: ...memberlucy12:10 14 Jun '02  
GeneralRe: ...memberBrit14:21 14 Jun '02  
GeneralRe: ...memberMario M.15:41 14 Jun '02  
GeneralRe: ...sussAnonymous21:58 18 Jul '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Jun 2002
Editor: Nishant Sivakumar
Copyright 2002 by lucy
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project