Original link: http://blog.csdn.net/clodfront/article/details/5488648
Why use double buffering to draw
When you are plotting multiple primitives:
Because it is to be a picture, so each drawing an entity, the system will do a graphical drawing operation, the graphic redraw is very resource-intensive, especially when the number of graphics need to redraw a lot of time, the resulting consumption is particularly large, resulting in flashing, not smooth and so on. So how to solve this problem?
That's double buffering.
The rationale for this is
First, create a virtual canvas in memory, and then draw all the elements you need to draw on this "virtual canvas", and finally draw the entire "virtual canvas" onto the real form at once. Because all the individual drawing is not really called the display system to "draw", so will not occupy the overhead of the display system, greatly improve the efficiency of the drawing, avoid flicker, not smooth and so on.
Specific steps for GDI + to implement double buffering
Ide:vc++/vs.net C + +
1. Create a "virtual canvas" in memory:
Get window Area Width height
RECT R;
GetClientRect (M_HWND,&R);
M_bufw=r.right; M_bufh=r.bottom;
Create a derived class of image in memory Bitma object as "canvas"
Bitmap Membitmap (M_BUFW,M_BUFH);
2. Get the graphics reference for this memory canvas:
Graphics memgr (&MEMBITMAP);
3. Draw on this piece of memory canvas:
Here you can draw multiple entities on the Membitmap by memgr
I only have an example of drawing a picture
Image* Pimage=null;
Pimage=new image (L "res//picture 1.png");
int W=pimage->getwidth ();
int H=pimage->getheight ();
Memgr.drawimage (Pimage,0,0,0,0,w,h,unitpixel);
Delete Pimage;
Pimage=null;
4. Draw the memory canvas into the window
M_HDC = GetDC (m_hwnd);
Graphics GR (M_HDC);
Gr. DrawImage (&membitmap,0,0);
ReleaseDC (M_HWND,M_HDC);
Analysis and implementation of simple GDI + double buffering