Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / MFC
Article

Drawing without flicker

Rate me:
Please Sign up or sign in to vote.
3.62/5 (18 votes)
28 Jun 20021 min read 183.9K   61   34
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


Written By
Founder
India India
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 double buffering is done in GDI+ Pin
00thilani5-Feb-08 8:52
00thilani5-Feb-08 8:52 
GeneralFinally! Pin
gregmiller0115-Nov-07 6:27
gregmiller0115-Nov-07 6:27 
GeneralThanks Pin
Trollslayer12-Oct-05 10:37
mentorTrollslayer12-Oct-05 10:37 
GeneralUsing In a child Dialog Pin
blueneon11-Jan-04 6:15
blueneon11-Jan-04 6:15 
Generalabout BitBlt Pin
Muhammad Ahmed16-Jun-03 2:57
Muhammad Ahmed16-Jun-03 2:57 
QuestionLeak: What the? Pin
squid26-Nov-02 4:59
squid26-Nov-02 4:59 
AnswerRe: Leak: What the? Pin
Christian Graus2-Nov-03 18:01
protectorChristian Graus2-Nov-03 18:01 
GeneralRe: Leak: What the? Pin
Christian Graus2-Nov-03 18:02
protectorChristian Graus2-Nov-03 18:02 
GeneralGreat Code ! Pin
squid21-Nov-02 12:52
squid21-Nov-02 12:52 
GeneralRe: Great Code ! Pin
Christian Graus21-Nov-02 15:51
protectorChristian Graus21-Nov-02 15:51 
squid wrote:
Most of the code published by so called guru's is junk that most people would not understand anyway.

I humbly submit that 'most people' do not know how to program, and that this is not the fault of the few that do.



Christian

No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002

Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002

Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
GeneralRe: Great Code ! Pin
Anonymous22-Nov-02 20:15
Anonymous22-Nov-02 20:15 
GeneralRe: Great Code ! Pin
Christian Graus23-Nov-02 21:23
protectorChristian Graus23-Nov-02 21:23 
GeneralCHRISTIAN GRAUSS - THE UGLY DODO Pin
Anonymous23-Nov-02 22:45
Anonymous23-Nov-02 22:45 
GeneralRe: CHRISTIAN GRAUSS - THE UGLY DODO Pin
Christian Graus23-Nov-02 22:56
protectorChristian Graus23-Nov-02 22:56 
GeneralRe: CHRISTIAN GRAUSS - THE UGLY DODO Pin
Brendan Tregear2-Nov-03 17:57
Brendan Tregear2-Nov-03 17:57 
GeneralRe: CHRISTIAN GRAUSS - THE UGLY DODO Pin
Christian Graus2-Nov-03 18:05
protectorChristian Graus2-Nov-03 18:05 
GeneralNice excellent article thanx a lot Pin
Anonymous9-Jul-02 21:16
Anonymous9-Jul-02 21:16 
GeneralRe: Nice excellent article thanx a lot Pin
VGirish9-Jul-02 22:25
VGirish9-Jul-02 22:25 
GeneralRe: Nice excellent article thanx a lot Pin
VGirish9-Jul-02 22:29
VGirish9-Jul-02 22:29 
GeneralRe: Nice excellent article thanx a lot Pin
Parasuraman SundarRajan10-Jul-02 1:08
Parasuraman SundarRajan10-Jul-02 1:08 
Generalreinventing the weel Pin
29-Jun-02 12:14
suss29-Jun-02 12:14 
GeneralRe: reinventing the weel Pin
Shog929-Jun-02 12:33
sitebuilderShog929-Jun-02 12:33 
GeneralRe: reinventing the weel Pin
Christian Graus29-Jun-02 18:49
protectorChristian Graus29-Jun-02 18:49 
GeneralRe: reinventing the weel Pin
WREY2-Jul-02 13:38
WREY2-Jul-02 13:38 
GeneralRe: reinventing the weel Pin
Christian Graus2-Jul-02 14:14
protectorChristian Graus2-Jul-02 14:14 

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.