MFC Resizing controls

There are times when we need to dynamically resize the controls inside the Windows/Dialogs. This article tries to give a small sample of how to resize a Tree Control inside a CFormView derived class dynamically. This can then be extended to all other MFC Controls.

The following article assumes the application is already created and Applications, dialogs and controls are in place. It tries to resize a Tree control dynamically whenever the ON_WM_SIZE message is dispatched to the Application.

void CControllerView::OnSize(UINT nType, int cx, int cy) 
{

    CFormView::OnSize(nType, cx, cy);

    // Declare a CRect to get the co-ordinates
    CRect l_formRect;
    GetClientRect(&l_formRect);

    // get pointer to the control to be resized dynamically
    CTreeCtrl* pTreeCtrl;

    pTreeCtrl = (CTreeCtrl *)GetDlgItem(IDC_TREEFOLDER);

    //Calculate the new width of the control to be resized
    long newWidthTree = l_formRect.Width()/3;

    if(newWidthTree > 250)
       newWidthTree = 250;

    long newWidthTab = l_formRect.Width() - newWidthTree ;
    long newHeightTree = l_formRect.Height();

    // Now resize the control dynamically by calling MoveWindow
    pTreeCtrl->MoveWindow(l_formRect.TopLeft().x+5, l_formRect.TopLeft().y+25, (long)newWidthTree,    (long)newHeightTree-25, TRUE);

    // repaint control
    pTreeCtrl->RedrawWindow(); 

 }
}