The size of the MFC control varies with the size of the form

  • 2020-04-02 03:06:05
  • OfStack

one Size and position change

1. First, add CRect m_rect to the form class. This member variable is used to record the current size of the form.

2. In the class wizard (Ctrl+W), add the response function OnSize() of the message WM_SIZE to the form;

Note that the if (nType = = 1) return; This sentence must be added, otherwise an error will occur when minimizing the recovery.


void CPaperManagementDlg::OnSize(UINT nType, int cx, int cy)  
{ 
  CDialog::OnSize(nType, cx, cy); 
  if(nType==1) return;//Minimize and do nothing, right
  // TODO: Add your message handler code here 
  CWnd *pWnd;  
  pWnd = GetDlgItem(IDC_STATIC); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_FILE_TREE); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_EDIT_NAME); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_EDIT_REFERENCE); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_EDIT_SUMMARY); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_EDIT_REMARK); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_BUTTON_UPDATE); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_BUTTON_SAVE); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_STATIC_1); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_STATIC_2); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_STATIC_3); 
  ChangeSize(pWnd, cx, cy); 
  pWnd = GetDlgItem(IDC_STATIC_4); 
  ChangeSize(pWnd, cx, cy); 
  GetClientRect(&m_rect);//Set the changed dialog size to the old size
} 

The function ChangeSize is a manually added user function, as shown below

3. Add a user function to change the size of the control, void ChangeSize(CWnd *pWnd, int cx, int cy), and write the code


void CPaperManagementDlg::ChangeSize(CWnd *pWnd, int cx, int cy) 
{ 
  if(pWnd) //Determines whether it is empty because this function is called when the dialog box is created, while the control is not yet created
  { 
    CRect rect;  //Gets the size of the control before it changes
    pWnd->GetWindowRect(&rect); 
    ScreenToClient(&rect);//Converts the control size to the region coordinates in the dialog box
  
    //Cx/m_rect.width () is the change ratio of the dialog box in landscape
    rect.left=rect.left*cx/m_rect.Width();//Resize control
    rect.right=rect.right*cx/m_rect.Width(); 
    rect.top=rect.top*cy/m_rect.Height(); 
    rect.bottom=rect.bottom*cy/m_rect.Height(); 
    pWnd->MoveWindow(rect);//Set control size
  } 
} 

The function that really changes the size of the control is the ChangeSize function, which is called repeatedly in OnSize to change the size of all the controls on the form.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

The following content is from the Internet:

Two, VC++ according to the size of the dialog box only adjust the control position

      1. Add the member variable CRect m_rect in the dialog box class; Save the size of the dialog box before the size changes;
      2. In the OnInitDialog() function of the dialog box, 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:


  CWnd *pWnd; 
  pWnd = GetDlgItem(IDC_BUTTON1);   //Gets the control handle
  if(nType==1) return;  //Do nothing if you minimize the form
  if(pWnd)//Determines whether it is empty because this function is called when the dialog box is created, while the control is not yet created
  { 
  CRect rect;  //Gets the size of the control before it changes
   LONG cWidth,cHeight; //Records the distance from the right of the control to the right of the form, and the distance from the bottom of the control to the bottom of the form
   pWnd->GetWindowRect(&rect);
   ScreenToClient(&rect);//Converts the control size to the region coordinates in the dialog box
   cWidth=m_rect.Width()-rect.right;
   cHeight=m_rect.Height()-rect.bottom;
   rect.left=cx-rect.Width()-cWidth;
   rect.right=cx-cWidth;  
   rect.top=cy-rect.Height()-cHeight;
   rect.bottom=cy-cHeight;
   pWnd->MoveWindow(rect);//Set control size
  }
  GetClientRect(&m_rect);// Set the changed dialog size to the old size 

The above is all the content of this article, I hope you can enjoy it.


Related articles: