C:\\MFC control size changes depending on the size of the form

Source: Internet
Author: User

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

  1. void ? Cpapermanagementdlg::onsize (UINTnType,? int ? CX,? intcy)???
  2. {??
  3. ???? Cdialog::onsize (ntype,?cx,?cy);??
  4. ???? if(ntype==1)? return ; // Minimize it and do nothing. ??
  5. ???? //? Todo:? Add?your?message?handler?code?here??
  6. ???? Cwnd?*pwnd;???
  7. ???? Pwnd?=? GetDlgItem (idc_static);??
  8. ???? Changesize (pwnd,?cx,?cy);??
  9. ???? Pwnd?=? GetDlgItem (Idc_file_tree);??
  10. ???? Changesize (pwnd,?cx,?cy);??
  11. ???? Pwnd?=? GetDlgItem (idc_edit_name);??
  12. ???? Changesize (pwnd,?cx,?cy);??
  13. ???? Pwnd?=? GetDlgItem (idc_edit_reference);??
  14. ???? Changesize (pwnd,?cx,?cy);??
  15. ???? Pwnd?=? GetDlgItem (idc_edit_summary);??
  16. ???? Changesize (pwnd,?cx,?cy);??
  17. ???? Pwnd?=? GetDlgItem (Idc_edit_remark);??
  18. ???? Changesize (pwnd,?cx,?cy);??
  19. ???? Pwnd?=? GetDlgItem (idc_button_update);??
  20. ???? Changesize (pwnd,?cx,?cy);??
  21. ???? Pwnd?=? GetDlgItem (Idc_button_save);??
  22. ???? Changesize (pwnd,?cx,?cy);??
  23. ???? Pwnd?=? GetDlgItem (idc_static_1);??
  24. ???? Changesize (pwnd,?cx,?cy);??
  25. ???? Pwnd?=? GetDlgItem (idc_static_2);??
  26. ???? Changesize (pwnd,?cx,?cy);??
  27. ???? Pwnd?=? GetDlgItem (idc_static_3);??
  28. ???? Changesize (pwnd,?cx,?cy);??
  29. ???? Pwnd?=? GetDlgItem (idc_static_4);??
  30. ???? Changesize (pwnd,?cx,?cy);??
  31. ???? GetClientRect (&m_rect); //? set the changed dialog box size to the old size ??? ??
  32. }??


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

  1. void? Cpapermanagementdlg::changesize (cwnd?*pwnd,?int?cx,?int?cy)??
  2. {??
  3. ???? 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 ???
  4. ???? {??
  5. ???????? Crect?rect;??? Gets the size of the control before it changes ????
  6. ???????? Pwnd->getwindowrect (&rect);??
  7. ???????? ScreenToClient (&rect);// convert the control size to the area coordinates in the dialog box ??
  8. ???
  9. ???????? //???? Cx/m_rect. Width () for the dialog box in the horizontal variation ratio ??
  10. ???????? Rect.left=rect.left*cx/m_rect. Width ();// Adjust the size of the control ??
  11. ???????? Rect.right=rect.right*cx/m_rect. Width ();??
  12. ???????? Rect.top=rect.top*cy/m_rect. Height ();??
  13. ???????? Rect.bottom=rect.bottom*cy/m_rect. Height ();??
  14. ???????? Pwnd->movewindow (rect);// set control size ??
  15. ????}??
  16. }??

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

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.