Getsystemmetrics () function usage

Source: Internet
Author: User
Tags textout

You can use the getsystemmetrics function to obtain the system resolution, but this is only one of its functions. The getsystemmetrics function has only one parameter, which is called "Index". This index has 75 identifiers, you can set different identifiers to obtain the system resolution, the width and height of the display area of the form, and the width and height of the scroll bar.

To enable the getsystemmetrics function, we take obtaining the system resolution as an example and output the two values to the form using textout.

Step 1: Use getsystemmetrics to get the screen width and height

    1. IntX, Y;
    2. X = getsystemmetrics (sm_cxscreen );// Screen width
    3. Y = getsystemmetrics (sm_cyscreen );// Screen height

Step 2: Use textout to output the resolution

Because the values of X and Y are int type, textout cannot be used for direct output. You need to use the wsprintf function. In C, you can use the printf function to directly output the string to the screen, however, wsprintf outputs the string to a char character array, and wsprintf returns the length of the output string.

    1. TcharSzbuffer [10];
    2. IntX = 1024;
    3. IntY = 768;
    4. IntIlength;
    5. Ilength = wsprintf (szbuffer, text ("% 5d"), X );

Tip: the length of the szbuffer array must be greater than the length of X. Otherwise, it cannot be output.

The above sectionCodeIs to use wsprintf to store the value of X into szbuffer, and then store the length of X in ilength. This step actually solves two problems: converting the screen width of the int type to the tchar type, the textout function can directly output the tchar type string and obtain the length of the string, then we can use the textout function to output the screen width in the form:

    1. Textout (HDC, 0, 0, szbuffer, ilength );

The focus of this article is the getsystemmetrics function. I did not try the 75 index identifiers of this function one by one. You only need to know the functions of this function. In this article, the wsprintf function solves the problem of int-to-String Conversion. I wanted to use the inttostr function in textout to output the INT-type value directly, but I didn't know which header file the inttostr function is in, not always used. I have used the inttostr function before. This function should be available in C ++.

 

Obtain the display area size of the form.

As I know, there are three methods to obtain the display area size of a form.

Method 1: Use the getsystemmetrics Function

    1. Getsystemmetrics (sm_cxfullscreen );// Obtain the display area width of the maximized form.
    2. Getsystemmetrics (sm_cyfullscreen );// Obtain the height of the display area of the maximized form

Method 2: Use the getclientrect Function

The function is prototype as follows:

    1. BoolGetclientrect (
    2. HwndHwnd,// Handle to window
    3. Lprect// Client coordinates
    4. );

The usage of this function is not described in the book. Check the sample code of msdn. After testing, the usage is as follows:

    1. Rect;
    2. HDC = beginpaint (hwnd, & PS );
    3. Getclientrect (hwnd, & rect );
    4. X = rect. Right;
    5. Y = rect. bottom;
    6. Settextalign (HDC, ta_right | ta_top );
    7. Textout (HDC, 100, 0, szbuffer, wsprintf (szbuffer, text ("% 5d"), X ));
    8. Textout (HDC, 100, cychar, szbuffer, wsprintf (szbuffer, text ("% 5d"), Y ));
    9. Endpaint (hwnd, & PS );

 

Method 3: loword and hiword macros

    1. CaseWm_size:
    2. X = loword (lparam );
    3. Y = hiword (lparam );

The following is the definition of the getsystemmetrics function parameter nindex:

Sm_arrange indicates whether to minimize the number of returned values.
Sm_cleanboot:
0 normal start
1. Start in Safe Mode
2. Start in network security mode
The return value of sm_cmousebuttons is the number of mouse keys supported by the system. If 0 is returned, no mouse is installed in the system.
Sm_cxborder,
Sm_cyborder returns the width and height of the Windows window border in units of prime value. If Windows is in 3D format
Equivalent to the sm_cxedge Parameter
Sm_cxcursor,
Sm_cycursor returns the width and height of the standard cursor in units of prime value
Sm_cxdlgframe,
Sm_cydlgframe is equivalent to sm_cxfixedframe and sm_cyfixedframe.
Sm_cxdoubleclk,
Sm_cydoubleclk double-click the valid rectangular area in units of prime value
Width and height of the 3D border in units of prime value for sm_cxedge and sm_cyedge
Sm_cxfixedframe,
Sm_cyfixedframe refers to the Border thickness of a window (usually a dialog box) with a title but cannot be changed.
Sm_cxframe, sm_cyframe is equivalent to sm_cxsizeframe and sm_cysizeframe
Sm_cxfullscreen,
Width and height of the window area of the sm_cyfullscreen full screen window
Sm_cxhscroll,
Height of the sm_cyhscroll horizontal scroll bar and width of the arrow on the horizontal scroll bar
The sliding block width of the sm_cxhthumb horizontal scroll bar in units of Elements
Sm_cxicon, sm_cyicon system default icon height and width (generally 32*32)
Sm_cxiconspacing,
Sm_cyiconspacing: the spacing between icons when the item is viewed in the big icon mode. The distance is always greater than or equal
Sm_cxicon and sm_cyicon.
Sm_cxmaximized,
Sm_cymaximized: Default size of the maximum window at the top layer
Sm_cxmaxtrack,
Sm_cymaxtrack has the default maximum size of the window with the size border and title bar that can be changed. If the window is larger
Size. The window cannot be moved.
Sm_cxmenucheck,
Sm_cymenucheck: the size of the marked bitmap in the selected menu calculated in units of Elements
Sm_cxmenusize,
Sm_cymenusize: the size of the menu bar button calculated by the prime.
Minimum Size of sm_cxmin and sm_cymin windows
Sm_cxminimized,
Sm_cyminimized minimizes the size of the window normally
Sm_cxmintrack,
The minimum trace distance of sm_cymintrack. The window will not be moved when the distance of the user's drag window is smaller than this value.
Sm_cxscreen,
The screen size of sm_cyscreen is calculated in units of elements.
Sm_cxsize and sm_cysize are the size of the title bar button calculated by the phase element.
Sm_cxsizeframe,
The thickness of the sm_cysizeframe around the border of the window that can be changed
Sm_cxsmicon,
Sm_cysmicon is the size of the small icon calculated by the prime. The small icon usually appears on the window title bar.
M_cxvscroll,
Sm_cyvscroll the width of the vertical scroll bar calculated by the prime and the height of the arrow on the vertical scroll bar
Height of the sm_cycaption ordinary window title calculated by the phase element
Sm_cymenu height of a single menu bar calculated by phase element
Sm_cysmcaption height of the small window title bar calculated by elements
Sm_cyvthumb the height of the scroll block in the vertical scroll bar calculated by the prime.
If the value of sm_dbcsenabled is true or not 0, it indicates that the dual-byte version of user. EXE is installed. If it is false or 0, it is not.
If the value of sm_debug is true or not 0, it indicates that the user. EXE of the debug version is installed. If it is false or 0, it is not.
Sm_menudropalignment if the value is true or not 0, the drop-down menu is right aligned or left aligned.
If sm_mousepresent is true or not 0, the mouse is installed. Otherwise, the mouse is not installed.
If sm_mousewheelpresent is set to true or is not set to 0, the scroll wheel mouse is installed. Otherwise, the mouse is not installed. (Windows NT only)
If sm_swapbutton is true or not 0, the left and right mouse keys are exchanged; otherwise, no.

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.