Windows program design: No-basics self-learning _ 5_gdi basics _ getting device content information

Source: Internet
Author: User
Tags textout

I don't know what's going on. Recently, the fire was so big that it was difficult to calm down and learn.

It is very difficult for state-owned enterprises to fight hard, and they struggle all day in the form of swords and swords, and they are still in war. I don't know how foreign-funded enterprises are doing. Isn't there anything like this.

I have been hearing that foreign-funded enterprises are looking at technology and capabilities, and basically do not have such things. I don't know if they are .....................

I haven't studied it for several days. I was woken up to handle the fault in the middle of the night yesterday. Although it was a weekend, I went to the factory to handle the fault in the morning. It was really tired ..... after a nap in the afternoon, I feel better.

Reading books.

The following content is summarized today ......

5.2.2 obtain device content information
A device content usually refers to an actual display device. You can call a system function to obtain information about the device, such as the display size and color display capability. You can use
The getdevicecaps function is used to obtain these messages.
Exp:
Ivalue = getdevicecaps (HDC, iindex );
Tip:
Iindex is one of the 29 macros defined in the wingdi. h header file.
For example:
If iindex is horzres, getdevicecaps will be sent back to the device in width (in the unit of pixels). If iindex is vertres, getdevicecaps will
Returns the height of the device. If HDC is the content handle of the printer device, getdevicecaps returns the height and width (unit:
Is a graphic element ).

5.2.3 device size
To draw a square with a side length of 1 inch, you need to know the number of pixels corresponding to 1 inch on the display. In Windows, you can use getdevicecaps.
Function to obtain the actual display size of output devices, such as monitors and printers.
Resolution:
The printer uses dots per inch to indicate resolution: 300 dpi
The general diagram of the display is usually flat and vertical, indicating the resolution: 1024*768
In Windows program design, we adopt a strict definition:
Resolution: number of elements per measurement unit
Pixel size/pixel size: indicates the total pixel size displayed horizontally or vertically by the device.
Measurement size/measurement size: the size of the display area of a device in inches or mm, that is, the size of the X axis or Y axis.
In this way:
Resolution = image size/measurement size
Obtain the screen element size:
1) Windows applications can use the sm_cxscreen and sm_cyscreen parameters to obtain the pixel size from getsystemmetrics.
2) The horzres (horizontal resolution: horizontal prime size) and vertres (vertical resolution: vertical prime size) parameters can be used.
Get the same value from getdevicecaps
About horzsize and vertsize:
Horzsize: the actual screen width in millimeters.
Vertsize: the actual screen height in millimeters.
5.2.3.1
When we change the display attribute: desktop-> right-click-> properties-> appearance-> font size.
The reason is that the font size is used to display a larger font when the display resolution is increased, for example, 640*480 -- 1024*768.
The font size refers to the system font size, which refers to the small font and big font in the page labels set by the display program.

In traditional typography, the font Letter size is represented by a dot. ~ = 1/72 inch.
Theoretically, the font point value is the distance from the top of the top to the bottom of the character in the font, excluding the accent. High characters such as J, p, q, and Y.
For example, in the font at, the character point value is 10/72 inch.
In the textmetric structure:
Font point value = tmheight-tminternalleading field.
The tmheight field specifies the interval between consecutive lines of text on the screen or printer, which can also be measured by points.
Line spacing: the distance between the two line fonts between the baselines. The 12-point line spacing indicates that the baseline spacing of the continuous lines of the text is 12/72 inch.

5.2.3.2 color
When black and white elements are displayed, each element only needs one in the memory, and each element in the color display needs multiple places.
The more colors you display, the more digits you need.
The number of different colors displayed on the monitor is equal to the number of multiplied by two digits.

The full-color display has a sensitivity rate of 24 bits per graphic element. The values are 8 bits in red, 8 bits in green, and 8 bits in blue. Red, green, and blue are the three primary colors of color and light, mixed
These three colors can generate various other colors.

High-Color Display resolution: Each Graphic Element has 16 bits: 5 bits in red, 6 bits in green, and 5 bits in blue. Because human eyes are sensitive to green, one more bits are displayed.

In display 256, the color is that each graphic element of the display card requires 8 digits. These 8-bit values are generally organized by a color palette that defines the actual color.

Each graphic element of the display card of 16 colors needs four digits. These colors are generally fixed to: dark red/bright red, black, blue, blue, purple, yellow, and two gray colors.

Generally, only some special programs need to know how the memory on the display card is organized. You can use the getdevicecaps function to obtain the Storage Organization and
Number of colors supported by the video card:
Iplanes = getdevicecaps (HDC, PLANeS );
Pass:
Ibitspixel = getdevicecaps (HDC, bitspixel); obtains the color digits of each element.
Most display devices use multiple color planes or each graph is designed with multiple color bits. These two methods cannot be used together.
The return values of these two functions must be equal to or equal to 1.
Number of colors supported by the video card:
About icolors = 1 <(iplanes * ibitpixel );
This value may be the same or different from the value obtained using the numcolors parameter.
Exp:
Icolors = getdevicecaps (HDC, numcolors );

