The use of the GetSystemMetrics () function

Source: Internet
Author: User
Tags textout

The GetSystemMetrics function can be used to obtain the system resolution, but this is only one of its functions, the GetSystemMetrics function has only one parameter, called "Index", the index has 75 identifiers, by setting different identifiers can obtain the system resolution, The form displays the width and height of the area, the width and height of the scroll bar.

For the purpose of making the GetSystemMetrics function, we take the system resolution as an example and output the two values in TextOut to the form.

First step: Get the width and height of the screen with GetSystemMetrics

1 int x, y; 2 // Screen Width 3 // Screen Height

Step two: output resolution with TextOut

Because the values of x and y are of type int, it is not possible to output directly with TextOut, and with the help of the wsprintf function, in C, the string can be output directly to the screen using the printf function, but wsprintf is outputting the string to a char character array. And wsprintf will return the length of the output string.

1 TCHAR szbuffer[]; 2 int 1024x768 ; 3 int 768 ; 4 int ilength; 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 code is to use wsprintf to save the value of x in 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 output the TCHAR type string directly, and gets the length of the string, then we can use the TextOut function to output the width of the screen in the form:

    1. TextOut (hdc, 0, 0, szbuffer, ilength);

The focus of this article is the GetSystemMetrics function, 75 index identifiers of the function I did not try one, anyway, as long as I know what this function is all about. In this article wsprintf function is to solve the problem of int to string, originally wanted to use the INTTOSTR function directly in textout to output the value of the int type, but did not understand which inttostr function in which header file, always use. The INTTOSTR function I used before, should have this function in C + +.

Get the form display area size

As I know now, there are three ways to get the size of the form display area.

The first method: using the GetSystemMetrics function

1 // gets the width of the display area of the maximized form 2 // gets the height of the display area of the maximized form

Second approach: using the GetClientRect function

The function is prototyped as follows:

1 BOOL GetClientRect (2HWND hwnd,//Handle to Window3LPRECT LPRECT//Client coordinates4 );5 The book does not describe the use of the function, see the MSDN Sample code, tested, using the following methods:6 7 rect rect;8HDC = BeginPaint (hwnd, &PS);9GetClientRect (hwnd, &rect);Tenx =Rect.right; Oney =Rect.bottom; ASetTextAlign (hdc, Ta_right |ta_top); -TextOut (HDC, -,0, Szbuffer, wsprintf (Szbuffer,text ("%5d"), x); -TextOut (HDC, -, Cychar,szbuffer, wsprintf (Szbuffer, TEXT ("%5d") , y)); theEndPaint (hwnd, &PS);

Third method: LoWord and HiWord macros

1  Case wm_size: 2      x = LoWord (lParam); 3      y = HiWord (LParam);

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

Sm_arrange Returns whether the preparation is minimized. Sm_cleanboot return system boot mode:0Normal Start1Safe Mode startup2network security mode start sm_cmousebuttons The return value is the number of mouse keys supported by the system and returns 0, the mouse is not installed in the system. Sm_cxborder, Sm_cyborder returns the width and height of the Windows window border in pixels, which is equivalent to the Sm_cxedge parameter sm_cxcursor if Windows is in 3D form, sm_cycursor Returns the width and height of the standard cursor in phase Sm_cxdlgframe,sm_cydlgframe equivalent to Sm_cxfixedframe and Sm_cyfixedframe SM_CXDOUBLECLK, sm_ CYDOUBLECLK Double-click a valid rectangular area in pixels sm_cxedge,sm_cyedge the width and height of the 3D border in the phase value Sm_cxfixedframe,sm_cyfixedframe The thickness of the border around a window that has a title but cannot be changed (usually some dialog boxes) is sm_cxframe,sm_cyframe equivalent to Sm_cxsizeframe and Sm_cysizeframe Sm_cxfullscreen,sm_ Cyfullscreen the width and height of the window area of the Full-screen window sm_cxhscroll,sm_cyhscroll the height of the horizontal scrollbar and the width of the arrows on the horizontal scroll bar sm_cxhthumb The width of the slider on the horizontal scroll bar in pixels sm_ Cxicon,sm_cyicon the height and width of the default icons for the system (typically* +Sm_cxiconspacing,sm_cyiconspacing A large icon to view the spacing between icons in the item, this distance is always greater than or equal to Sm_cxicon and Sm_cyicon. Sm_cxmaximized,sm_cymaximized The default size of the maximized window in the top-level sm_cxmaxtrack,sm_cymaxtrack has the default maximum size of the window that can change the size border and the title bar, if the window is larger than this size, The window is not movable. Sm_cxmenucheck,sm_cymenucheck the dimension of a menu-checked marker bitmap in pixels sm_cxmenusize,sm_cymenusize the size of the menu bar button calculated in phase Sm_cxmin,sm_cymin The minimum size that the window can reach sm_cxminimized,sm_cyminimized the normal minimized window size Sm_cxmintrack,sm_cymintrack the minimum tracking distance, and the window will not move when the user drags the window less than this value. Sm_cxscreen,sm_cyscreen the screen size as a unit of the phase factor. Sm_cxsize,sm_cysize the size of the title bar button calculated in phase Sm_cxsizeframe,sm_cysizeframe the thickness of the border around the window that can be resized Sm_cxsmicon,sm_cysmicon The size of small icons in phase factor, small icons generally appear in the window title bar. M_cxvscroll,sm_cyvscroll the width of the vertical scrollbar computed by the phase element and the height of the arrows on the vertical scroll bar sm_cycaption The height of the normal window caption calculated by the voxel sm_cymenu the height of a single menu bar calculated as a phase element sm_ Cysmcaption the height of the small title bar of a window in a phase element sm_cyvthumb the height of the scroll block in the vertical scrollbar calculated as a phase sm_dbcsenabled if TRUE or a value not of 0 indicates that the system has a double-byte version of USER.EXE installed, is False or 0 is not. Sm_debug if the value of TRUE or not 0 indicates that the system has a DEBUG version of USER.EXE installed, false or 0 is not. Sm_menudropalignment if the value is true or not 0, the drop-down menu is right-aligned or left-aligned. Sm_mousepresent if a value of true or not 0 is installed, the mouse is not installed. Sm_mousewheelpresent a Wheel Mouse is installed if the value is true or not 0, otherwise it is not installed. (Windows NT only) Sm_swapbutton if TRUE or not a value of 0 then the mouse key is exchanged or not.

Reprinted from: http://www.cnblogs.com/lidabo/archive/2012/07/10/2584725.html

The use of the GetSystemMetrics () function

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.