In Chapter 3 we have used the Device Environment handle to plot in the window client area. To draw a graph on a graphics output device (such as a screen or printer), you must first obtain the handle of the device environment, i.e., DC. When Windows hands over this handle to your application, Windows also gives you permission to use the device. Next, use this handle as a parameter in the GDI function to tell Windows which device to draw on.
What you need to know in this section:
Get the device environment handle
Obtain device environment information
Exercise 22: Obtain equipment environment information
The device environment contains a number of properties that determine how the GDI function works. These properties allow the GDI function to provide only a few parameters (such as the starting coordinates) without providing all the information that Windows needs to display the object on the device. For example, when you call the textout function, you only need to specify the device environment handle, start coordinates, text, and the length of the text in the function, and you do not need to specify the font, the color of the text, the color of the text background, or the character spacing. All of these properties are part of the device environment. When you want to change these properties, you can call a function to execute them, and then the textout function will use the properties of the new device environment.
Get it in the wm paint message
Use the beginpaint function and the endpaint function when processing wm paint messages
hdc = beginpaint (hwnd, &ps) ;
Other stroke type.
endpaint (hwnd, &ps) ;
Get it in other messages
hdc = getdc (hwnd) ;
Other stroke type.
releasedc (hwnd, hdc) ;
The main differences between the endpaint function combination and the getdc function combination are:
The endpaint function gets the handle to the device environment in the invalid area.
The getdc function returns a handle to the device environment for the entire window client area. And the getdc and releasedc functions do not make any invalid areas of the client zone valid.
Get the screen device environment handle
hdc = createdc (pszdriver, pszdevice, pszoutput, pdata) ;
Other stroke type.
deletedc (hdc) ;
For example, you can get a handle to the device environment for the current entire screen by calling the following function:
hdc = createdc (text ("display"), null, null, null) ;
In addition, we will use the createdc function in Chapter 13 to get the handle to the printer device environment.
Get the handle to the memory device environment
hdcmem = createcompatibledc (hdc) ;
Other stroke type.
deletedc (hdcmem) ;
You can select a bitmap into the memory device environment and call the gdl function to draw the bitmap. I will cover these techniques in Chapter 14.
Get the entire window device environment handle
hdc = getwindowdc (hwnd) ;
Other stroke type.
releasedc (hwnd, hdc) ;
In addition to the client area, the device environment here also includes a window title bar, menus, scroll bars, and the outline of the client area. Applications rarely use the getwindowdc function. If you want to try using it, you should also capture the WM NCPAINT (nonclient paint) message, which Windows uses to draw in the non-client area of the window.
Gets the metafile device environment handle
hdcmeta = createmetafile (pszfilename) ;
Other stroke type.
hmf = closemetafile (hdcmeta) ;
Any GDI calls made with HDCMETA will not be displayed directly when the metafile device environment is valid, they will become part of the metafile. When you call closemetafile, the metafile device environment handle becomes invalid and the function returns a metafile handle (hmf). I will discuss metafiles in Chapter 17.
The device environment usually refers to a physical display device, such as a monitor or printer. It is often necessary to obtain certain information about these devices, including the size of the display (in pixels or physical dimensions) and its color capabilities. This information can be obtained by calling the getdevicecaps function.
ivalue = getdevicecaps (hdc, iindex) ;
where the parameter iindex is defined in wingdlOne of the 29 identifiers in the h header file. For example, when the value of iindex is horzres, the getdevicecaps function returns the width of the device in pixelsUsing the ertres parameter value returns the height of the device in pixels. If HDC is a handle to a screen device environment, the information obtained here is the same as that obtained from the GetSystemMetrics function. If HDC is a printer device environment, then GetDevice Caps will return the height and width of the printer's display area in pixels.
You can also use the getdevicecaps function to determine the device's ability to handle various types of graphics. Usually this is not important for monitors, but it is very important for printers. For example, most plotters can't draw bitmap images, which can be known in advance by calling the getdevicecaps function. We'll demonstrate how to use the getDeviceCaps function in the following examples.
022.Programming expert win32 api practice every day.
The 22nd example devcaps1c: Obtain the environment information of the display device.
getdevicecaps function.
c) www.bcdaren.com, 2020
#include
define numlines ((int)(sizeof devcaps sizeof devcaps[0])).
struct
int iindex;Index.
tchar *szlable;System Information.
tchar *szdesc;Description.
devcaps= Structural array.
horzsize, text("horzsize"), text("width in millimeters:"),vertsize, text("vertsize"), text("height in millimeters:"),horzres, text("horzres"), text("width in pixels:"),vertres, text("vertres"), text("height in raster lines:"),bitspixel, text("bitspixel"), text("color bits per pixel:"),planes, text("planes"), text("number of color planes:"),numbrushes, text("numbrushes"), text("number of device brushes:"),numpens, text("numpens"), text("number of device pens:"),nummarkers, text("nummarkers"), text("number of device markers:"),numfonts, text("numfonts"), text("number of device fonts:"),numcolors, text("numcolors"), text("number of device colors:"),pdevicesize, text("pdevicesize"), text("size of devicestructure:"),aspectx, text("aspectx"), text("relative width of pixel:"),aspecty, text("aspecty"), text("relative height of pixel:"),aspectxy, text("aspectxy"), text("relative diagonal of pixel:"),logpixelsx, text("logpixelsx"), text("horizontal dots per inch:"),logpixelsy, text("logpixelsy"), text("vertical dots per inch:"),sizepalette, text("sizepalette"), text("number of palette entries:"),numreserved, text("numreserved"), text("reserved palette entries:"),colorres, text("colorres"), text("actual color resolution:")
lresult callback wndproc (hwnd, uint, wparam, lparam) ;
int winapi winmain (hinstance hinstance, hinstance hprevinstance,pstr szcmdline, int icmdshow)
static tchar szappname = text ("devcaps1.c") ;
Omitted) return msgwparam ;
lresult callback wndproc (hwnd hwnd, uint message, wparam wparam, lparam lparam)
static int cxchar, cxcaps, cychar;
hdc hdc ;
int i;
paintstruct ps ;
tchar szbuffer[10] ;
textmetric tm ;
switch (message)
case wm_create:
hdc = getdc (hwnd) ;
gettextmetrics (hdc, &tm) ;
cxchar = tm.tm**echarwidth ;The average width of the character.
The average character width cxcaps is set to 15 times. 1 indicates a widened font.
cxcaps = (tm.tmpitchandfamily & 1 ? 3 : 2) *cxchar / 2 ;
cychar = tm.tmheight + tm.tmexternalleading ;Character leading.
releasedc (hwnd, hdc) ;
return 0 ;
case wm_paint :
hdc = beginpaint (hwnd, &ps) ;
for (i = 0 ; i < numlines ; i++)
textout(hdc, 0, cychar * i,devcaps[i].szlable,lstrlen(devcaps[i].szlable));
textout(hdc, 14 * cxcaps, cychar*i,devcaps[i].szdesc,lstrlen(devcaps[i].szdesc));
settextalign(hdc, ta_right | ta_top);
textout(hdc, 14 * cxcaps + 35 * cxchar, cychar * i, szbuffer,wsprintf(szbuffer, text("%5d"),getdevicecaps(hdc,devcaps[i].iindex)))
settextalign(hdc, ta_left | ta_top);
endpaint(hwnd, &ps);
return 0;
case wm_destroy:
postquitmessage(0);
return 0;
return defwindowproc(hwnd, message, wparam, lparam);
The getDeviceCaps function retrieves device-specific information for a specified device.
int getdevicecaps(
HDC HDC, Device Environment Handle.
int index of device environment information.
The return value specifies the value of the desired item.
Similar api:
getSystemMetrics function: retrieves specified system metrics or system configuration settings.
int getsystemmetrics (int nindex to retrieve the system metrics or configuration settings);
Running Results:
Figure 4-1 Obtaining information about the display device.
Summary. devcaps1.The ** implementation of C is very simple, define an array of structures that display device information, and then loop through the output of GetDeviceCaps(HDC, DevCaps[I].) in the WM PAINT messageiindex))) Obtained display device information.