Access to MFC window position and size

Source: Internet
Author: User

Recently doing a project requires the control to change with the size of the dialog box, so you need to get the dialog window, the control's size, and the position exactly.

After a good search, testing, finally saw the hope. Here are some functions to get the window position and size, as in the following example:

1. Get screen resolution

//The bottom two functions get the size of the display screen, but do not include areas such as the taskbarintCX =GetSystemMetrics (sm_cxfullscreen);intcy =GetSystemMetrics (sm_cyfullscreen);//The bottom two functions get the actual size of the screen: screen resolutionintNwidth=getsystemmetrics (Sm_cxscreen);//Screen WidthintNheight=getsystemmetrics (Sm_cyscreen);//Screen HeightCString Strscreen;strscreen.format (L"%d,%d", Nwidth,nheight); MessageBox (strscreen);

2. Get dialog box form size and its screen coordinates

//dialog box form size and its screen coordinatesCRect Rectdlg;//Act 1:GetClientRect (RECTDLG);//get the size of the form//Act 2://GetWindowRect (RECTDLG);//get the position of the form on the screen//screentoclient (RECTDLG);CString Strdlg;strdlg.format (L"%d,%d,%d,%d", Rectdlg.left,rectdlg.top,rectdlg.width (), Rectdlg.height ()); MessageBox (STRDLG);

3. Get control size and position

//control size and positionCRect Rectctrl; CStatic*p= (cstatic*) GetDlgItem (idc_static_test);p->movewindow ( -, -, -, -);//change the size of the control and move it to the specified positionP->GetWindowRect (Rectctrl); This-screentoclient (Rectctrl);//GetDlgItem (idc_static_test)->getclientrect (Rectctrl);CString str;str. Format (L"%d,%d,%d,%d", Rectctrl.left,rectctrl.top,rectctrl.width (), Rectctrl.height ()); MessageBox (str);

Summarize:

1. GetSystemMetrics () --to obtain the defined system data or system configuration information;

2, GetClientRect () to get the location and size of the customer area, generally used to get the window size;

3, GetWindowRect ()-Gets the screen coordinates of the window (dialog box or control), typically used to get the screen coordinates of the dialog box or to use the client area coordinates of the get control with the ScreenToClient ();

4, ScreenToClient ()--screen coordinates convert to client area coordinates, general and GetWindowRect () use to get the client area coordinates of the control;

Access to MFC window position and size

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.