Double buffering technology drawing

Source: Internet
Author: User

When the data size is large, drawing may take several seconds or even longer, and sometimes flashes. To solve these problems, double buffering technology can be used for drawing.

Double Buffering creates an object in the memory that is consistent with the drawing area on the screen. First, the image is drawn to this object in the memory, and then the image on this object is copied to the screen at one time, this greatly accelerates the drawing speed. The dual-buffer implementation process is as follows:

1. Create a buffer consistent with the canvas in the memory

2. drawing in the buffer zone

3. Copy the buffer bitmap to the current canvas.

4. Release the memory buffer

In image processing and programming, double buffering is a basic technology. We know that if the form needs to perform complex graphic processing when responding to the wm_paint message, the form will flash due to frequent refresh during repainting. The effective solution to this problem is the dual-buffer technology. When the form is refreshed, there is always an onerasebkgnd process to erase the original image. It uses the background color to fill the form drawing area, and then calls the new drawing code for re-painting, the image color contrast is caused by such a wipe. When wm_paint responds frequently, this contrast becomes more and more obvious. So we can see the flickering phenomenon.

We will naturally think that it is the most direct way to avoid filling the background color. But in that case, the form will become a mess. Because the original image is not cleared every time the image is drawn
As a result, the screen is often messy when the screen weight is painted. Therefore, it is not enough to prohibit background re-painting. We had to re-plot it, but it was fast, so we thought of using the bitblt function. It supports the copying of graphic blocks at a fast speed. We can plot the image in the memory first, then use this function to copy the completed graph to the foreground, and disable background refresh. This eliminates the flash swap. The above is the basic idea of Double Buffer plotting.

The implementation program is provided first, and then explained again, also in ondraw (CDC
* PDC:

CDC
Memdc; // first define a display device object

Cbitmap
Membitmap; // defines a bitmap object. // then, a memory display device compatible with screen display is created.

Memdc. createcompatibledc (null); // drawing is not allowed yet, because there is no place to draw
Pai_^

// A bitmap compatible with screen display is created below. The size of the bitmap can be defined by the size of the window.

(For example, if a scroll bar exists, it must be larger than the current window size. When bitblt is used, it determines which part of the memory is copied to the screen)

Membitmap. createcompatiblebitmap (PDC, nwidth, nheight); // select the bitmap to the memory display device. // only the memory display device with the bitmap selected has a place to draw and draw to the specified bitmap.

Cbitmap
* Poldbit = memdc. SelectObject (& membitmap); // first clear the bitmap with the background color. Here I use white as the background color. // you can also use your own color.

Memdc. fillsolidrect (255,255,255, nwidth, nheight, RGB (); // plot

Memdc. moveTo (......); Memdc. lineto (......); // Copy the image in the memory to the screen for display

PDC-> bitblt (, nwidth, nheight, & memdc, srccopy); // clean up after the drawing is complete // select the previous poldbit. remove membitmap from the device before deleting it.

Memdc. SelectObject (poldbit); membitmap. deleteobject (); memdc. deletedc ();Double Buffer (two
Way soft-closing)

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.