Click here to Skip to main content
Click here to Skip to main content

Double Buffered DC Class

, 15 Jan 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Alex Kolesnichenko
Software Developer (Senior)
United States United States
Started professional career in software development back in 2000, in Ukraine. Founder and owner of a boutique software company called ByteGems.com Software. Worked for 6 years 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.

Comments and Discussions

 
GeneralBuffered DC PinmemberMarcioKerob7-Jun-06 9:32 
GeneralInventing the wheel over and over again PinmemberTom Collins15-Jan-04 23:38 
GeneralRe: Inventing the wheel over and over again PineditorRob Manderson16-Jan-04 0:33 
GeneralRe: Inventing the wheel over and over again PinmemberJohn M. Drescher16-Jan-04 5:51 
GeneralRe: Inventing the wheel over and over again PinmemberJörgen Sigvardsson16-Jan-04 11:32 
GeneralRe: Inventing the wheel over and over again PinmemberJohn M. Drescher16-Jan-04 11:53 
GeneralRe: Inventing the wheel over and over again Pinmemberleppie18-Jan-04 12:32 
GeneralRe: Inventing the wheel over and over again PinmemberFernando A. Gómez F.4-Sep-06 7:48 
QuestionRe: Inventing the wheel over and over again Pinmembergregmiller0130-Oct-07 13:51 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 16 Jan 2004
Article Copyright 2004 by Alex Kolesnichenko
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid