Interface flash in Windows User Interface Programming

Source: Internet
Author: User
Tags bmp image

(Xdesigner studio is copyrighted. For more information, see the source)
In Windows Graphical User Interface Programming, ifProgramWhen you draw a user interface by yourself, the interface will often flash. For example, if other windows are moved above and the user interface is scrolling, these may cause flickering. To draw a specific document in a container, you must use the onpaint event as the control of the drawing container, and re-draw the document in the onpaint event processing, in Windows, the onpaint event is triggered in two cases: the container control is overwritten by other forms and then displayed, and the Container Control is rolled. In these cases, the Windows operating system will frequently trigger the onpaint event, and the application will frequently re-draw the image in the drawing container. If the application is not well optimized, the user interface is likely to flash.

There are a lot of natural harm to the user interface flickering. First, it makes your program look unprofessional and even suspected of being unstable. You will allow it to exist in the pursuit of perfection; second, flashing will damage your eyesight and lead to visual fatigue.

Okay, I will not talk much about it. We will find the problem, analyze the problem, and solve the problem.

First of all, let's talk about the essence of the Flash. When it comes to the essence, we have to mention some knowledge about the computer system structure and Windows graphic user subsystem. We know that there is an area in the computer memory called video memory, while the video card scans the video memory every several milliseconds, and then draws pixels Based on the operating display, therefore, the content displayed on the monitor is reset every few milliseconds. This is a hardware operation and is very fast. If the screen content does not change, the human eye cannot see this refresh, at this time, the user interface does not flash.

The figure on the right shows how the application draws the user interface. The application fills data in the display memory with the CPU support, and the video card also loads data from the display to draw the image, (If the application can directly access the display, the rendering speed should not be too fast), and the flickering user interface is rooted in this display structure. As mentioned above, the video card scans the video memory every several milliseconds to refresh the display of the display. If a display card is set to refresh at a frequency of 50Hz, It scans the Display memory to refresh the display every 20 milliseconds, and there is no relationship between the operation of the video card and the operation of the application, the video card comes with a processor, so the application and the video card operate the video memory at the same time. The video card only reads the video memory, while the application modifies the video memory, this leads to data synchronization issues similar to multi-threaded programs. However, the hardware structure determines that there is no locking mechanism available at this time. The video card is refreshed every 20 ms, and the operating system cannot be blocked, and the application does not know when the video card will be refreshed.

At a certain time point, the application needs to draw the user interface. First, it needs to clear the drawing container, so it sets a large area of the display to white, and the application has just completed the clearing operation, every time you have time to draw the content, the video card is refreshed, and soon a large white area is displayed on the display. At the same time, the application starts to draw content, and the application runs slowly. It draws the upper half of the document in 20 milliseconds. the upper half of the document is mainly red, and the upper half is just drawn, the video card is refreshed relentlessly, and soon half of the documents are displayed on the display. Half of the white color is red. In this case, the display turns white, then, half of the display changes to Red. At this time, the content of the display changes significantly twice, and then the application shows the lower body of the document for 20 milliseconds. The lower body of the document is mainly green, at this time, the video card is refreshed, and the remaining white on the other half of the display turns green. As the application draws a document, the display is no longer modified, and the display content of the monitor is no longer changed.

In the above description, the display first changes from the yellow green to a piece of white, the half changes to red after 20 milliseconds, and the other half changes to green after 20 milliseconds, short-term changes to such a large area of display content are called flickering. At this time, computer displays are suffering users' eyes.

In fact, in a broad sense, the frequent and massive changes in the content displayed by computer monitors are a test of human eyes. Some people play some intense 3D games, such as Raytheon CS. If they play for a long time, they will not be able to stand it. This is because these game programs frequently change the display content. Therefore, the game program causes the user interface to flash. However, this flickering is not uncomfortable.

Through the above discussion, we learned the root cause of the flash, so we found the problem, analyzed the problem, and now solved the problem. In the current computer structure, our applications can only draw user interfaces by filling in the video memory. In this case, the best way to deal with flickering is fast, the application needs to modify the video memory very quickly. For graphics cards that refresh every 20 milliseconds, if our application can modify the video memory within 20 milliseconds, in many cases, the video card will only change the display content of the display, reduce flickering, and compare the data before and after the application fills in the display memory, if the number of inconsistent bytes is smaller, the smaller the number of pixel prime numbers that change the color before and after the refresh operation in the monitor, the smaller the user interface flashes.

There are many ways to increase the speed of modifying the video memory of an application, and the Windows operating system has done a lot of underlying operations for us. We know that if a form is overwritten and displayed again, the Windows operating system will send a re-drawing message to the form and transmit a rectangular data, this rectangle indicates the area in the form that needs to be refreshed. The application can redraw a part of the document based on this rectangle. This eliminates the need to draw all the content, increase the painting speed, and reduce the Painting time, this requires plotting.Code.

In some cases, it is difficult to optimize the drawing speed. In this case, the so-called "Double Buffering" technology can be used to reduce flickering, applications can create a screen-compatible graphical device context in the memory. This context actually processes a BMP image object stored in the memory, allowing the application to draw images on this BMP slowly. After the application is drawn, use the WIN32API function bitblt to fill the BMP image into the video memory. The bitblt function is fast enough to complete the drawing within the screen refresh cycle, in this way, you can basically avoid blinking. However, dual-buffering also has disadvantages. First, it increases program complexity, and second, it consumes some memory. In addition, it makes the entire drawing longer, resulting in a "User Experience" in scrolling ", there is "inertia", in addition, it also makes people lazy and do not pay attention to the optimization of drawing operations.

As the saying goes, capacity is hard to achieve, and programming is the same. In this article, Flash is lightweight, but in actual programming, Flash has always been a hard problem in graphic user interface programming, you need to carefully design the program structure, optimize the code, and increase the speed. This requires a lot of theoretical knowledge and profound programming skills, which requires long-term programming practices.

Related Article

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.