This article address: http://www.jztop.com/dev/vc/2006-02-27/12460.html [click here copy address]
On the forum yesterday, I was asked about the implementation of Dual-buffering. I think this information on the internet is messy and mostly related to DirectX, today, I would like to briefly introduce the dual-buffer technology and its implementation in the visual ++ GDI drawing environment.
1. Windows plotting Principle
In Windows, we can see various elements, such as menus, buttons, windows, and images. Basically, they are all "painted. At this time, the screen is equivalent to a blackboard, and various GDI elements in windows, such as paint brushes and paint brushes, are equivalent to colored chalk. When we draw a picture manually on the blackboard, we draw a picture in one stroke, and the computer is also similar. However, the computer speed is much faster than the manual speed, so it seems that all the graphic text appears at the same time.
2. Limitations of common plotting
For the time being, we call the preceding plotting method a normal plotting method. Although this method can meet the needs of a considerable part of the drawing, the computer will not be able to do it when the object to be drawn is too complex, especially when it contains bitmap. At this time, the screen will show very slowly. For the motion picture, it will give people the feeling of being stuck. In short, one word: unhappy.
3. Solution: Dual Buffering
The principle of double buffering can be understood as follows: the computer screen is regarded as a blackboard. First, we create a "virtual" blackboard in the memory environment, and then draw complex graphics on this blackboard. When all the graphics are finished, copy the image drawn in the memory to another blackboard (screen) at one time. This method can increase the drawing speed and greatly improve the drawing effect. The following is a schematic diagram:
Figure 1 buffering Principle
4. Related functions
1) Create compatible memory DC for the screen DC: createcompatibledc ()
if(!m_dcMemory.CreateCompatibleDC(NULL)) // CDC m_dcMemory; { ::PostQuitMessage(0); }
2) create a bitmap: createcompatiblebitmap ()
m_Bmp.CreateCompatibleBitmap(&m_dcMemory, rt.Width(), rt.Height()); // CBitmap m_Bmp;
3) Select the bitmap to the device environment: SelectObject (), which can be understood as the canvas selection.
::SelectObject(m_dcMemory.GetSafeHdc(), m_Bmp);
4) copy the drawn image to the screen: bitblt ()
pdcView->BitBlt(0, 0, rt.Width(), rt.Height(), &m_dcMemory, 0, 0, SRCCOPY);
For details about the Function usage, see msdn. I have repeated one sentence multiple times. It's okay to say it again: msdn is the best teacher.
5. This article provides an example to describe the limitations of the common plotting method and the advantages of double buffering technology by comparing the effects.
This example draws a lot of radius gradient circles on a view. You can pay attention to the animation effects in two different drawing modes:
[EDIT: youjia] [back to the school homepage]