For a 256-color display card, use a color palette. In this case, when the numcolors parameter is used as the parameter, the value returned by getdevicecaps is Windows
The number of colors retained. The value is 20. The remaining 236 colors are managed by the windows color palette. For highcolor and true-color
Resolution. When the value returned by calling the getdevicecaps function with the numcolors parameter is-1, the required information cannot be obtained. In this case, planes and
The icolors formula for calculating the bitspixel value.

RGB color
In most GDI function calls, the colorref value (unsigned INT) is used to represent a color. The colorref value is based on the brightness of red, green, and blue.
Specifies a color, usually called RGB color.
31... 24 23... 16 15... 8 7... 0
0 blue green red
Note that this is the color displayed for full-color: The D31-D24 bit is 0; The D23-D16 represents blue, the D15-D8 is green, and the D7-D0 is red. Theoretically
In this way, RGB can represent 2 ^ 24 or 16 million colors.

Such an unsigned number is often an RGB color. The Windows header file wingdi. H provides several macros that use RGB color values. RGB macro requirements
The three parameters represent the values of red, green, and blue respectively, and then combine them into an unsigned integer.
Exp:
# Define RGB (R, G, B) (colorref) (byte) (R) | (Word) (byte) (g) <8 )) | (DWORD) (byte) (B) <16 )))
Therefore value:
RGB (255,255, 0); 0x0000ffff or yellow (red and green ).
When R, G, and B are all set to 0, the color is black,
When R, G, and B are all 255, the color is white.
You can use the getrvalue, getgvalue, and getbvalue macros to extract primary color values from colorref values.

On a 16-or 256-color display card, Windows can use a mixed color to simulate colors other than those displayed by the device. Mixed colors use a variety
A graphic pattern composed of color elements, which can be called:
Getnearestcolor determines the closest solid color of a certain color:
Exp:
Crpurecolor = getnearestcolor (HDC, crcolor );

5.2.4 device content attributes
Windows uses the device content to save various properties that control how the GDI function operates on the display.
Exp:
Windows function textout does not need to specify the color and font of the text. Windows obtains the information from the device content.

When an application obtains a device content handle, Windows uses the default value to set all its properties.
Windows defines many device content attributes, which can be obtained through the Table query.

5.2.4.1 save device content
When getdc or beginpaint is called, Windows uses the default value to create a new device content.
All changes made will be lost when the device content is released by calling releasedc or endpaint. If the program uses non-default device content
Properties, the device content must be initialized when the application processes the message to obtain the device content handle.
Exp:
Case wm_paint:
HDC = beginpaint (hwnd, & PS );
Device Content Attributes
Display area of the drawing window
Endpaint (hwnd, & PS );
Return 0;

Although the above method can meet certain requirements, you can still save the changes made to the properties of the device content after the device content is released,
So that the attribute can still work when getdc and beginpaint are called next time. In this case, you need:
When registering a window category, add the cs_owndc flag to the style attribute of the window category.
Exp:
Wndclss. Style = cs_vredraw | cs_hredraw | cs_owndc;
In this way, each window created based on this window category will have its own device content, and these properties will exist
The window is deleted. If the cs_owndc style is used, you only need to initialize the device content once. The window can be completed during wm_create message processing.
Device content initialization:
Exp:
Case wm_create:
HDC = getdc (hwnd );
Initialize device content
Releasedc (hwnd, HDC );
Return 0;
The modified device content attributes remain valid until they are changed again or deleted in the window.
Key points:
1) The wm_owndc style only affects the device content obtained by getdc and beginpaint, and does not affect the retrieval of other functions (such as getwindowdc ).
.
2) even if the wm_owndc style is used, you still need to release the device content handle before Message Processing return 0.
3) wm_owndc requires memory usage, but it can provide performance in graphics processing programs.
5.2.4.2
Sometimes you may want to change the properties of some devices, use the changed properties for drawing, and restore to
Device content attributes. You can use
Exp:
Case wm_lbuttondown:
HDC = getdc (hwnd );
I = gettextcolor (HDC );
Setsystemmetrics (HDC, some attributes );
Draw graphics;
Settextcolor (HDC, I );
Releasedc (hwnd, HDC );
You can also use:
Exp:
Idsaved = savedc (HDC );
Change attributes
Plotting
Restoredc (HDC, idsaved );
You can call savedc multiple times before calling restroredc.
A more direct method is provided here:
Exp:
Savedc (HDC );
Changing certain attributes
You can call savedc once.
Restoredc (HDC,-1); this operation restores the last time the property value of the device content is saved.
If you call savedc multiple times and want to restore to the initial state, you need to perform multiple restoredc operations.

