【问题标题】:MFC: how to keep child CWnd dialog from jumping upon resizing the parent window?MFC:如何防止子 CWnd 对话框在调整父窗口大小时跳转?
【发布时间】:2016-04-26 17:47:01
【问题描述】:

我正在编写一个带有多个控件的MFC dialog。我目前有一个CWnd,它位于dialog 的右半部分。单击编辑按钮后,子 CWnd 会调整大小以占据对话框的更大部分。

但是,现在当我尝试调整窗口大小时,子 CWnd 会跳回原来的位置。我似乎无法弄清楚如何在调整大小时将其保持在新位置。

相关代码:

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;
}

【问题讨论】:

    标签: c++ mfc cwnd


    【解决方案1】:

    单击编辑按钮后,子 CWnd 会调整大小以占用 对话框的较大部分。

    您必须在 OnSize() (ON_WM_SIZE()) 消息处理程序中处理相同的调整大小(使用某种 BOOL 成员来跟踪子窗口的状态)。

    OnSize() 在调整对话框大小时被重复调用。

    例子:

    // .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);
        }
    }
    

    【讨论】:

    • 你的意思是,在调整大小时,我必须不断计算父客户端的矩形并设置孩子的位置?最初创建对话框时,孩子如何调整大小?你能解释一下为什么它只有在我给孩子打电话SetWindowPos() 后才跳回原来的位置吗?
    • @Busch - 1) OnSize() 具有参数 cx、cy。致电SetWindowPos()MoveWindow() 应该可以。 2)原始(静态)位置保存在资源中。因此,当父级调整大小时,所有控件都被绘制/放置在该原始位置。
    • @Busch - 我添加了一个小例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多