One. Both size and position change
1. First add CRect m_rect to the form class, which is used to record the current size of the form.
2. Inside the Class Wizard (Ctrl+w), add the response function of the message wm_size to the form onsize ();
Note if (ntype==1) return, this sentence must be added, otherwise the error will occur when the restore is minimized.
[CPP]? View Plain Copy
- void ? Cpapermanagementdlg::onsize (UINTnType,? int ? CX,? intcy)???
- {??
- ???? Cdialog::onsize (ntype,?cx,?cy);??
- ???? if(ntype==1)? return ; // Minimize it and 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 size 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
[plain]? View Plain Copy
- void? Cpapermanagementdlg::changesize (cwnd?*pwnd,?int?cx,?int?cy)??
- {??
- ???? if (pWnd)?? Determines whether it is empty because the dialog box is called when it is created, but the control has not yet been created ???
- ???? {??
- ???????? Crect?rect;??? Gets the size of the control before it changes ????
- ???????? Pwnd->getwindowrect (&rect);??
- ???????? ScreenToClient (&rect);// convert the control size to the area coordinates in the dialog box ??
- ???
- ???????? //???? Cx/m_rect. Width () for the dialog box in the horizontal variation ratio ??
- ???????? Rect.left=rect.left*cx/m_rect. Width ();// Adjust the size of the 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 changesize function, in OnSize constantly called in changesize function to change the size of all controls in the form.
——————————————————————————————————————————————————————
The following content is from the network:
Second,VC +adjusts the control position only based on the dialog box size
? ? 1, adding member variables to the dialog classCRect m_rect;used to save the size of the dialog box before the change size;
? ? 2, in the dialog box,OnInitDialog ()function to get the size of the dialog box when it was created:?
? ? GetClientRect (&m_rect);
? ? 3, inwm_sizeThe response functionOnSize ()Add the following code:
? ? CWnd *pwnd;?
? ? PWnd = GetDlgItem (Idc_button1);?? //Get control handle
? ? if (ntype==1) return;? //If the form is minimized then nothing is done
? ? if (pWnd)//determines whether it is empty because the dialog box is called when it is created, but the control is not yet created
? ? {?
? ? CRect rect;? //gets the size of the control before it changes
? ? ? LONG Cwidth,cheight; //record the distance from the right part 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 size of a control to an area coordinate in a 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 box size to the old size
C:\\MFC control size changes depending on the size of the form