MFC double Buffering

Source: Internet
Author: User

As we all know, including the Windows desktop, everything we see is a system picture, the Windows desktop is the equivalent of a blackboard;

"1" ordinary drawing is directly on the blackboard we can see the drawing

"2" Double buffering is the first on a virtual blackboard painting, and so on when the virtual blackboard on the picture copied to the blackboard we can see up;

The advantage of double buffering is the ability to make the picture smooth, you can imagine that the picture is good to paste directly on the blackboard must be faster than on the blackboard re-painting.

—————————————————————— start

First step: Create a new dialog box project

Step Two: Add two buttons:

A named double-buffered drawing;

A named general drawing;

Step three: Declare variables:

On the CMyDlg class, right-click the add variable as follows:

CDC MYDC;
CBitmap bmp;
CBitmap *oldbmp;

First, declare a memory DC compatible with the Windows DC (MYDC) and two memory-compatible bitmaps (bmp,*oldbmp)

Fourth Step: Add the following code in the OnInitDialog () function:

Window DC

CDC *dc=getdc ();

Create a Memory DC (MYDC) and bitmap (bmp,*oldbmp) that is compatible with the Windows DC
Mydc.createcompatibledc (DC);
Bmp. CreateCompatibleBitmap (dc,200,200);

Select the memory bitmap in the memory DC to hold the graphics drawn in the memory DC

Oldbmp=mydc.selectobject (&bmp);

Draw a few small circles in the memory DC, with a number of numbers (reflecting the advantages of double caching)

for (int i=0;i<200;i+=6)
for (int j=0;j<200;j+=6)
Mydc.ellipse (i-3,j-3,i+3,j+3);

Fifth step: Right-click the CMyDlg class to add the Windows message response function Wm_close, add the following code:

Mydc.selectobject (oldbmp);
Bmp. DeleteObject ();
Mydc.deletedc ();

Select the original bitmap, delete the memory bitmap object and the memory DC

Sixth step: Double-click the double-buffering button to add the following code:

GetDC ()->stretchblt (0,0,200,200,&mydc,0,0,200,200,srccopy);

Paste the graphics from the memory DC into the window;

Seventh step: Double-click the "Normal Drawing" button to add the following code:

for (int i=0;i<200;i+=6)
for (int j=0;j<200;j+=6)
GetDC ()->ellipse (i-3,j-3,i+3,j+3);

Draw in the normal way in the window and in the memory DC in the same number and size of the bitmap;

The eighth step: Run the program ......... .........

Click the Normal drawing button, you can see the speed of the drawing is a bit slow

Then click the double-buffered draw button, and the image will be displayed immediately.

That's the difference between a double cushion and a normal drawing.

______________________________________________________________________ completed

MFC 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.