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);
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