Click here to Skip to main content
15,072,776 members
Articles / Desktop Programming / MFC
Article
Posted 15 Jan 2004

Stats

68.4K views
1.7K downloads
25 bookmarked

Double Buffered DC Class

Rate me:
Please Sign up or sign in to vote.
3.93/5 (15 votes)
15 Jan 20041 min read
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, currently work in a large multinational company based in Redmond, WA.

My buzzwords at the moment: .NET, C#, ASP.NET, MVC, LINQ, TypeScript, JavaScript, AngularJS, HTML, JSON, services.

Still buzzing: C++, Win32, ATL, MFC, SQL, WinForms, WebForms, EF, Sockets, TCP/IP, Remoting.

Comments and Discussions

 
GeneralBuffered DC Pin
MarcioKerob7-Jun-06 8:32
MemberMarcioKerob7-Jun-06 8:32 
GeneralInventing the wheel over and over again Pin
<b>T</b>om <b>C</b>ollins15-Jan-04 22:38
Member<b>T</b>om <b>C</b>ollins15-Jan-04 22:38 
GeneralRe: Inventing the wheel over and over again Pin
Rob Manderson15-Jan-04 23:33
protectorRob Manderson15-Jan-04 23:33 
GeneralRe: Inventing the wheel over and over again Pin
John M. Drescher16-Jan-04 4:51
MemberJohn M. Drescher16-Jan-04 4:51 
GeneralRe: Inventing the wheel over and over again Pin
Jörgen Sigvardsson16-Jan-04 10:32
MemberJörgen Sigvardsson16-Jan-04 10:32 
GeneralRe: Inventing the wheel over and over again Pin
John M. Drescher16-Jan-04 10:53
MemberJohn M. Drescher16-Jan-04 10:53 
GeneralRe: Inventing the wheel over and over again Pin
leppie18-Jan-04 11:32
Memberleppie18-Jan-04 11:32 
GeneralRe: Inventing the wheel over and over again Pin
Fernando A. Gomez F.4-Sep-06 6:48
MemberFernando A. Gomez F.4-Sep-06 6:48 
QuestionRe: Inventing the wheel over and over again Pin
gregmiller0130-Oct-07 12:51
Membergregmiller0130-Oct-07 12:51 

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.