How to obtain the handle of the device description table-Win32 API

Source: Internet
Author: User


Windows provides several methods to obtain the device description table handle. If you obtain the device description table handle when processing a message, you should release it before exiting the window function. Once the handle is released, it is no longer valid.

1. The most common method to obtain and release the device description table handle is to call beginpaint and endpaint when processing the wm_paint message:

hdc = BeginPaint(hwnd,&ps);//other program linesEndPaint(hwnd,&ps);

The variable PS is a structure of the paintstruct type. The HDC field of this structure is the device description table handle returned by beginpaint.

The paintstruct structure contains a rect structure named rcpaint. rcpaint defines a rectangle that contains an invalid range in the customer area of the window. The device description table handle obtained from beginpaint can be drawn only in this area. The beginpaint call makes the region valid.


2. The windows program can also obtain the handle of the device description table when processing non-wm_paint messages:

hdc = GetDC(hwnd);//other program linesReleaseDC(hwnd,hdc);

This device description table is used in the customer zone where the window handle is hwnd. The basic difference between these calls and the combination of beginpaint and endpaint is that the use of the handle returned from getdc can draw on the entire customer zone. Of course, getdc and releasedc do not make any possible invalid areas in the customer zone valid.


3. The windows program can also obtain the device description handle for the entire window (not limited to the client area of the window:

hdc = GetWindowDC(hwnd);//other program linesReleaseDC(hwnd,hdc);

In addition to the customer area, this device description table also includes the title bar, menu, scroll bar, and frame of the window. The getwindowdc function is rarely used. If you want to use it, you must capture the wm_ncpaint (non-customer drawing) message. Windows uses this message to draw on the non-customer area of the window. We can see that most online games use the device description table handle for personalized production.


The device descriptions obtained by beginpaint, getdc, and getwindowdc are related to a specific window on the video display. Createdc:

hdc = CreateDC(pszDriver,pszDevice,pszOutput,pData);//other program linesDeleteDC(hdc);

For example, you can call the following to obtain the device description table handle for the entire screen:

hdc = CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);

Sometimes, you only need to obtain information about a device description table without drawing any data. In this case, you can use createic to obtain a "Information Description table" handle, its parameters are the same as those of the createdc function, with the following columns:

hdc = CreateIC(TEXT("DISPLAY"),NULL,NULL,NULL);

You cannot use the information environment handle to write data to the device.
When bitmap is used, obtaining a memory device description table is sometimes useful:

hdcMem = CreateCompatibleDC(hdc);//other program linesDeleteDC(hdcMem);

This function can be used to select the bitmap into the memory device description table, and then use the GDI function to draw on the in-place graph.
You can also obtain the metadata file device description table to create the metadata file:

hdcMeta = CreateMetaFile(pszFiename);//other program lineshmf = CloseMetaFile(hdcMeta);

During the validity period of the Meta File device description table, any GDI call made with hdcmeta is part of the Meta File and will not be displayed. After closemetafile is called, the handle of the device description table becomes invalid. The function returns a handle pointing to the metadata file (hmf.

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.