c++mfccwnd

MFC: how to keep child CWnd dialog from jumping upon resizing the parent window?


I'm writing an MFC dialog with multiple controls. I have currently have a CWnd that is placed on the right half of the dialog. Upon clicking an edit button, the child CWnd is resized to take up a larger portion of the dialog.

However, now when I try to resize the window, the child CWnd jumps back to where it was originally. I cannot seem to figure out how to keep it in it's new position when resizing.

Relevant code:

OnInit() {
    //the grouper rectangle
    CRect rectHTMLGrouper;
    m_grpHTMLbox.GetWindowRect(&rectHTMLGrouper);
    ScreenToClient(&rectHTMLGrouper);

    //the new rectangle to use for positioning
    CRect rectHtml;
    rectHtml.left = rectHTMLGrouper.left + PREVIEW_EDITOR_LEFT;
    rectHtml.right = rectHTMLGrouper.right - PREVIEW_EDITOR_RIGHT;
    rectHtml.top = rectHTMLGrouper.top + PREVIEW_EDITOR_TOP;
    rectHtml.bottom = rectHTMLGrouper.bottom - PREVIEW_EDITOR_BOTTOM;    

    //this inits my editor and sets the position 
    m_wHtmlEditor.CreateHtmlEditor(rectHTMLGrouper, this, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN);

    //CodeJock - XTREMEToolkit Call for SetResize Logic
    SetResize(m_wHtmlEditor.GetDlgCtrlID(), LEFT_PANE_RESIZE, 0, 1, 1);
    m_wHtmlEditor.SetWindowPos(&CWnd::wndTop, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOMOVE);
}


OnEditMode() {

    //enlarge the editor to take up the full dialog
    CRect parentClientRect;
    m_wHtmlEditor.GetParent()->GetClientRect(&parentClientRect);
    m_wHtmlEditor.SetWindowPos(&CWnd::wndTop, parentClientRect.left + edgePadding, parentClientRect.top + editorTopPadding, parentClientRect.right - (edgePadding * 2), parentClientRect.bottom - bottomPadding, SWP_NOOWNERZORDER);

    return;
}

Solution

  • Upon clicking an edit button, the child CWnd is resized to take up a larger portion of the dialog.

    You have to handle that same resize in your OnSize() (ON_WM_SIZE()) message handler (using some kind of BOOL member to keep track of the child window's status).

    OnSize() is called repeatedly while resizing the dialog.

    Example:

    // .h
    BOOL m_bIsEditMode;
    
    // .cpp
    // keep track of m_bIsEditMode
    
    void CMyDlg::OnSize(UINT nType, int cx, int cy)
    {
        CDialog::OnSize(nType, cx, cy);
    
        if (m_bIsEditMode) {
    
            //enlarge the editor to take up the full dialog
            m_wHtmlEditor.MoveWindow (0, 0, cx, cy);
        }
    }