MFC, resize control in CDialog app


/ Published in: C++
Save to your folder(s)

This OnSize function resizes one large control in a dialog.
The one control grows horizontally and vertically to fill the dialog. It's position remains unchanged.
Other controls (buttons etc) would typically be above the one resizable control.


Copy this code and paste it in your HTML
  1. // This OnSize function resizes one large control in a dialog.
  2. // The one control grows horizontally and vertically to fill the dialog. It's position remains unchanged.
  3. // Other controls (buttons etc) would typically be above the one resizable control.
  4. // How to add OnSize:
  5. // [1] add to .h: afx_msg void OnSize(UINT nType, int cx, int cy);
  6. // [2] add to message map in .cpp: ON_WM_SIZE()
  7. // [3] add this OnSize function.
  8. void CMyDlg::OnSize(UINT nType, int formWidthArg, int formHeightArg)
  9. {
  10. CDialog::OnSize(nType, formWidthArg, formHeightArg); // Let dialog resize itself.
  11.  
  12. // http://www.codersource.net/mfc_resize_controls.html
  13. // http://wwwusers.brookes.ac.uk/p0071643/resize.htm
  14.  
  15. // get pointer to the control to be resized dynamically
  16. CWnd* pCtl = GetDlgItem(IDC_MSFLEXGRID1);
  17.  
  18. if (!pCtl) { return; } // control may not exist yet.
  19.  
  20. CRect rectCtl; // Allocate CRect for control's position.
  21. pCtl->GetWindowRect(&rectCtl); // Get control's position.
  22. ScreenToClient(&rectCtl); // Convert from absolute screen coordinates to dialog-relative coordinates.
  23.  
  24. // Now resize the control dynamically by calling MoveWindow
  25. // rectCtl.left is assumed to be the left, bottom and right margin for the control.
  26. pCtl->MoveWindow(
  27. rectCtl.left, // x. remains unchanged
  28. rectCtl.top, // y. remains unchanged
  29. formWidthArg - 2 * rectCtl.left, // w. Grow to fill horizontally
  30. formHeightArg - rectCtl.top - rectCtl.left, // h. Grow to fill vertically
  31. TRUE)
  32. ;
  33.  
  34. return;
  35. } // OnSize()

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.