To show how graphics avoid flicker, how to improve display efficiency is a more problematic question. And most people think that the MFC drawing function is very inefficient, always looking for other solutions.
MFC's drawing efficiency is really not high but also not bad, and its drawing function is very simple, as long as the use of appropriate methods, coupled with some skills, MFC can be highly efficient drawing programs.
I would like to discuss some of my views on the experience of using MFC drawing for my long term (oh, of course, only 2 years).
1. Why does the displayed graphics flicker?
Most of our drawing process is in the OnDraw or OnPaint function, OnDraw is called by OnPaint when the screen is displayed. When the window needs to be redrawn for any reason, it always clears the display area with the background color before calling OnPaint, and the background color often contrasts with the drawing content, so that in a short period of time the background color appears alternately with the display graphic, making the display window appear flashing. If you set the background brush to NULL, this will not flash any redraw. Of course, this makes the display of the window mess, because there is no background color to erase the original drawing, and the new graphics are superimposed on the redraw. Some people will say that the flicker is because the speed of the drawing is too slow or the display of the graphics too complex caused, in fact, this is not true, the drawing of the speed of the display of the impact of flicker is not fundamental. For example, this is written in OnDraw (CDC *PDC):
Pdc->moveto (0,0);
Pdc->lineto (100,100);
The drawing process should be very simple, very fast, but the pull window changes will still see flashing. In fact, the more complicated the process of drawing, the less the flicker should be, because the time spent drawing with the background to clear the screen will be more noticeable. For example: clear screen time for 1s drawing time is also 1s, so in the 10s in the continuous redraw will be flashing 5 times, if the screen time is not changed to 1s, and the drawing time is 9s, so the continuous redraw in the 10s will only blink once. This can also be tested, inOnDraw(CDC *pDC)中这样写:
for(int i=0;i<100000;i++)
{
pDC->MoveTo(0,i);
pDC->LineTo(1000,i);
}
Oh, the procedure is a bit abnormal, but can explain the problem.
Speaking of which, there may be another saying, why does a simple graphic look like it doesn't have complex graphics? This is because of the large size of complex graphics, the contrast caused by heavy painting, so feel like to flash a bit more, but the frequency of flicker is low. Then why does the animation have a high frequency, but it does not look flash? Here, I'm going to emphasize again, what is flashing? Flashing is the contrast, the greater the contrast, the more powerful the flicker. Because the difference between two consecutive frames of the animation is so small that it does not appear to flash. If not, you can add a white frame in the middle of each frame of the animation, do not Flash is strange.
2. How to avoid flashing
After knowing the reasons for the flickering of the graphics, the right remedy is good. The first of course is to remove the background drawing process provided by MFC. There are a lot of ways to implement,
* You can pay NULL for the background brush of the registration class of the window when the window is formed
* Can also modify the background after the formation
Static CBrush Brush (RGB (255,0,0));
Setclasslong (This->m_hwnd,gcl_hbrbackground, (LONG) (hbrush) brush);
* To be simple You can also overload OnEraseBkgnd (cdc* PDC) to return true directly
This background does not, the results of the graphic display does not flash, but the display is also like the previous said, become a mess. What to do? This requires a dual caching approach. Double buffering is in addition to the on-screen display of graphics, in memory there are graphics in the drawing. We can draw the graphics that we want to display first in memory, and then once again to cover the graphics in memory at one point to the screen (this process is very fast, because it is a very regular memory copy). In this way, when drawing in memory, you can not flash with any contrasting background color, because you can't see it. When posted to the screen, because the final graphics in memory and screen display graphics are very small (if there is no movement, of course there is no difference), so that does not appear to flash.