1

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;
}
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
Busch
  • 857
  • 10
  • 29

1 Answers1

1

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);
    }
}
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • You mean, upon resizing I have to continuously calculate the parent client rect and set the position of the child? How come when the dialog is initially created, the child resizes fine? Can you explain why it jumps back to it's original position only after I call `SetWindowPos()` on the child? – Busch Jan 20 '16 at 18:31
  • 1
    @Busch - 1) OnSize() has the parameters cx, cy. Calling `SetWindowPos()` or `MoveWindow()` should do. 2) The original (static) position is saved in the resource. So when the parent is resized, all the controls are drawn/placed on that original position. – Danny_ds Jan 20 '16 at 18:42
  • 1
    @Busch - I added a small example. – Danny_ds Jan 20 '16 at 18:45