This example shows how could be useful 'Double Buffering' technique.
void CDrawingArea::OnPaint()
{
CPaintDC dc(this); // device context for painting
//
// Draw with 'double buffering'
//
if( m_bDoubleBuffering ) {
CBitmap bitmap;
CDC dcMem;
if( dcMem.CreateCompatibleDC( &dc ) ) {
bitmap.CreateCompatibleBitmap(&dc, m_rectArea.Width(), m_rectArea.Height() );
CBitmap* pOldBitmap = dcMem.SelectObject(&bitmap);
/*************/
Draw(dcMem);
/*************/
dc.BitBlt( m_rectArea.left,
m_rectArea.top,
m_rectArea.Width(),
m_rectArea.Height(),
&dcMem,
m_rectArea.left,
m_rectArea.top,
SRCCOPY );
dcMem.SelectObject(pOldBitmap);
}
}
//
// Draw without 'double buffering'
//
else {
Draw(dc);
}
}
Where Draw(...)
is the function that draws objects into the device context (could be the 'real' or the 'memory' device context).
There is another most important effect: the drawing objects are 'clipped' into the device context automatically.
Of course, there is a peformace reduction due to the BitBlt
function.
Reading the example code you can found a class called CDrawingArea
. This class represents the embryo for a complex class I'm working on.
When completed, the user could draw lines, text, ellipses and all others geometric figures. It will allow the user to zoom in and out, pan, copy to clipboard or copy the image into a file, an so on.