Click here to Skip to main content
13,044,428 members (86,889 online)
Click here to Skip to main content
Add your own
alternative version


44 bookmarked
Posted 3 May 2007

CBufferDC: a simple MFC class for automatic flicker free drawing

, 3 May 2007
Rate this:
Please Sign up or sign in to vote.
A CPaintDC replacement for in OnPaint painting. Simply change CPaintDC dc(this) in CBufferDC dc(this), and you're done

Screenshot - shot.jpg


When working on my HyperLink control I found the need to do flicker free drawing and I thought about creating a simple class which could do so automatically, with the least amount of code modification possible. So I wrote this little class to accomplish that.

Using the code

Simply replace the declaration of your DC in your OnPaint like so:

From this:

CPaintDC dc(this);

// Your drawing here

To this:

CBufferDC dc(this);

// Your drawing here

And you're done. If you resize the window in the OnPaint after the creation of the CBufferDC, you must remember to call SetBoundsRect with the new client rectangle and with the flags parameter set to DCB_DISABLE if you want to avoid the default behavior of calling this method on a CDC. The reason to use this method and not a custom one is that I wanted my class to be compatible with the CPaintDC one so that someone could simply change the class names to obtain the purpose but could safely return back to the CPaintDC without the need to change his code.


The code provided within the demo (particularly the CBallBouncer class) is only for demonstrative purposes, and is obviously poorly-written and not reusable. But as I said this is only a demo so I wrote it fast to provide a demonstration in which the functionality of the CBufferDC class (that is, avoid flicker) was clear and nothing more.


So, that's all, I think.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Elia Sarti
Synved Ltd.
Ireland Ireland

You may also be interested in...

Comments and Discussions

QuestionThanks a ton! Pin
Member 778699215-Feb-16 19:33
memberMember 778699215-Feb-16 19:33 
GeneralThis rocks! Pin
Leslie Sanford1-Oct-07 18:49
memberLeslie Sanford1-Oct-07 18:49 
GeneralRe: This rocks! Pin
Gladstone2-Oct-07 1:34
memberGladstone2-Oct-07 1:34 
GeneralEffective & simple Pin
Hans Dietrich3-May-07 23:17
mvpHans Dietrich3-May-07 23:17 
GeneralRe: Effective & simple Pin
Gladstone4-May-07 0:00
memberGladstone4-May-07 0:00 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 3 May 2007
Article Copyright 2007 by Elia Sarti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid