因为 CMDIChildWnd CFrameWnd 派生的类,MDI 子窗口中的工具栏可能是可停靠。这也很容易实现通过调用 EnableDocking() DockControlBar() 以及此类函数。
示例代码
在 MDI 子窗口中创建一个工具栏的步骤:- 创建使用类向导 (如果缺少一个 CMDIChildWnd 派生类。 请记住在 CWinApp::InitInstance() 函数 CMultiDocTemplate 语句中使用此派生的类。
- 将类型 CToolBar 的成员变量添加到 CMDIChildWnd 派生类中。
class CChildFrame : public CMDIChildWnd { ... CToolBar m_wndToolBar; }
- 重写 OnCreate() 函数使用类向导您 CMDIChildWnd 派生类中。
- 下面的代码添加到 OnCreate() CMDIChildWnd-派生类的函数:
int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1) return -1; // Create a toolbar window. IDR_CHILDFRAME is the resource name // of the toolbar to be loaded. if (!m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_CHILDFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } // TODO: Remove this if you don't want tool tips or a // resizeable toolbar m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); // 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; }