状态栏总在CDialogBar上方的问题解决方法
场景: 一个VC单文档工程中在文档底部创建一个CDialogBar, 并且要求CDialogBar不能被拖放停靠,
这就是要实现如图的效果:
按照步骤创建CDialogBar, 发现状态栏在CDialogBar的上方. 如图所示
解决方法如下:
1. CMainFrame::OnCreate中创建代码
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_TOOLTIPS) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// RightBar的创建 注意创建风格
if (!m_RightBar.Create(this, CRightBar::IDD,
CBRS_RIGHT | CBRS_TOOLTIPS | CBRS_HIDE_INPLACE | CBRS_FLYBY | CBRS_FLOATING, CRightBar::IDD))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// BottomBar的创建 注意创建风格
if (!m_BottomBar.Create(this, CBottomBar::IDD,
CBRS_BOTTOM | CBRS_TOOLTIPS | CBRS_HIDE_INPLACE | CBRS_FLYBY | CBRS_FLOATING, CBottomBar::IDD))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
//
// 这下面两段代码就是使ControlBar停靠在状态栏上方的代码
// 如果不添加这两段代码, 状态栏就会乱跑.. 但是, 上面的创建代码创建ControlBar的属性
// 是不可拖放停靠的, 如果加了下面两段代码, ControlBar却变成了可以拖
// 放停靠, 可以悬浮出来.
// 现在的解决办法就是重载ControlBar的鼠标左键单击消息和鼠标左键双击消息.
m_RightBar.EnableDocking(CBRS_ALIGN_RIGHT);
EnableDocking(CBRS_ALIGN_RIGHT);
DockControlBar(&m_RightBar);
m_dlgBottomBar.EnableDocking(CBRS_ALIGN_BOTTOM);
EnableDocking(CBRS_ALIGN_BOTTOM);
DockControlBar(&m_BottomBar);
//
// TODO: Delete these three lines if you don't want the toolbar to be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
1. 重载ControlBar的鼠标左键单击消息和鼠标左键双击消息.
void CRightBar::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 屏蔽掉父类的OnLButtonDown
//CDialogBar::OnLButtonDown(nFlags, point);
}
void CRightBar::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 屏蔽掉父类的OnLButtonDblClk
//CDialogBar::OnLButtonDblClk(nFlags, point);
}
void CBottomBar::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 屏蔽掉父类的OnLButtonDown
//CDialogBar::OnLButtonDown(nFlags, point);
}
void CBottomBar::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 屏蔽掉父类的OnLButtonDblClk
//CDialogBar::OnLButtonDblClk(nFlags, point);
}
其实原理就是, 工具栏或者ControlBar的拖放停靠功能需要鼠标点击或者双击消息来处理的, 把它们拦截掉就行了.
如果拖放停靠有快捷键的话, 那就还要拦截快捷键
现在还没有找到治本的方法.
参考:(收到这篇文章的启发的)
http://www.vckbase.com/index.php/wv/359