The code for the test below is relatively simple. Only a small part of the code is annotated:

/*
*
* Basic instance program for getting started with device content
*
*/

# Include <windows. h>
# Include <winuser. h>

# Define numlines (INT) (sizeof devcaps/sizeof devcaps [0])

Struct devcaps
{
Int iindex;
Tchar * szlabel;
Tchar * szdesc;
};
Devcaps [] =
{
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 pixels "),
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 marks :"),
Numfonts, text ("numfonts"), text ("Number of device fonts "),
Numcolors, text ("numcolors"), text ("Number of device colors :"),
Pdevicesize, text ("pdevicesize"), text ("size of Device of structure :"),
Aspectx, text ("aspectx"), text ("relative width of pixel :"),
Aspecty, text ("aspecty"), text ("relative height of pixel :"),
Logpixelsx, text ("logpixelsx"), text ("Horizontal dots per inch :"),
Logpixelsy, text ("logpixelsy"), text ("vertical dots per inch :"),
Sizepalette, text ("sizepalette"), text ("Reserved palette entries :"),
Colorres, text ("colorres"), text ("actual color resolution :")
};

Lresult callback wndproc (hwnd, uint, wparam, lparam );

Int winapi winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ishowcmd)
{
Static tchar szappname [] = text ("devcaps1 ");
Hwnd;
MSG;
Wndclass;

Wndclass. cbclsextra = 0;
Wndclass. cbwndextra = 0;
Wndclass. hbrbackground = (hbrush) getstockobject (white_brush );
Wndclass. hcursor = loadcursor (null, idc_arrow );
Wndclass. hicon = loadicon (null, idi_application );
Wndclass. hinstance = hinstance;
Wndclass. lpfnwndproc = wndproc;
Wndclass. lpszclassname = szappname;
Wndclass. lpszmenuname = NULL;
Wndclass. Style = cs_hredraw | cs_vredraw;

If (! Registerclass (& wndclass ))
{
MessageBox (null, "You need winnt to run this program! ", Text (" warning "), mb_ OK );
Return 0;
}

Hwnd = createwindow (szappname,
Text ("getdevcaps "),
Ws_overlappedwindow | ws_hscroll | ws_vscroll,
100,
100,
640,
480,
Null,
Null,
Hinstance,
Null );
Showwindow (hwnd, ishowcmd );
Updatewindow (hwnd );

While (getmessage (& MSG, hwnd, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Return msg. wparam;
}

Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
HDC;
Paintstruct pS;
Textmetric TM;
Static int cxchar,
Cychar,
Cxcaps,
Cycaps;
Static int I,
J = 0;
Tchar szbuffer [10];

Switch (Message)
{
Case wm_create:
HDC = getdc (hwnd );
Gettextmetrics (HDC, & TM); // gets the system font information of the device.
Cxchar = TM. tmavecharwidth;
Cxcaps = (TM. tmpitchandfamily & 1? 3: 2) * cxchar/2;
Cychar = TM. tmheight + TM. tmexternalleading;
Releasedc (hwnd, HDC );
Return 0;
Case wm_paint:
HDC = beginpaint (hwnd, & PS );
Endpaint (hwnd, & PS );
Return 0; // This program indicates that if wm_paint is processed like this, the invalid area will not be repainted.
Case wm_lbuttondown:
HDC = getdc (hwnd );
For (I = 0; I <numlines; I ++)
{
J = J + 20;
Settextcolor (HDC, RGB (J + 5, J-10, j ));
Textout (HDC, 0, cychar * I, devcaps [I]. szlabel, lstrlen (devcaps [I]. szlabel ));
Textout (HDC, 14 * cxcaps, cychar * I, devcaps [I]. szdesc, lstrlen (devcaps [I]. szdesc ));
Settextalign (HDC, ta_right | ta_top); // sets the font alignment mode.
Textout (HDC, 14 * cxcaps + 35 * cxchar, cychar * I, szbuffer, wsprintf (szbuffer, text ("% 5d"), getdevicecaps (HDC, devcaps [I]. iindex )));
Settextalign (HDC, ta_left | ta_top );
}
Settextcolor (HDC, RGB (0, 0 ));
Releasedc (hwnd, HDC );
Return 0;
Case wm_rbuttondown:
HDC = getdc (hwnd );
Textout (HDC, 0, I * (1 + cychar), text ("test color set function! "), Lstrlen (" test color set function! "));
Releasedc (hwnd, HDC );
Return 0;
Case wm_destroy:
Postquitmessage (0 );
Return 0;
}
Return defwindowproc (hwnd, message, wparam, lparam );
}
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.