Modify the widget size and position in MFC (zz)

Source: Internet
Author: User

You can use the functions MoveWindow () or SetWindowPos () of the CWnd class to change the widget size and position.

Void MoveWindow (int x, int y, int nWidth, int nHeight );
Void MoveWindow (LPCRECT lpRect );
The first method requires the new coordinates, width, and height of the control;
The second method provides the CRect object of the storage location;
Example:
CWnd * pWnd;
PWnd = GetDlgItem (IDC_EDIT1); // gets the control pointer. IDC_EDIT1 indicates the Control ID.
PWnd-> MoveWindow (CRect (100,100, 100); // an editing control with a width of 100 and a height of is displayed in the upper left corner of the window.

The SetWindowPos () function is more flexible to use. It is mainly used when only the control position is changed but the size is not changed or only the size is changed but the position is not changed:
BOOL SetWindowPos (const CWnd * pWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags );
I will not use the first parameter. It is generally set to NULL;
Position of the x and y controls; width and height of the cx and cy controls;
Common nFlags values:
SWP_NOZORDER: Ignore the first parameter;
SWP_NOMOVE: Ignore x and y and keep the position unchanged;
SWP_NOSIZE: Ignore cx and cy and keep the size unchanged;
Example:
CWnd * pWnd;
PWnd = GetDlgItem (IDC_BUTTON1); // gets the control pointer. IDC_BUTTON1 indicates the Control ID.
PWnd-> SetWindowPos (NULL, 50, 80, SWP_NOZORDER | SWP_NOSIZE); // move the button to the window (50, 80)
PWnd = GetDlgItem (IDC_EDIT1 );
PWnd-> SetWindowPos (NULL, 80, SWP_NOZORDER | SWP_NOMOVE); // set the size of the Edit Control to (, 80), and the position remains unchanged
PWnd = GetDlgItem (IDC_EDIT1 );
PWnd-> SetWindowPos (NULL, 80, SWP_NOZORDER); // modify the widget size and position
The preceding method also applies to various windows.

 

 

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.