There are two mix-in classes in the WTL: CdoublebufferimplAnd Cdoublebufferwindowimpl, used to create a double buffered drawing window, which is very easy to use.
The following creates a common WTL window class that has a large amount of drawing work in the client area of the window and uses the Cdoublebufferimpl class to eliminate flicker when drawing:
Copy Code code as follows:
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 the Windows Vista operating system has added built-in support for double buffered paint, and here is an article that describes
How to use these APIs in WIN32 programs:
Using Windows Vista built-in Double buffering
Using Vista in WTL is easy, with the latest WTL library offering Cbufferedpaintimpl and Cbufferedpaintwindowimpl two classes, The usage of these two classes is almost the same as that of the two WTL from the preceding mentioned. The difference is simply that the parameters of the overloaded DoPaint () function are slightly different.
for the Cbufferedpaintimpl class, the desired overloaded dopaint () function looks like the following:
Copy Code code as follows:
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);
}