Welcome: Hunan Intelligent Applications Tecgnology CO.,ltd.-HNIAT.com
Language: Chinese ∷  English

Basic knowledge

MFC change control position and size

Recently, I often used to change the position and size of the control on the dialog box. I have not been able to find an effective method. I checked a lot of information. This blog post is quite reliable, reproduced here.

1

void CmyqeDlg :: OnSize (UINT nType, int cx, int cy)


2

{


3

CDialog :: OnSize (nType, cx, cy);


4

if (nType! = SIZE_MINIMIZED)


5

{


6

  if (me) // determine if it is empty, because this function will be called when the dialog is created, and the control has not been created yet


7

 {


8

    CRect rt;


9

    me.GetWindowRect (& rt);


10

   ScreenToClient (& rt);


11

    me.MoveWindow (rt.left, rt.top, cx-rt.left * 2, cy-rt.top-rt.left);


12

   }


13

}


14

 


15

CWnd * pWnd;


16

pWnd = GetDlgItem (IDC_BUTTON1); // Get the control pointer, IDC_BUTTON1 is the control ID number




 Use the CWnd class functions MoveWindow () or SetWindowPos () to change the size and position of the control.

1

void MoveWindow (int x, int y, int nWidth, int nHeight);


2

void MoveWindow (LPCRECT lpRect);




 The SetWindowPos () function is more flexible. It is mostly used to modify only the position of the control without changing the size, or only modify the size and change the position:
BOOL SetWindowPos (const CWnd * pWndInsertAfter, int x, int y, int cx, int cy, UINT nFlags);
I will not use the first parameter, generally set to NULL;
 x, y control position; cx, cy control width and height;
nFlags commonly used values:
SWP_NOZORDER: ignore the first parameter;
SWP_NOMOVE: Ignore x and y, keep the position unchanged;
SWP_NOSIZE: Ignore cx and cy, keep the size unchanged;
 example:

1

CWnd * pWnd;


2

pWnd = GetDlgItem (IDC_BUTTON1); // Get the control pointer, IDC_BUTTON1 is the control ID number


3

pWnd-> SetWindowPos (NULL, 50,80,0,0, SWP_NOZORDER | SWP_NOSIZE); // Move the button to (50,80) of the window


4

pWnd = GetDlgItem (IDC_EDIT1);


5

pWnd-> SetWindowPos (NULL, 0,0,100,80, SWP_NOZORDER | SWP_NOMOVE); // Set the size of the edit control to (100,80) without changing the position


6

pWnd = GetDlgItem (IDC_EDIT1);


7

pWnd-> SetWindowPos (NULL, 0,0,100,80, SWP_NOZORDER); // The size and position of the edit control are changed




 The above method is also applicable to various windows.


 If the size and position of the control does not change after the size of the dialog box or view class is adjusted, the interface will look uncomfortable
 The control is derived from CWnd, but you cannot use SetWindowPos () or OnSize () or OnSizing () to change its size. You should use MoveWindow () to adjust it in the WM_SIZE message of the parent window.


VC ++ adjust the size of the control according to the size of the dialog box
1. Add a member variable CRect m_rect to the dialog class; it is used to save the size of the dialog before the size changes;
2. In the dialog box's OnInitDialog () function, get the size of the dialog box when it is created: GetClientRect (& m_rect);
 3. Add the following code to the response function OnSize () of WM_SIZE:

1

CWnd * pWnd;


2

pWnd = GetDlgItem (IDC_LIST); // Get control handle


3

if (pWnd) // determine whether it is empty, because this function will be called when the dialog box is created, and the control has not been created yet


4

{


5

          CRect rect; // Get the size before the control changes


6

          pWnd-> GetWindowRect (& rect);


7

          ScreenToClient (& rect); // Convert control size to area coordinates in the dialog


8

          // cx / m_rect.Width () is the change ratio of the dialog box in the horizontal direction


9

          rect.left = rect.left * cx / m_rect.Width (); / ** / /// // resize the control


10

         rect.right = rect.right * cx / m_rect.Width ();


11

         rect.top = rect.top * cy / m_rect.Height ();


12

         rect.bottom = rect.bottom * cy / m_rect.Height ();


13

         pWnd-> MoveWindow (rect); // Set the size of the control


14

}


15

GetClientRect (& m_rect); // Set the changed dialog size to the old size




 Solve a bug:
Plus if (nType! = 1) ()
Or if (nType! = SIZE_MINIMIZED) ()
Otherwise, BUG (integer divided by 0) will be generated after the window is minimized and restored.




Post the code for my own actual question:

First define a few variables in the header file:

1

CRect rectPointList, rectCmdResponse;


2

CWnd * pWnd;




Then save the size and position of the control you want to get in the dialog initialization function
The system first obtains the screen coordinates. After obtaining the screen coordinates, the screen coordinates are converted into the user coordinate system:

1

        pWnd = (CWnd *) GetDlgItem (IDC_STATIC_GROUPBOX_POINT_LIST);


2

pWnd-> GetWindowRect (rectPointList);


3

ScreenToClient (rectPointList);


4

 


5

pWnd = (CWnd *) GetDlgItem (IDC_EDIT_RESPONSE);


6

pWnd-> GetWindowRect (rectCmdResponse);


7

ScreenToClient (rectCmdResponse);




Resize the control in a function that responds to the change in size:
1

pWnd = GetDlgItem (IDC_EDIT_RESPONSE);


2

pWnd-> MoveWindow (rectCmdResponse.left, rectCmdResponse.top, rectCmdResponse.right-rectCmdResponse.left, rectCmdResponse.bottom-rectCmdResponse.top);


3

//pWnd->SetWindowPos(NULL,rectCmdResponse.left,rectCmdResponse.top,rectCmdResponse.right-rectCmdResponse.left,rectCmdResponse.bottom-rectCmdResponse.top,SWP_NOZORDER);



The functions of MoveWindow and SetWindowPos are the same, and the parameters are different. Then the coordinates of the four points, the first two are the left position of the control, the first is the position of the top of the control, and the third is the width of the control. You need to subtract the left from the right, and the fourth height, subtract the top from the bottom.
In this way, the function can be perfectly realized!




note! !! Edit Control in MFC, if you write to the control using \ n, then after changing the size, the text in the box is not wrapped in the new position! It's all lined up.

So when entering text into Edit Control, it's best! Best line breaks use \ r \ n !!!!

CONTACT US

Contact: Manager Xu

Phone: 13907330718

Tel: 0731-22222718

Email: hniatcom@163.com

Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province

Scan the qr codeClose
the qr code