Click here to Skip to main content
15,607,633 members
Articles / Desktop Programming / MFC
Posted 27 Mar 2000


62 bookmarked

Flicker Free Redrawing and Background Buffering of Drawing with Two Simple Classes

Rate me:
Please Sign up or sign in to vote.
4.88/5 (8 votes)
27 Mar 2000
Two classes that make double buffering simple


I present here two classes which can be used for:

  1. Flicker free drawing
  2. Buffering drawing, for example, the background

The advantages of these two classes are:

  • There are multiple buffers possible for several purposes. Unlike the code of several other CMemDC classes at other sites, there is a distinction between the buffers and the code which is used to access these buffers.
  • When we use GetSafeCDC() for all drawing operations, we never get a problem during printing or previewing because we simply bypass in this case, the pointer to CDC which is given to OnDraw. This simple method will never fail.
  • 16 and 32 bit compatible
  • The buffers can be allocated once. If we have a large screen, we do not allocate a new big bitmap for each OnDraw event. If only a part of the screen needs to be redrawn, it is still possible to declare and use a temporary buffer.
  • If the buffer allocation fails due to limited system resources, all drawings will be done unbuffered
  • If we have drawing operations which do last a long time, it is possible to show the user at regular time intervals (for example, each second) intermediate results. The implementation for this is simple: Just call CopyToScreen(1000) during these operations. This will copy all seconds the current content of the background buffer to the screen.

Note: During preview, the CDC supplied to OnDraw is internally a CPreviewDC. This is an internal MFC class. This class does have some functions which are overloaded and not virtual. So when I used other CMemDCs implementation, I had errors during printing and previewing because those CMemDC did not call the functions of CPreviewDC but called the corresponding functions of CDC.

Using the Classes

There are a lot of possibilities how to use this classes. Look at the declarations in MemDc.h.

An example of usage, explained step by step:

  1. Define one or more CBufferMemDC in the header of your CView derived class. In my case, I have defined two buffers. One for the general background which is static and one for the information which I had to redraw more often on the background. It is also possible to allocate an additional buffer for the final result of the drawing to buffer the total window client area for the case where the user does switch between the applications for example with Alt-Tab.


    CBufferMemDC m_BufferBackground;
    // a buffer for the part of drawing which does not change
    CBufferMemDC m_BufferFlickerFreeDrawing;
    // a buffer which is used for flicker free redrawing on top of
    // the background
  2. Add the following function to your CView derived class:
    static void CalcSizeTotalAreaWnd( CDC* pDC, const YourView* pView,
                                      CRect& totalAreaWnd ) {
    	if ( pDC->IsPrinting() ){
    // if we are printing we get the total area from the clip box. 
    // Perhaps there is a better way to get this information
    	} else {
    // if we don't print we just take the client area of the window
    		pView->GetClientRect( totalAreaWnd );
  3. Put the implementation of your OnDraw function in another function. For example:
    OnRedraw( CDC* pDC).
  4. Modify your OnDraw function:
    void MyView::OnDraw( CDC* pDC )
    	CRect totalAreaWnd;
    	CalcSizeTotalAreaWnd( pDC, this, totalAreaWnd );
    	CMemDC memDC( pDC, totalAreaWnd, totalAreaWnd,
                         &m_BufferBackground );
    	if ( pDC->IsPrinting() || m_BufferBackground.IsDirty()){
    // draw what you want to draw, for example a line. We simply call here
    // our routine, which we have defined above. It is important not to
    // use memDC, but GetSafeCDC. See the descriptions of this function
    // in the header file. Note: OnRedraw is only called when the
    // background buffer is dirty or when we are printing. Printing
    // processes are not buffered because this can
    // create very big bitmaps if we use for example A0-printers
    		OnRedraw( memDC.GetSafeCDC() );	
    // when we have drawn all, we have all information in the bitmap but 
    // not on the screen. So we copy the contents of the buffer to the
    // screen, to show the user what has happened. The user will see only
    // the result and so we have realized the flicker free redrawing

My own implementation is more complex because I had to buffer not only the background but also several intermediate states during drawing. But this was no problem with these two classes because these two classes do solve the two main parts of such problems: How to handle a buffer and how to do the buffering.

At the end, I have to say that I was astonished at how small the final code was.


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.

Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionHow to use in a dialog-based app? Pin
Hawks22-Aug-05 7:49
Hawks22-Aug-05 7:49 
GeneralCFormView Pin
Member 3920046-Jul-04 23:58
Member 3920046-Jul-04 23:58 
Generalmemory leak in CBufferMemDC::SetSizeBuffer Pin
shettelbus16-Jun-03 13:33
shettelbus16-Jun-03 13:33 
GeneralMemory Leaks! Pin
klm25-Jul-02 20:36
klm25-Jul-02 20:36 
GeneralRe: Memory Leaks! Pin
klm25-Jul-02 23:52
klm25-Jul-02 23:52 
QuestionCool But? Pin
jack Mesic14-Apr-02 20:25
jack Mesic14-Apr-02 20:25 
QuestionWhat OnRedraw()??? Pin
13-Sep-01 16:24
suss13-Sep-01 16:24 
GeneralCMemDC Pin
19-Aug-01 22:27
suss19-Aug-01 22:27 
QuestionBlack background??? Pin
4-May-01 0:13
suss4-May-01 0:13 
AnswerRe: Black background??? Pin
Anonymous8-Mar-03 10:52
Anonymous8-Mar-03 10:52 
GeneralRe: Black background??? Pin
Tydia-kun12-Aug-06 4:16
Tydia-kun12-Aug-06 4:16 
GeneralUse with CScrollView Pin
richard25-Sep-00 7:47
richard25-Sep-00 7:47 
GeneralRe: Use with CScrollView Pin
21-Oct-01 23:58
suss21-Oct-01 23:58 
Generalproblems copying data between buffers Pin
Yosi Keller28-Aug-00 10:11
sussYosi Keller28-Aug-00 10:11 
GeneralRe: problems copying data between buffers Pin
Ernst Versteeg28-Aug-00 12:00
Ernst Versteeg28-Aug-00 12:00 
GeneralSpeed Increase Pin
David Gallagher21-Aug-00 13:48
David Gallagher21-Aug-00 13:48 
GeneralRe: Speed Increase Pin
7-May-01 1:13
suss7-May-01 1:13 
GeneralOnEraseBgnd ! Pin
David Gallagher21-Aug-00 13:01
David Gallagher21-Aug-00 13:01 
Generalyes, it's good idea. Pin
Andy Byron7-Dec-03 16:26
Andy Byron7-Dec-03 16:26 

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.