MFC control size changes _c language with form size

Source: Internet
Author: User

One. Change in size and position

1. First add the CRect m_rect for the form class, which is used to record the current size of the form.

2. In the Class Wizard (Ctrl+w), add the message wm_size response function OnSize () to the form;

Note if (ntype==1) return; This sentence must be added, otherwise the minimum recovery will be an error.

void Cpapermanagementdlg::onsize (UINT ntype, int cx, int cy) {cdialog::onsize (ntype, CX, CY);  
  if (ntype==1) return;//Minimize then do nothing//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 box to the old size} 

 

Where the function changesize is a manually added user function, the steps are as follows

3. Add a user function that changes the size of the control, void Changesize (CWnd *pwnd, int cx, int cy), and write code

void Cpapermanagementdlg::changesize (CWnd *pwnd, int cx, int cy) 
{ 
  if (pwnd)//To determine whether it is empty because this function is called when the dialog box is created, and the control is not yet created  
  { 
    CRect rect;  Gets the size  
    pwnd->getwindowrect (&rect) before the control changes; 
    ScreenToClient (&rect);//Converts the control size to the regional coordinates//Cx/m_rect in the dialog box  . Width () For the dialog box in the horizontal variation ratio 
    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 constantly calls the Changesize function in OnSize to change the size of all the controls in the form.

——————————————————————————————————————————————————————

The following comes from the network:

Second, VC + + based on the dialog box size only adjust the control position

1, add the member variable CRect m_rect in the dialog class; used to save the size of the dialog box before size changes;
2, in the dialog box OnInitDialog () function to get the size of the dialog box when created:
GetClientRect (&m_rect);
3, in the wm_size response function OnSize () Add the following code:

  CWnd *pwnd; 
  pwnd = GetDlgItem (idc_button1);   Gets the control handle
  if (ntype==1) return;  If the form is minimized, nothing is done if
  (pwnd)/Is NULL, because this function is called when the dialog box is created, and the control has not yet created
  { 
  CRect rect;  Gets the size of a
   LONG cwidth,cheight//record control's right hand to the right of the form, recording 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 regional coordinate cwidth=m_rect in the dialog box
   . 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),//Setting control size
  }
  getclientrect (&m_rect);//Set the changed dialog box to the old size

The above mentioned is the entire content of this article, I hope you can enjoy.

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.