Bitblt () Dual buffering to solve image flickering)

Source: Internet
Author: User
There are many questions about how to avoid flickering and how to improve the display efficiency. Most people think that the efficiency of the MFC plot function is very low, and they always want to seek other solutions.
The drawing efficiency of MFC is indeed not high, but it is not bad, and its drawing function is very simple to use, as long as the use method is appropriate, plus some skills, using MFC can get a very efficient drawing Program .
I would like to share some of my views on my long-term experience in using MFC plotting (of course, I only have more than two years.

1. Why does the displayed image flash?
Me
Most of their drawing processes are stored in the ondraw or onpaint functions. ondraw is called by onpaint for screen display. When the window needs to be re-painted for any reason
Always use the background color to clear the display area and then call onpaint. The background color often has a large contrast with the drawing content, so that the background color and the display image alternate in a short time, display window
It looks like a flash. If you set the background to null, the duplicate drawing will not flash. Of course, this will make the display of the window messy, because there is no background color for the original graph during re-painting.
And overlay the new image. Some people may say that the blinking is caused by the drawing speed being too slow or the display graphics being too complex. In fact, this is not true. The display speed of the drawing has no effect on the flashing speed.
Is fundamental. For example, in ondraw (CDC * PDC), write as follows:
PDC-> moveTo (0, 0 );
PDC-> lineto (100,100 );
This
Drawing Process should be very simple, very fast, but will still see flashing when pulling the window changes. In fact, in principle, the more complex the drawing process, the slower the drawing process, the less flickering, because the time used for drawing and the back
The larger the percentage of time taken to clear the screen, the less obvious a person will feel. For example, if the screen time is 1 s, the drawing time is also 1 s, so that the continuous re-painting within 10 s will flash 5 times; if
The screen time is 1 s while the drawing time is 9 s, so that the continuous re-painting within 10 s will only Flash once. This can also be tested by writing in ondraw (CDC * PDC) as follows:
For (INT I = 0; I <100000; I ++)
{
PDC-> moveTo (0, I );
PDC-> lineto (1000, I );
}
The program is abnormal, but the problem can be explained.
Description
Some people may want to talk about it again. Why does a simple image look as if it is not as complex as it is? This is because the area occupied by complex images is large, and the contrast caused by the re-painting is relatively large, so I feel like I have to flash a lot.
But the flicker frequency is low. So why does the animation appear non-flashing when its re-painting frequency is high? Here, I want to emphasize again, what is flashing? Flashing means contrast. The larger the contrast, the more powerful the blinking. Because Animation
The difference between two consecutive frames is very small, so it does not seem to flash. If you don't believe it, you can add a pure white frame in the middle of each frame of the animation.

2. How to Avoid blinking
After you know why the image flash, you can do the right thing. The first step is to remove the background rendering process provided by MFC. There are many implementation methods,
* When the window is formed, you can pay NULL for the registration background of the window.
* You can also modify the background after the formation.
Static cbrush brush (# ff0000 );
Setclasslong (this-> m_hwnd, gcl_hbrbackground, (long) (hbrush) brush );
* You can also load onerasebkgnd (CDC * PDC) to return true directly.
This
The background is no longer displayed, and the result graphic display is indeed not flashing, but the display is also messy as mentioned above. What should I do? This requires the dual-Cache method. In addition to the image on the screen, dual Buffering
In addition to the display, there are also images in the memory. We can plot the image to be displayed in the memory first, and then overwrite the image in the memory to the screen one by one point at a time (this process
Very fast, because it is a very regular copy of memory ). In this way, when drawing in the memory, any background color with a large contrast will not flash, because it cannot be seen. When pasted to the screen, because the final graph in the memory
There is little difference between the shape and the screen display image (if there is no motion, of course there is no difference), so that it does not seem to flash.

3. How to implement dual Buffering
The implementation program is provided first, and then explained again, also in ondraw (CDC * PDC:

CDC memdc; // define a display device object
Cbitmap membitmap; // defines a bitmap object.

// Create a memory display device compatible with Screen Display
Memdc. createcompatibledc (null );
// No drawing yet, because there is no place to draw ^_^
// A bitmap compatible with screen display is created below. The size of the bitmap can be set to the size of the window.
Membitmap. createcompatiblebitmap (PDC, nwidth, nheight );

// Select the bitmap to the memory display device.
// Only the memory display device with the bitmap selected can draw a local image and draw it to the specified bitmap.
Cbitmap * poldbit = memdc. SelectObject (& membitmap );

// Use the background color to clear the bitmap. Here, I use white as the background color.
// You can also use your own color
Memdc. fillsolidrect (0, 0, nwidth, nheight, # ffffff );

// Drawing
Memdc. moveTo (......);
Memdc. lineto (......);

// Copy the image in the memory to the screen for display
PDC-> bitblt (0, 0, nwidth, nheight, & memdc, 0, srccopy );

// Cleanup after drawing is complete
Membitmap. deleteobject ();
Memdc. deletedc ();

The above comment should be very detailed, so I will not talk much about it.

4. How to Improve the drawing efficiency
I
mainly used CAD software for the Network Graphics of the power system, thousands of Power elements are usually displayed in a window, and each element is composed of basic figures such as points, lines, and circles. If you really want to re-draw so many elements in a re-painting
process, it can be imagined that this process is very long. If you have added the Image Browsing function, you need to re-paint a lot when you move the mouse to scroll the image. The speed will be too slow for users to endure. How can I
do it? We only need to study the Drawing Process of MFC.
in fact, not all charts drawn in ondraw (CDC
* PDC) are displayed. For example, you have drawn two rectangles in ondraw, although both the drawing functions of the two rectangles are executed in one re-painting, it is likely that only one
is displayed, because MFC sets the cropping area to improve the re-painting efficiency. The purpose of the cropping area is: only the plotting process in this area will be truly valid, and those outside the area will be invalid, the drawing function is not displayed even if the drawing function is
displayed outside the zone. In most cases, the re-painting of a window is mostly due to partial occlusion of the window or scrolling of the window. The changed area is not the whole graph but only a small part, the part of this
must be changed is the cropping area in the PDC. Because the display (display to memory or video memory) is much more time-consuming than the calculation of the drawing process. With the cropping area, the display is only the part that should be displayed, greatly
improved display efficiency. However, this cropping area is set by MFC, which has improved the display efficiency. How can we further improve the efficiency when drawing complex images? Then, you only need to remove the painting outside the cropping area
. You can first use PDC-> getclipbox () to obtain the cropping area, and then determine whether your image is in this area during the drawing. If you are painting it, you will not draw it if you are not.
if your drawing process is not complex, this may not improve your drawing efficiency.

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.