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.