The principle of double buffering can be understood as follows: the computer screen is regarded as a blackboard. First, we will create a "virtual" blackboard in the memory environment, and then draw complex graphics on this blackboard.
When the preparation is complete, 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. Can understand this layer, work
It is easy to do. You can do it in just four steps. See the following code:
For example, the ondraw () function can implement dual buffering as described below. The main steps are as follows:
Rong:
Cpen pen;
Pen. createpen (ps_insideframe, 1, RGB (225,225, 0 ));
Cbrush
Brush; brush. createsolidbrush (RGB (225,225, 0 ));
CDC dcmem;
Cbitmap
BM;
Crect RC;
Getclientrect (& rc );
// Step 1: Create a compatible memory DC for the screen DC:
Createcompatibledc () dcmem. createcompatibledc (PDC );
//
Step 2
Create a bitmap:
Createcompatiblebitmap () BM. createcompatiblebitmap (PDC, RC. Width (), RC. Height ());
//
Step 3: select the bitmap to the device environment:
SelectObject (),
It can be understood as selecting a canvas
Dcmem. SelectObject (& BM );
Dcmem. SelectObject (PEN );
Dcmem. SelectObject (Brush );
Dcmem. ellipse (0, 0, 50, 50 );//
Draw an ellipse
// Step 4: copy the drawn image to the screen: bitblt ()
PDC-> bitblt (0, 0, RC. Width (), RC. Height (), & dcmem, 0, srccopy );
Dcmem. deletedc ();
BM. deleteobject ();
In this way, dual buffering is implemented. This method can be used to prevent screen flickering when drawing in VC.