Analysis and implementation of simple GDI + double buffering

Source: Internet
Author: User

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

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.