Return to Snippet

Revision: 13698
at May 2, 2009 19:57 by jimfred


Initial Code
// 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.
// How to add OnSize:
// [1] add to .h:                     afx_msg void OnSize(UINT nType, int cx, int cy);
// [2] add to message map in .cpp:    ON_WM_SIZE()
// [3] add this OnSize function.
void CMyDlg::OnSize(UINT nType, int formWidthArg, int formHeightArg)
{
   CDialog::OnSize(nType, formWidthArg, formHeightArg); // Let dialog resize itself.

   // http://www.codersource.net/mfc_resize_controls.html
   // http://wwwusers.brookes.ac.uk/p0071643/resize.htm
   
   // get pointer to the control to be resized dynamically
   CWnd* pCtl = GetDlgItem(IDC_MSFLEXGRID1);
   
   if (!pCtl) { return; } // control may not exist yet.

   CRect rectCtl;                 // Allocate CRect for control's position.
   pCtl->GetWindowRect(&rectCtl); // Get control's position.
   ScreenToClient(&rectCtl);      // Convert from absolute screen coordinates to dialog-relative coordinates.

   // Now resize the control dynamically by calling MoveWindow
   // rectCtl.left is assumed to be the left, bottom and right margin for the control. 
   pCtl->MoveWindow(
      rectCtl.left,                               // x. remains unchanged
      rectCtl.top,                                // y. remains unchanged
      formWidthArg - 2 * rectCtl.left,            // w. Grow to fill horizontally
      formHeightArg - rectCtl.top - rectCtl.left, // h. Grow to fill vertically
      TRUE)
   ;

   return;
} // OnSize()

Initial URL

                                

Initial Description
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.

Initial Title
MFC, resize control in CDialog app

Initial Tags
c

Initial Language
C++