Click here to Skip to main content
12,299,086 members (53,037 online)
Click here to Skip to main content
Add your own
alternative version

Stats

124.6K views
2.9K downloads
36 bookmarked
Posted

Do a flicker-free drawing using MFC methods

, 13 Jun 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
How to achieve flicker-free drawing when using the methods MFC provides

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 callingInvalidateRect(&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)

Share

About the Author

lucy
Software Developer
Canada Canada
I love this place!

You may also be interested in...

Comments and Discussions

 
GeneralThanks Pin
invader117-Dec-08 11:27
memberinvader117-Dec-08 11:27 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 14 Jun 2002
Article Copyright 2002 by lucy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid