GDI Usage Experience Summary

Source: Internet
Author: User

I because of the work, the study of GDI more, the following is the use of some of my experience in the summary, I hope you have problems point of view, if you can have a little help for beginners, I would be considered not white write this article!

Windows Internal for the operation of the GDI assigned a very high authority, but want to do a good graphical interface, and the use of less resources of the graphical interface, or to make a few efforts, the following I summed up a few:

One: Double buffering, which is the ear to hear the calluses of something, which, primarily, is to build memory-compatible DC and memory-compatible bitmaps, as follows: HDC HDC =:: GetDC (M_hwnd); Hfrcedc = CreateCompatibleDC (HDC); Memory compatible DC hfrcebmp = CreateCompatibleBitmap (HDC, Winwidth, winheight); Memory compatible bitmap SelectObject (Hfrcedc, hfrcebmp); Select INTO memory compatible DC:: ReleaseDC (M_hwnd, HDC);

All subsequent GDI operations, such as LineTo, are only for HFRCEDC when it is time to refresh: void Cst_curvectrl::ondraw (cdc* PDC, const crect& rcBounds, const CRect & Rcinvalid) {BitBlt (PDC->M_HDC, Rcinvalid.left, Rcinvalid.top, Rcinvalid.width (), Rcinvalid.height (), HFrceDC , Rcinvalid.left, Rcinvalid.top, srccopy); ... }

Simple, that's why double buffering is the most basic technique in GDI programming. This way no matter how complex your drawing is, the screen will no longer blink and the complexity of the drawing will only take up a bit more CPU.

Two: Refresh the area is very important, take the above OnDraw function, do not go to refresh the Rcbounds area, it is the entire customer area, but should refresh rcinvalid, it is invalid area. If the Rcbounds region refreshes, of course, the effect is no different, but will occupy more CPU.

Three: It is important to refresh the background, some novice to use the above double buffering on the dialog box, and then tell me, or flashing! That's because the background was brushed, because something that was drawn with BitBlt, in Windows, is not a window, it's not like a button, because the button is a window, so when Windows brushes the background, it will remove the area of the button from the Refresh area, then back to the point, When BitBlt on a dialog box, if the bitmap display fills the entire client area of the dialog box, it is simple to respond directly to the WM_ERASEBKGND message, and then return True:bool cxxx::onerasebkgnd (cdc* PDC) {return TRUE; If BitBlt does not fill the entire client area, then I'm sorry, it's a bit of a hassle, you're going to generate an area that doesn't include the BitBlt display area, and then execute the SELECTCLIPRGN function on the DC, search for "Region Functions" in MSDN, Most of the functions inside of you may be used, mainly to create areas, operating areas (such as seeking and, seeking or, for diff, etc.).

Four: The number of GDI function calls is important, with double buffering and elimination of refreshing the background, flashing problems can be considered to be resolved, the following we will discuss the problem of reducing CPU usage. GDI operation is a waste of CPU resources, such as the frequency of the call GDI function, it is bound to waste CPU resources, which in particular the character of the printing operation, for this GDI provides polytextout function, call it once, can output any number of characters, each of which is independently set the output location, The use of this function I give an example, such as you are drawing a coordinate on the scale value, then this function is no better. Similar to this function are PolylineTo, Polyline, Polypolyline, Polygon, Polypolygon, and so on.

Five:

Note the ExtTextOut function, which appears on the surface to display a string, but it can also be used to draw a filled rectangle, without borders, acting like a fillsolidrect, in which case, Do not use the rectangle function (choose an empty brush can also achieve a borderless effect), ExtTextOut function fastest I can find evidence, see MFC Source: Void Cdc::fillsolidrect (Lpcrect lpRect, COLORREF CLR) {assert_valid (this); ASSERT (m_hDC! = NULL);

:: SetBkColor (m_hDC, CLR); :: ExtTextOut (m_hdc, 0, 0, eto_opaque, lpRect, NULL, 0, NULL); } inside the wingdix.cpp.

Six: Regional refresh technology, this technology I do not know that everyone is called, if not called, is I give it to the name, its thinking is, the customer area is divided into several areas (generally rectangular on the line, speed), specific how to divide, the number of inconclusive, theoretically, may need to refresh the place alone, It is divided into one area, and each time the area needs to be refreshed, only the area is drawn and bitblt to the screen. Specifically, each zone should correspond to a refresh function (or a case entry for a switch statement), which performs the GDI operation, and when the area needs to be refreshed, call this function to draw something new to a memory-compatible DC, and then invalidate the rectangle. Then Windows will call OnDraw (or similar function) at the appropriate time, and in OnDraw, use BitBlt to map the invalid area (that is, the rectangular area previously mentioned).

This technique is used more in my st_curve, here is one, more details please see my homepage:

www.st-curve.cn

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

GDI Usage Experience Summary

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.