There are two mix-in classes in WTL: Cdoublebufferimpl and Cdoublebufferwindowimpl, which are used to create a double-buffered drawing window with very simple usage.
The following creates a common WTL window class that has a large amount of drawing work in the client area of the window, using the Cdoublebufferimpl class to eliminate flicker when plotting:
Const COLORREF White_color = RGB (255,255,255);
Const COLORREF Blue_color = RGB (0,0,255);
Class CMainWindow:
Public cwindowimpl<cmainwindow,cwindow,csimplewintraits>
Public cdoublebufferimpl<cmainwindow>
{
Public
typedef CMainWindow _THISCLASS;
typedef cdoublebufferimpl<_thisclass> _BASEDBLBUFIMPL;
Begin_msg_map (CMainWindow)
Msg_wm_create (OnCreate)
Msg_wm_destroy (OnDestroy)
Chain_msg_map (_basedblbufimpl)
End_msg_map ()
int OnCreate (lpcreatestruct lpcreatestruct)
{
M_rectpen.createpen (Ps_solid,1,blue_color);
return 0;
}
void OnDestroy ()
{
PostQuitMessage (0);
}
void OnPaint (Cdchandle)
{
CPAINTDC DC (m_hwnd);
DoPaint (DC.M_HDC);
}
void DoPaint (Cdchandle DC)
{
CRect RC;
GetClientRect (&RC);
dc. FillRect (&rc,white_color);
Hpen hOldPen = DC. Selectpen (M_rectpen);
const int width = 5;
int x = 0;
int count = RC. Width ()/width;
int height = 0;
for (int i=0; i<count; i++)
{
Height = (int) ((double) rand () *RC. Height ())/rand_max;
dc. Rectangle (X,RC. Height (), X+WIDTH,RC. Height ()-height);
x + = width;
}
dc. Selectpen (hOldPen);
}
/*
void DoPaint (Cdchandle DC)
{
CRect RC;
GetClientRect (&RC);
int width = rc. Width (), height = rc. Height ();
Use GDI + to draw in the client area
Graphics g (DC.M_HDC);
SolidBrush Whitebrush (Color (255,255,255));
G.fillrectangle (&whitebrush,0,0,width,height);
Pen Bluepen (Color (0,0,255));
const int DX = 5;
int count = WIDTH/DX;
int x = 0, y = 0, h = 0;
for (int i=0;i<count;i++)
{
h = (double) rand () *height)/rand_max;
G.drawrectangle (&BLUEPEN,X,Y,DX,H);
x + = DX;
}
}
*/
Private
CPen M_rectpen;
};
It is worth mentioning that theWindows Vista operating system adds built-in support for double buffered paint , and here is an article that describes how to use these APIs in a WIN32 program:
Using Windows Vista built-in Double buffering
This feature is easy to use with Vista in WTL, and the latest WTL library offers Cbufferedpaintimpl and Cbufferedpaintwindowimpl two classes, The usage of these two classes is almost the same as that of the two WTL that were mentioned earlier. The difference is simply that the parameters of the overloaded DoPaint () function are slightly different.
For the Cbufferedpaintimpl class, the desired overload of the DoPaint () function looks like this:
void DoPaint (cdchandle DC, rect& RECT)
{
CRect RC (rect);
dc. Fillsolidrect (&rc,white_color);
Hpen hOldPen = DC. Selectpen (M_rectpen);
const int width = 5;
int x = 0;
int count = RC. Width ()/width;
int height = 0;
for (int i=0; i<count; i++)
{
Height = (int) ((double) rand () *RC. Height ())/rand_max;
dc. Rectangle (X,RC. Height (), X+WIDTH,RC. Height ()-height);
x + = width;
}
dc. Selectpen (hOldPen);
}
wtl-double-buffered (double buffer) drawing