Click here to Skip to main content
Licence 
First Posted 28 Jun 2002
Views 153,274
Bookmarked 60 times

Drawing without flicker

By | 28 Jun 2002 | Article
Steps for Double buffering ( Drawing without flicker )

Explanation of Double Buffering

If you have to draw repeatedly to the screen in a short period of time, then when you keep drawing step by step to the DC, it updates the Window again and again which makes the screen flicker.

To avoid this, we first draw to a DC in memory (see the CDC MemDC and CBitmap MemBitmap declarations), and we store the resultant drawing in a memory bitmap. After all the drawing has been completed, we move the bitmap from the memory to the screen in a fast single bitblt call. Thus, we only need to draw once to the screen which avoids flickering totally. This principle is called Double Buffering.

Example code

This example assumes that you are going to draw filled rectangles onto the screen. It generates random shades of green to fill that rectangle.

Use this code in your view class's OnDraw() function if you are using Doc/View architecture or if you are using a dialog based application, then you can add this code in the OnPaint Function.

    CRect rcClient;		
    GetClientRect(rcClient);	// See Note 1

    CDC MemDC,*pDC;
    CBitmap MemBitmap;

    pDC = this->GetDC()         // Get Current DC
    MemDC.CreateCompatibleDC(pDC);
    MemBitmap.CreateCompatibleBitmap(pDC,rcClient.right,rcClient.bottom);

    CBitmap *pOldBitmap = MemDC.SelectObject(&MemBitmap);
    CBrush bkBrush(HS_FDIAGONAL,RGB(0,rand()%255,0));	// See Note 2
    MemDC.FillRect(rcClient,&bkBrush);

    pDC->BitBlt(0,0,rcClient.right,rcClient.bottom,&MemDC,0,0,SRCCOPY);	//See Note 3
    MemDC.SelectObject(pOldBitmap);

Note 1 : Gets the coordinates of the bounding rectangle.
Note 2 : Creates a brush with random shades of green. The rand()%255 generates a value between 0 and 255 randomly.
Note 3 : Copies the bitmap from the memory dc to the pdc using a fast bitblt function call.

I hope that i have made it clear to you. Good Luck.

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

About the Author

VGirish

Founder

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow double buffering is done in GDI+ Pinmember00thilani8:52 5 Feb '08  
GeneralFinally! Pinmembergregmiller016:27 15 Nov '07  
GeneralThanks PinmemberTrollslayer10:37 12 Oct '05  
GeneralUsing In a child Dialog Pinmemberblueneon6:15 11 Jan '04  
Generalabout BitBlt PinmemberMuhammad Ahmed2:57 16 Jun '03  
QuestionLeak: What the? Pinmembersquid4:59 26 Nov '02  
AnswerRe: Leak: What the? PinmemberChristian Graus18:01 2 Nov '03  
GeneralRe: Leak: What the? PinmemberChristian Graus18:02 2 Nov '03  
GeneralGreat Code ! Pinmembersquid12:52 21 Nov '02  
GeneralRe: Great Code ! PinmemberChristian Graus15:51 21 Nov '02  
GeneralRe: Great Code ! PinsussAnonymous20:15 22 Nov '02  
GeneralRe: Great Code ! PinmemberChristian Graus21:23 23 Nov '02  
GeneralCHRISTIAN GRAUSS - THE UGLY DODO PinsussAnonymous22:45 23 Nov '02  
GeneralRe: CHRISTIAN GRAUSS - THE UGLY DODO PinmemberChristian Graus22:56 23 Nov '02  
GeneralRe: CHRISTIAN GRAUSS - THE UGLY DODO PinmemberBrendan Tregear17:57 2 Nov '03  
GeneralRe: CHRISTIAN GRAUSS - THE UGLY DODO PinmemberChristian Graus18:05 2 Nov '03  
GeneralNice excellent article thanx a lot PinsussAnonymous21:16 9 Jul '02  
GeneralRe: Nice excellent article thanx a lot PinmemberVGirish22:25 9 Jul '02  
GeneralRe: Nice excellent article thanx a lot PinmemberVGirish22:29 9 Jul '02  
GeneralRe: Nice excellent article thanx a lot PinmemberParasuraman SundarRajan1:08 10 Jul '02  
Generalreinventing the weel PinmemberAnonymous12:14 29 Jun '02  
GeneralRe: reinventing the weel PinmemberShog912:33 29 Jun '02  
GeneralRe: reinventing the weel PinmemberChristian Graus18:49 29 Jun '02  
GeneralRe: reinventing the weel PinmemberWREY13:38 2 Jul '02  
GeneralRe: reinventing the weel PinmemberChristian Graus14:14 2 Jul '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 29 Jun 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid