"MFC" MFC drawing does not flicker--double buffering Technology

Source: Internet
Author: User

MFC drawing does not flicker--double buffering technology [go]

2010-04-30 09:33:33| Category: VC| Report | Font size Subscription

[Transfer from: Http://blog.163.com/[email protected]/blog/static/49846449201033093333394/]

When VC/MFC with CDC drawing, frequent refresh, the screen will appear flashing phenomenon, CPU time occupation rate is very high, drawing efficiency is very low, it is easy to appear program crashes.

The so-called double buffering technology, the following is the explanation of Baidu Encyclopedia:

When we watch TV, the screen we see is called the OSD layer, which means we can only see the image on the OSD layer. Now, I need to create a virtual, invisible, but can draw (for example, draw dots, lines) of the OSD layer, I call offscreen (back buffer). This offscreen exists in memory, we draw on it, this offscreen above can be displayed on the OSD layer, need a function to create this offscreen, return this offscreen handle (int pointer), width, height, A pointer to the new offscreen data buffer, which is a offscreen data buffer created outside the function, and the size is offscreen height * Width * The size of each pixel point data. Flicker is a common problem in graphic programming. Graphics operations that require multiple complex drawing operations can cause the rendered image to blink or have other unacceptable appearances. The use of double buffering solves these problems. Double buffering uses memory buffers to resolve flicker problems caused by multi-draw operations. When double buffering is enabled, all drawing operations are first rendered to the memory buffer, not the drawing surface on the screen. When all the drawing operations are complete, the memory buffers are copied directly to the drawing surface to which they are associated. Because only one graphics operation is performed on the screen, the image flicker caused by complex drawing operations is eliminated.

In the process of graphic image processing, double buffering is a basic technique. We know that when a form responds to a WM_PAINT message with complex graphics, the form flickers when it is redrawn due to an over-frequency refresh. The effective way to solve this problem is double buffering technology.

Because the form is refreshed, there is always a process to erase the original image OnEraseBkgnd, which fills the form plot area with the background color, and then redraws it by invoking a new drawing code, which causes the contrast of the image color. When WM_PAINT's response is frequent, the contrast is becoming more pronounced. So we saw a flicker.

It's natural to think that avoiding background-colored padding is the most straightforward way. But in that case, the form will become a mess. Because each time the image is not removed from the original image, resulting in the image of the residue, so when the form is redrawn, the screen will often become a mess. So simply banning background repainting is not enough. We also have to re-draw, but the requirements are fast, so we think of using the BitBlt function. It can support the copying of graphics blocks and is very fast. We can draw in memory first, and then use this function to copy the good diagram to the foreground, and suppress the background refresh, thus eliminating flicker. The above is also the basic idea of double-buffered drawing.

First, according to the normal diagram method to program. That is, add the drawing code to the OnDraw function of the view class. Here we draw a number of concentric circles, the code is as follows:

cbcdoc* PDoc = GetDocument ();

Assert_valid (PDOC);

CPoint Ptcenter;

CRect Rect,ellipserect;

GetClientRect (&rect);

Ptcenter = rect. CenterPoint ();

for (int i=20;i>0;i--)

{

Ellipserect.setrect (Ptcenter,ptcenter);

Ellipserect.inflaterect (I*10,I*10);

Pdc->ellipse (Ellipserect);

}

Compile and run the program, try to change the window size, you can find the flicker phenomenon.

In the double buffering method, the first thing to do is to mask the background refresh. The background refresh is actually in response to the WM_ERASEBKGND message. We add a response to this message in the view class, and we can see that the default code is as follows:

BOOL cmyview::onerasebkgnd (cdc* PDC)

{

Return Cview::onerasebkgnd (PDC);

}

is to call the ONERASEBKGND function of the parent class, we block this call and only return TRUE directly;

Here are the steps for the memory buffer drawing.

CPoint Ptcenter;

CRect Rect,ellipserect;

GetClientRect (&rect);

Ptcenter = rect. CenterPoint ();

CDC Dcmem; Memory DC for buffering the drawing

CBitmap bmp; Bitmap that hosts temporary images in memory

Dcmem.createcompatibledc (PDC); Dependency window DC creates a compatible memory DC

Bmp. CreateCompatibleBitmap (&dcmem,rect. Width (), Rect. Height ());//Create a compatible bitmap

Dcmem.selectobject (&bmp); Selecting a bitmap into a memory DC

Dcmem.fillsolidrect (Rect,pdc->getbkcolor ());//fill the customer area with the original background, otherwise it will be black

for (int i=20;i>0;i--)//make the same concentric circle image on the memory DC

{

Ellipserect.setrect (Ptcenter,ptcenter);

Ellipserect.inflaterect (I*10,I*10);

Dcmem.ellipse (Ellipserect);

}

Pdc->bitblt (0,0,rect. Width (), Rect. Height (), &dcmem,0,0,srccopy);//Copy the image on the memory DC to the foreground

Dcmem.deletedc (); Remove DC

Bm.                                        DeleteObject (); Delete Bitmap

As a result of complex drawing operations into the background, we see a fast copy operation, and naturally eliminate the flicker phenomenon.

"MFC" MFC drawing does not flicker--double buffering Technology

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.