Click here to Skip to main content
16,020,628 members
Articles / Desktop Programming / MFC

Do a Flicker-free Drawing using MFC Methods

Rate me:
Please Sign up or sign in to vote.
4.78/5 (21 votes)
13 Jun 2002CPOL2 min read 352.3K   3.6K   41   20
How to achieve flicker-free drawing when using the methods MFC provides

Image 1

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 painted properly.

Some Tricks

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

  1. When drawing 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
I love this place!

Comments and Discussions

 
Questionwhat to do if MapMode is HIMETRIC? Pin
mikel_mike33126-Jul-09 3:27
mikel_mike33126-Jul-09 3:27 
GeneralThanks Pin
invader117-Dec-08 11:27
invader117-Dec-08 11:27 
GeneralThanks for this article Pin
BryanInTokyo9-Aug-05 18:51
BryanInTokyo9-Aug-05 18:51 
GeneralI am looking for a simple MFC Control with Draw Area Pin
p2002ad25-May-05 6:38
p2002ad25-May-05 6:38 
GeneralUnable to pass CDC* to another function Pin
Sheldon20-Sep-03 8:06
Sheldon20-Sep-03 8:06 
GeneralThanks Pin
Moak19-Feb-03 5:24
Moak19-Feb-03 5:24 
GeneralSimple and to the Point Pin
JEWeaver7-Nov-02 5:30
JEWeaver7-Nov-02 5:30 
I'm new to MFC and struggled for two days with halfway-working examples. This one was simple enough to follow, while structured to present 'real world' placement in the app framework. Bonus points for lots of clear remarks in the code and pointing out the need to deal with OnEraseBkgnd().

My code works now. Thank you.
Generalpicture... Pin
Mario M.16-Jun-02 1:01
Mario M.16-Jun-02 1:01 
GeneralRe: picture... Pin
lucy16-Jun-02 3:58
lucy16-Jun-02 3:58 
GeneralRe: picture... Pin
Shog916-Jun-02 4:22
sitebuilderShog916-Jun-02 4:22 
General说实话,您的文章实在没您本人亮丽 Pin
16-Jun-02 4:46
suss16-Jun-02 4:46 
GeneralSuck guys Pin
MyronLi9-Sep-07 23:47
MyronLi9-Sep-07 23:47 
General咳! 太可惜了 Pin
15-Jun-02 2:21
suss15-Jun-02 2:21 
GeneralRe: 咳! 太可惜了 Pin
15-Jun-02 13:24
suss15-Jun-02 13:24 
GeneralWooo Pin
14-Jun-02 11:16
suss14-Jun-02 11:16 
General... Pin
Mario M.14-Jun-02 10:58
Mario M.14-Jun-02 10:58 
GeneralRe: ... Pin
lucy14-Jun-02 11:10
lucy14-Jun-02 11:10 
GeneralRe: ... Pin
Brit14-Jun-02 13:21
Brit14-Jun-02 13:21 
GeneralRe: ... Pin
Mario M.14-Jun-02 14:41
Mario M.14-Jun-02 14:41 
GeneralRe: ... Pin
Anonymous18-Jul-02 20:58
Anonymous18-Jul-02 20:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.