Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CBufferDC: A Simple MFC Class for Automatic Flicker Free Drawing

0.00/5 (No votes)
3 May 2007 1  
A CPaintDC replacement in OnPaint painting. Simply change CPaintDC dc(this) in CBufferDC dc(this), and you're done.

Screenshot - shot.jpg

Introduction

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.

Notes

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.

Conclusions

So, that's all, I think.

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