|

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:
- 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.
- 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.
- 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.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh) | FirstPrevNext |
|
 |
|
|
 |
|
|
Hello,
I am looking for a simple MFC Control with Draw Area.
I would like to:
0) place this Control in CDialog, CFormView
1) draw CDC Objects on it (e.g. CDC::Polyline, CDC::Rectangle)
2) Copy Drawing to Clipboard
3) Save Drawing to EMF
Many Thanks, Jessica
|
| Sign In·View Thread·PermaLink | 3.50/5 (2 votes) |
|
|
|
 |
|
|
I have a few objects that I call from my OnDraw function. It seems that if I try to pass your dcMem object, nothing shows. If I move my drawing code into the OnDraw, then it works fine. Any ideas why I can't pass dcMem as a CDC pointer?
Thanks Sheldon
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
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.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Very nice. Cute kid. 
Oh, and nice job on the article!
--------
Laugh at your problems; everybody else does. --Shog9 --
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
小儿科的东西太多,象是MSDN sample注解, 建议您还是下点工夫,写些有水平的code, 想你肯定是国内大学计算机科班出身, 还是别辱没了中国的大学计算机院系的名声, 否则的话,大家也就只好为您的脸蛋喝彩了.
抱歉,说的太损了点,请原谅!
 
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
 | Wooo |  | Any | 12:16 14 Jun '02 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|