Click here to Skip to main content
Licence 
First Posted 15 Jan 2004
Views 48,201
Bookmarked 21 times

Double Buffered DC Class

By | 15 Jan 2004 | Article
CDC descendant with double buffering abilities

Introduction

If you have ever drawn something in the window by yourself then you know about flickering. The right solution to get rid of it is to use double-buffering by creating the off-screen bitmap, painting there and copying the bitmap to the window. This is pretty simple technique, but you can use this class to minimize your handwork.

Background

The technique is really simple. Create memory DC using CreateCompatibleDC() function, then create the bitmap of desired size and select it to the memory DC by using SelectObject() function. Draw on the memory DC and copy the result to the window DC with BitBlt(). The trick is to create right bitmap. I use CreateBitmap() function to specify correct BPP value.

Using the Code

To start using CakBufferedDC class just declare the object of it. Note that this class was designed to use only in WM_PAINT message handler (OnPaint method of CWnd). It will automatically call BeginPaint() and EndPaint() functions.

void CSomeWindow::OnPaint()
{
  CakBufferedDC dc(this);     // device context for painting
  //  Do the stuff
  dc.FillRect(&dc.ClientRect(), &FancyBruch);
  dc.DrawText(_T("Hello world"), &dc.ClientRect(), DT_VCENTER|DT_SINGLELINE);
}

When the object of CakBufferedDC will be destroyed, it will automatically copy your painting to the window. Only region that needs an update will be copied.

Since CakBufferedDC is a CDC descendant, you will be able to use all methods of CDC class (with the exception of Attach() and Detach() for obvious reasons) and pass the object of CakBufferedDC to API functions that expect variable of type HDC. If you didn't use things like MyPaintDC.m_hDC then your code won't feel the difference.

The class also provides you with ClientRect(), WindowRect() and UpdateRect() methods which return client rectangle of the window, window's rectangle and rectangle that needs an update respectively.

History

  • Version 1.0 so far.

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

Alex Kolesnichenko

Software Developer (Senior)
w2bi, Inc
United States United States

Member

Started professional career in software development back in 2000, in Ukraine. Founder and owner of a boutique software company called ByteGems.com Software. Currently employed full time at w2bi, Inc in New Jersey USA.
 
My buzzwords at the moment: .NET, C#, C++, Win32, ATL, MFC, SQL, ASP.NET, WinForms, WebForms, MVC, EF, LINQ, Sockets, TCP/IP, Remoting.

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
GeneralBuffered DC PinmemberMarcioKerob8:32 7 Jun '06  
GeneralInventing the wheel over and over again PinmemberTom Collins22:38 15 Jan '04  
GeneralRe: Inventing the wheel over and over again PineditorRob Manderson23:33 15 Jan '04  
GeneralRe: Inventing the wheel over and over again PinmemberJohn M. Drescher4:51 16 Jan '04  
GeneralRe: Inventing the wheel over and over again PinmemberJörgen Sigvardsson10:32 16 Jan '04  
GeneralRe: Inventing the wheel over and over again PinmemberJohn M. Drescher10:53 16 Jan '04  
GeneralRe: Inventing the wheel over and over again Pinmemberleppie11:32 18 Jan '04  
GeneralRe: Inventing the wheel over and over again PinmemberFernando A. Gómez F.6:48 4 Sep '06  
QuestionRe: Inventing the wheel over and over again Pinmembergregmiller0112:51 30 Oct '07  

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
Web04 | 2.5.120517.1 | Last Updated 16 Jan 2004
Article Copyright 2004 by Alex Kolesnichenko
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid