Windows API-GDI BASICS (1)

Source: Internet
Author: User

What is GDI?

GDI is the abbreviation of graphics device interface. It refers to the graphic device interface. Its main task is to take charge of the system and drawing.ProgramInformation exchange between them to process the graphic output of all Windows programs.

In Windows, most applications with graphical interfaces are inseparable from GDI. With the many functions provided by GDI, We can conveniently output graphics on screens, printers, and other output devices, text and other operations. With the advent of GDI, programmers can convert the output of applications into outputs on hardware devices without having to care about hardware devices and device drivers, thus realizing the isolation between program developers and hardware devices, this greatly facilitates development.

How is GDI output?

To output graphics or text on the screen or other output devices, we must first obtain the handle of an object called the device description table (DC: device context), taking it as a parameter, call a variety of GDI functions to achieve the output of various text or graphics.
The device description table is a data structure in which GDI stores data. The property content in this structure is related to a specific output device (display, printer, and so on, the attribute defines the working details of the GDI function. Later we will see how to use the textout function to output text. Here, the attribute determines the color of the text, how the X and Y coordinates map to the display area of the window.

Once the device description table handle is obtained, the system uses the default property value to fill the device description table structure.

If necessary, we can use some GDI functions to obtain and change the property values in the device description table.

What is a valid rectangle and what is an invalid rectangle?

When the application receives the wm_paint message, it is usually ready to update the main display area, but usually only needs to update a small area instead of the entire area, this usually occurs when a part of the application's main window is overwritten by a dialog box, and only the covered rectangular area needs to be re-painted (SEE ).

Download EXE sample program: Click here to download (90 K, WinZip compressed file)

The following part of the shadow is the rectangular area to be updated. This area is what we call an invalid area. It is because of the existence of this area that the system will put a wm_paint message into the message queue.

In Windows, A paintstruct structure is reserved for each window, which contains the coordinates of the smallest rectangle surrounding the invalid area and some other information, note that when the window message processing function displays another invalid region before processing the wm_paint message, Windows calculates a new invalid region that contains two invalid regions, save the changes in the paintstruct structure. Windows does not store multiple wm_paint messages in the message queue at the same time.

The window message processing function calls the invalidaterect function to make the rectangle in the window display area invalid. If a wm_paint message already exists in the message queue, Windows calculates a new invalid rectangle, when a wm_paint message is received, the window message processing function obtains the coordinates of invalid rectangles. By calling getupdaterect, the coordinates can be obtained at any time.

How do I obtain or release the device description table handle?

When an application needs to draw a table, it must first obtain the device description table handle. After the Drawing operation ends, it must release the device description table handle. You can obtain and release the device description table handle in two ways.

1. Use the beginpaint and endpaint Functions

Generally, when the application receives the wm_paint message, that is, when it needs to update the display area of the window, it calls the beginpaint function to obtain the device description table handle, call the endpaint function to release the device description table handle.

Their function prototype is:

HDC beginpaint (
Hwnd Hwnd , , // Handle to window
Lppaintstruct Ppaint  // Paint Information
);
Bool endpaint (
Hwnd Hwnd , // Handle to window
Const paintstruct * Ppaint   // Paint data
);

From the original form of the beginpaint function above, we can see the memory address of a paintstruct structure object. The paintstruct structure is included in the winuser. h header file.

Definition:

Typedef struct tagpaintstruct {
HDC; // Device description table handle
Bool ferase; // Erased status
Rect rcpaint; // Invalid rectangleCoordinates
Bool frestore;
Bool fincupdate;
Byte rgbreserved [32];
}
Paintstruct, * ppaintstruct, * nppaintstruct, * lppaintstruct;

In fact, when the program calls the beginpaint function, Windows will automatically fill in the attributes of this structure, and the author only needs to care about the first three attributes.

The first HDC attribute indicates the current device description table handle.

In ferase, the second attribute is marked as false (0) In most cases, which indicates that Windows has wiped the background of the invalid rectangle, this erasure action occurs in the beginpaint function, while the paint brush used to erase the background is the paint brush specified by the hbrbackground attribute in the wndclass structure to erase the background, in many cases, the program author may want to define some insertion/removal behaviors by themselves, so it can be done by responding to the wm_erasebkgnd message in the message queue.

The third attribute rcpaint indicates the coordinate of the invalid rectangle, which defines the boundary of the invalid rectangle.

The rect structure can be found in the windef. h header file.

Definition:

Typedef struct tagrect
{
Long left;
Long top;
Long right;
Long bottom;
} Rect, * prect, near * nprect, far * lprect;

Note: we mentioned the invalidaterect function before, and it is clear that calling it can make the rectangle in the display area of the window invalid, therefore, we can call wm_paint to plot the image outside the invalid rectangle when processing the wm_paint message. This call is called before the begingpaint function is called.

Usage:

Invalidaterect (hwnd, null, true );

Through the aboveCodeTo make the entire display area invalid and erase the background, note that if the last parameter is false, the background will not be erased, and the original content will be kept in the original position, this is usually the most convenient way to simply re-paint the entire display area when receiving the wm_paint message without considering the rcpaint attribute. For example, in the display area, we output a graph. A small part of the graph falls into the invalid rectangle area, which makes it meaningless to draw the invalid part of the graph, in this case, you need to re-paint the entire image, because when you call the beginpaint function to return the device description table handle, Windows will not draw rcpaint, that is, any part other than the invalid rectangle.

For a detailed example of the invalidaterect function, we will see it later.

2. Use the getdc and releasedc Functions

In many cases, we may need to obtain the device description table handle when receiving a non-wm_paint message. by calling the getdc function, we can obtain the device description table handle, because the program author may want to use the device description table handle to complete other work, such as obtaining the properties of the device description table or modifying the properties of the device description table, in the end, we need to release the handle like the first method and call the releasedc function to complete the work.

Their function prototype is:

HDC getdc (
HwndHwnd // Handle to window
);
Int releasedc (
HwndHwnd, // Handle to window
HDCHDC // Handle to DC
);

Differences between the two methods:

<1> the operation area obtained by using the beginpaint function is the invalid rectangular area in the display area. Next, the drawing operation can only be performed within the invalid area of the window, areas other than the invalid area are ignored and cannot be operated. The operation area obtained by the getdc function is the display area of the entire window. Subsequent operations can be performed in any part, it is not limited to invalid regions.

<2> the beginpaint function automatically changes the invalid region to a valid region, while the getdc function does not make any invalid region valid. The validaterect function must be forcibly called, set the second parameter to null.

Finally, we provide an example of an executable program. When the application is executed, a dialog box appears. When you pull this dialog box, the same dialog box appears, this proves that the window Overwrite will cause invalid rectangles, and the system will send the wm_paint message.

To be continued ......

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.