Analysis of the drawing based on WTL double buffer (double buffer) _c language

Source: Internet
Author: User
Tags rand
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 (&AMP;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 (&AMP;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 (&AMP;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);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.