为啥我的 CMFCToolBar 在 CDialog 上重叠?
Posted
技术标签:
【中文标题】为啥我的 CMFCToolBar 在 CDialog 上重叠?【英文标题】:Why is my CMFCToolBar overlapped on CDialog?为什么我的 CMFCToolBar 在 CDialog 上重叠? 【发布时间】:2019-12-30 20:39:16 【问题描述】:我在这里处于设计阶段。
代码:
if (m_ToolBar.CreateEx(this, TBSTYLE_TRANSPARENT | TBSTYLE_FLAT,
AFX_DEFAULT_TOOLBAR_STYLE, CRect(1, 1, 1, 1), IDR_TOOLBAR))
m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle()
& ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_ANY));
m_ToolBar.SetBorders();
m_ToolBar.LoadBitmap(IDB_BMP_SM_IMAGELIST);
m_ToolBar.InsertButton(CMFCToolBarButton(ID_FILE_OPEN_CHRISTIAN_LIFE_AND_MINISTRY_REPORT, 0, _T("Open MWB"), TRUE));
m_ToolBar.InsertButton(CMFCToolBarButton(ID_FILE_CREATE_CHRISTIAN_LIFE_AND_MINISTRY_REPORT, 0, _T("Create MWB"), TRUE));
m_ToolBar.InsertSeparator(2);
m_ToolBar.InsertButton(CMFCToolBarButton(ID_FILE_OPENREPORT, 0, _T("Open SRR"), TRUE));
m_ToolBar.InsertButton(CMFCToolBarButton(ID_FILE_CREATEREPORT, 0, _T("Create SRR"), TRUE));
m_ToolBar.InsertSeparator(5);
m_ToolBar.InsertButton(CMFCToolBarButton(ID_OPTIONS_PUBLISHERS_DATABASE, 0, _T("Publishers Database"), TRUE));
CSize sizeToolBar = m_ToolBar.CalcFixedLayout(FALSE, TRUE);
m_ToolBar.SetWindowPos(NULL, 0, 10, sizeToolBar.cx, sizeToolBar.cy,
SWP_NOACTIVATE | SWP_NOZORDER);
我在我的CDialog
中的OnInitDialog
中调用上述内容。
工具栏为什么重叠:
更新
我简化了代码,只使用了一个基本的工具栏资源:
if (m_ToolBar.CreateEx(this, TBSTYLE_TRANSPARENT | TBSTYLE_FLAT,
AFX_DEFAULT_TOOLBAR_STYLE, CRect(1, 1, 1, 1), IDR_TOOLBAR))
m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle()
& ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_ANY));
m_ToolBar.SetBorders();
m_ToolBar.LoadToolBar(IDR_TOOLBAR);
CSize sizeToolBar = m_ToolBar.CalcFixedLayout(TRUE, TRUE);
m_ToolBar.SetWindowPos(NULL, 0, 10, sizeToolBar.cx, sizeToolBar.cy,
SWP_NOACTIVATE | SWP_NOZORDER);
但结果还是一样。
更新
我注意到,如果我只是在 IDE 中调整对话框的大小,我就有了工具栏的空间。但是如何在 IDE 中准确地调整它的大小以允许 32 像素高的工具栏?
【问题讨论】:
【参考方案1】:有人提供了此存档article 的链接。
这是我更新的代码(来自OnInitDialog
):
void CMeetingScheduleAssistantDlg::CreateToolbar()
DWORD dwCtrlStyle = TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CBRS_SIZE_DYNAMIC;
DWORD dwStyle = AFX_DEFAULT_TOOLBAR_STYLE;
if (m_ToolBar.CreateEx(this, dwCtrlStyle,
dwStyle, CRect(1, 1, 1, 1), IDR_TOOLBAR))
dwStyle = CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC;
m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle() | dwStyle);
CMFCToolBarInfo info;
info.m_uiColdResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiHotResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiLargeColdResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiLargeHotResID = IDB_PNG_MAIN_TOOLBAR;
m_ToolBar.LoadToolBarEx(IDR_TOOLBAR, info, FALSE);
CSize sizeToolBar = m_ToolBar.CalcFixedLayout(TRUE, TRUE);
m_ToolBar.SetWindowPos(NULL, 0, 0, sizeToolBar.cx, sizeToolBar.cy,
SWP_NOACTIVATE | SWP_NOZORDER);
// Move all controls down
CPoint ptOffset(0, sizeToolBar.cy);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
if (pwndChild->GetSafeHwnd() != m_ToolBar.GetSafeHwnd())
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
// Resize the window
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.bottom += sizeToolBar.cy;
MoveWindow(rcWindow, FALSE);
简而言之,我必须手动向下移动所有控件并调整窗口大小。现在看起来不错:
【讨论】:
以上是关于为啥我的 CMFCToolBar 在 CDialog 上重叠?的主要内容,如果未能解决你的问题,请参考以下文章
如何动态创建CMFCToolbar的SetOriginalState?