MFC:如何防止子 CWnd 对话框在调整父对话框 window 时跳转?
MFC: how to keep child CWnd dialog from jumping upon resizing the parent window?
我正在编写一个带有多个控件的 MFC dialog
。我目前有一个 CWnd
放在 dialog
的右半边。单击编辑按钮后,子 CWnd
会调整大小以占据对话框的较大部分。
但是,现在当我尝试调整 window 的大小时,子 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;
}
Upon clicking an edit button, the child CWnd is resized to take up a
larger portion of the dialog.
您必须在 OnSize()
(ON_WM_SIZE()
) 消息处理程序中处理相同的调整大小(使用某种 BOOL 成员来跟踪子 window 的状态) .
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);
}
}
我正在编写一个带有多个控件的 MFC dialog
。我目前有一个 CWnd
放在 dialog
的右半边。单击编辑按钮后,子 CWnd
会调整大小以占据对话框的较大部分。
但是,现在当我尝试调整 window 的大小时,子 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;
}
Upon clicking an edit button, the child CWnd is resized to take up a larger portion of the dialog.
您必须在 OnSize()
(ON_WM_SIZE()
) 消息处理程序中处理相同的调整大小(使用某种 BOOL 成员来跟踪子 window 的状态) .
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);
}
}