Click here to Skip to main content
15,890,438 members
Articles / Desktop Programming / MFC
Article

Drawing without flicker

Rate me:
Please Sign up or sign in to vote.
4.29/5 (19 votes)
28 Jun 20021 min read 184.1K   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 
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 
Anonymous pussy wrote:
IF you THINK that you are the only guy who knows programming, think deeply
again. Who the hell do u think u are trying to tell others what u feel ?
Better find something else to do.


Thank you for this opportunity to listen to the opinions of a brain dead moron, who does not have the guts to put his name to his words. Let's examine the evidence shall we ?

1. You are a worthless pile of dog manure.
2. I never said I was the only person who knows how to program, where the hell do you get that from ?
3. You are a fetid steaming heap of donkey droppings.
4. What I said is that most people do not know how to program. Catch a bus, take a poll and you'll find it is true.
5. You are a pathetic lump of sparrow vomit.
6. When people post articles, other people find them useful, and people like you comwe along, claiming they are of no worth and do not work, only a fool would presume that anyone other than the person incapable of using the code provided is the problem
7. You are an ugly vomit stain on the fabric of society.

Anonymous pussy wrote:
NOBODY needs an idiots advice and keep them to yourself. You are only
spreading your contagious disease ( idioticism ).


I guess if this is the best you can do to string a sen
tence together, then it's no wonder you'r embarrased to put your name to it.

Anonymous pussy wrote:
Do something useful rather than try to PROVE that u are an idiot of the
highest category


Oh, I see. You mean like 28 articles, and countless posts helping people in the forums ? Get stuffed. Sometimes helping people does not mean saying what they want to hear. Deal with it.

Anonymous pussy wrote:
Hmmmmmmmmmm. Feeling sad that such idiots like you dont get exterminated.
Just asking you out of curiousity, werent u dodos termed "extinct" ? I thought
that u were. Gotta ask my prof. again. He's interested in collecting
specimens like u grauss.


The guy in the white coat is not a professor and your lab jacket isn't fitting badly, it's *supposed* to restrain you.

Anonymous pussy wrote:
I've been noticing quite a huge number of stupid posts from you trying
to prove a stupid point rather than doing something constructive. Wont u
ever stop that nonsense ?


In that case, if you're such a regular here, why do you not have a name ? Because you're a pussy, that's why. What makes you think I could care less of the opinions of someone who thinks the way to respond to an intelligent comment is to talk about extermination, idiots and so on, but to not have the courage to put a name to thier comments ? You are below contempt. I hate you. I really do. People like you are lowering the signal to noise ratio of this site, and the only good thing about you is that I am reminded that there are still idiots in this world, who once they fail their degree will need to employ people like me to write code for them.

Now get stuffed. Or tell us who you are, if you're not a worthless lump of jelly. It's easy to attack people anonymously. I'd much prefer to meet you face to face, so you can try to 'exterminate' me, and I can beat the crap out of you.

I'm waiting.....

Anonymous pussy wrote:
Hope this mail brings you back to your senses and get rid of your stupid
thoughts from your mind asap. Thats good for all of us.


I hope you crawl back under your rock, and consider what a gutless wimp you are.

Anonymous whiney pussy wrote:
Oh yeah. i posted it under an anonymous name bcoz i dont want myself to
be seen as someone trying to put sense into a fool and i shouldnt even be
trying to do that.


I see, you ARE stupid then. How do you remember to breathe ? You HAVE 'tried to talk sense to me', ( a.k.a. insulted me, and that badly ), you just don't have the guts to put your name to the attempt. Get over yourself.

Anonymous whiney worthless pussy wrote:
Makes me sick infact to write this to a GROSS MISTAKE like
you and hope its the last.


Like I said. Get in the ring, or go home and cry to momma.



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
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.