When drawing with VC ++, dual buffering is used to solve the problem of screen flashing and reprinting.

Source: Internet
Author: User

When drawing with VC ++, dual buffering is used to solve the problem of screen flashing and reprinting.

I recently encountered some problems when I was playing Chinese chess. During the drawing process, the screen blinks and I guess I would think of using double buffering to solve the problem. But I checked the information about double buffering on the Internet, it is found that it is basically MFC. After converting it to VC ++, the approximate code is as follows:

Void DrawBmp (HDC hDC, HBITMAP hBitmap) {HDC hdcMEM; // The memory dc hbitmap bmp used to buffer the plot; // The bitmap HANDLE hOld that carries the temporary image in the memory; hdcMEM = CreateCompatibleDC (hDC); // The attached window DC creates compatible memory DC bmp = CreateCompatibleBitmap (hDC, 100,100 ); // create a bitmap SelectObject (hdcMEM, bmp) that is compatible with devices related to the hDC environment; hOld = SelectObject (hdcImage, hBitmap); StretchBlt (hDC, 0, 0,100,100, hdcMEM, 0, 0,100,100, SRCCOPY); SelectObject (hdcImage, hOld); DeleteObject (hOld );}

 

However, the above Code does not seem to use hBitmap, and of course there will be no output on the screen, but the information on the Internet is basically the same. After checking the information, we can see that if the hDC already contains bitmap data, BitBlt will directly draw the data in the hDC to the memory buffer. Therefore, you also need to create a DC named hdcImage, select the bitmap to be drawn into the memory hdcImage, and then draw on the memory buffer.

The Code is as follows:

Void DrawBmp (HDC hDC, HBITMAP hBitmap) {HDC hdcImage; HDC hdcMEM; // note that two HDC hdcMEM = CreateCompatibleDC (hDC) are created here; hdcImage = CreateCompatibleDC (hDC ); HBITMAP bmp =: CreateCompatibleBitmap (hDC, nWidth, nHeight); // create a bitmap SelectObject (hdcMEM, bmp) that is compatible with devices related to the hDC environment; SelectObject (hdcImage, hBitmap ); // note that the bitmap to be drawn is selected as hdcImage StretchBlt (hdcMEM, 0, 0,100,100, hdcImage, 0, 0,100,100, SRCCOPY). // the image can be drawn normally, copy the bitmap in hdcImage directly to the memory buffer zone StretchBlt (hDC, 0, 0,100,100, hdcMEM, 0, 0,100,100, SRCCOPY); // then draw the data in the memory buffer to the screen. deleteObject (hdcImage );}

 

Of course, note that if you want to draw multiple images, for example, two images, you can call the following method:
DrawBmp (hDC, hBitmap1 );
DrawBmp (hDC, hBitmap2 );
The flash will still occur. The reason is as follows:
For example, screen plotting is like painting on the scene. If you call the plotting function twice, it is equivalent to painting in front of the audience. The first painting is the first one (such as the background of Chinese chess ). Draw the second picture (for example, a chessboard) for the second time ). In this way, when painting the background and the board, because of the color contrast, it is inevitable that the second picture will find a flash, so that the use of double buffering is useless, but also a waste of memory space.

The principle of dual-buffering is: first draw the first picture in the memory. At this time, do not transfer the picture to the screen, and then continue to draw the second picture in the original memory, after drawing all the images, paste them to the screen at one time. In this way, the entire graph exists in the memory. The viewer can only see the drawing result without looking at the drawing process. The last result is a one-time copy to the screen. Of course, the flash will not happen.

To better explain the principles of double buffering, see the following picture:

 

PS: The above photos are from the internet. I have no intention of infringement for better understanding.

Make the following changes based on the above Code:

Void DrawBmp (HDC hDC, HBITMAP hBitmap) // here, the returned type is changed to HDC {HDC hdcMEM; hdcMEM = CreateCompatibleDC (hDC); SelectObject (hdcMEM, hBitmap ); // select the bitmap into the memory DC StretchBlt (hDC, 0, 0,100,100, hdcMEM, 0, 0,100,100, SRCCOPY); // you can draw a picture normally here, copy the bitmap in hdcImage directly to the memory buffer DeleteObject (hdcMEM );}

 

Call the above function to draw the first image in the memory:
DrawBmp (hdcTmp, hBitmap1 );
Draw the second image
DrawBmp (hdcTmp, hBitmap2); // at this time, hdcTmp is passed. hdcTmp contains the data of the first image, after this call, the second image will be painted to the original base.

If you want to draw multiple images, call this function to draw them one by one. Remember to draw all the images to a device DC and then draw them to the screen one by one.
After you have completely painted all the images in hdcTmp, hdcTmp already has a complete image and then draws the complete image on the screen:
BitBlt (hDC, 0, 0,100,100, hdcTmp, 0, 0, SRCCOPY); // The first parameter is hDC, that is, the window handle.

So far, the dual-buffer painting has been completed.

 

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.