VC ++ dual-buffering solution to image refresh Problems

Source: Internet
Author: User

In image processing and programming, double buffering is a basic technology. We know that if the form is responding to the wm_paint message
When complex graphics processing is required, the form will flash due to frequent refresh during repainting. An effective solution to this problem
It is the dual-buffer technology.

When the form is refreshed, there is always an onerasebkgnd process to erase the original image. It uses the background color to fill the drawing area of the form. However
Then, the new drawing code is called for re-painting, which causes the contrast of the image color. When wm_paint responds frequently
, This contrast is more and more obvious. So we can see the flickering phenomenon.

We will naturally think that it is the most direct way to avoid filling the background color. But in that case, the form will become a mess. Because each painting
The original image is not cleared during image plotting, resulting in the residual image. Therefore, when the screen weight is painted, the image is often messy. Institute
It is not enough to simply prohibit background re-painting. We had to re-plot it, but it was fast, so we thought of using the bitblt function.
It supports the copying of graphic blocks at a fast speed. We can plot the graph in the memory first, then use this function to copy the graph to the foreground
Do not refresh the background. This eliminates flickering. The above is the basic idea of Double Buffer plotting.

 

I. Common Methods:

Program by using the common graph method. Add the drawing code to the ondraw function of the video class. Here we plot a number of concentric circles, Generation
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, and try to change the window size to detect flickering.

Ii. Dual buffering method:

In the double buffering method, the first thing to do is to block background refreshing. Background refreshing is actually in response to the wm_erasebkgnd message. In
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 can block this call and only directly return true.

The following describes the steps for drawing the memory buffer.

Cpoint ptcenter;

Crect rect, ellipserect;

Getclientrect (& rect );

Ptcenter = rect. centerpoint ();

CDC dcmem; // memory DC used to buffer plotting

Cbitmap BMP; // bitmap with temporary images in memory

Dcmem. createcompatibledc (PDC); // The attached window DC creates a compatible memory DC

BMP. createcompatiblebitmap (PDC, rect. Width (), rect. Height (); // create a compatible bitmap

Dcmem. SelectObject (& BMP); // select the bitmap into the memory DC

// Fill the customer area according to the original background, otherwise it will be black

Dcmem. fillsolidrect (rect, PDC-> getbkcolor ());

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, srccopy); // copy the image on the memory DC to the foreground

Dcmem. deletedc (); // Delete DC

BM. deleteobject (); // deletes a bitmap.

Because of the complicated drawing operations, we can see that the copying operation is fast, which naturally eliminates the flickering phenomenon.

Note: BMP. createcompatiblebitmap (PDC, rect. Width (), rect. Height ());

In this case, the first parameter createcompatiblebitmap cannot use dcmem. In this case, a black/white bitmap is created. If you want to create a color
The PDC is used to create the memory DC. For details, see the following msdn:

When a memory device context is created, it initially has a 1-by-1 monochrome bitmap selected
It. If this memory device context is used in createcompatiblebitmap, the bitmap that is created is
Monochrome bitmap. To create a color bitmap, use the HDC that was used to create the memory
Device context, as shown in the following code:

HDC memdc = createcompatibledc (HDC );

Hbitmap membm = createcompatiblebitmap (HDC, nwidth, nheight );

SelectObject (memdc, membm );

 

Sample Code:

// Parti
Bool crefreshimgview: onerasebkgnd (CDC * PDC)
{
// Todo: add your message handler code here and/or call default

// Return cview: onerasebkgnd (PDC );
Return true;
}

 

// Partii
Void crefreshimgview: ondraw (CDC * PDC/* PDC */)
{
Crefreshimgdoc * pdoc = getdocument ();
Assert_valid (pdoc );
If (! Pdoc)
Return;

// Todo: Add draw code for native data here
Cpoint ptcenter;
Crect rect, ellipserect;
Getclientrect (& rect );
Ptcenter = rect. centerpoint ();

// Memory DC used for buffering plotting
CDC dcmem;
// Bitmap carrying temporary images in the memory
Cbitmap BMP;
// Attach window DC to create compatible memory DC
Dcmem. createcompatibledc (PDC );
// Create a compatible bitmap
BMP. createcompatiblebitmap (PDC, rect. Width (), rect. Height ());
// Select the bitmap into the memory DC
Dcmem. SelectObject (& BMP );
// Fill the customer area according to the original background, otherwise it will be black
Dcmem. fillsolidrect (rect, PDC-> getbkcolor ());

For (INT I = 20; I> 0; I --)
{
Ellipserect. setrect (ptcenter, ptcenter );
Ellipserect. inflaterect (I * 10, I * 10 );
// PDC-> ellipse (ellipserect );
Dcmem. ellipse (ellipserect );
}

// Copy the image from the memory DC to the foreground
PDC-> bitblt (0, 0, rect. Width (), rect. Height (), & dcmem, 0, 0, srccopy );
// Delete the DC
Dcmem. deletedc ();
// Delete a bitmap
BMP. deleteobject ();
}

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.