Implementing Docking control Bar in Child Frame

In a multiple view application, sometimes I need to implement different control bars for different views. This article presents how to implement docking control bar in a child frame. Suppose we want to embed a tool bar and a status bar in a child frame. First, declare them in ChildFrm.h:


protected:  // control bar embedded members
	...
	CStatusBar m_wndStatusBar;
	CToolBar m_wndToolBar;
	static UINT indicators[4]; // indicators of status bar


Next, over write CChildFrame::OnCreate(). Create control bars just as we do in CmainFrame.


int CChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO: Add your specialized creation code here
	if (!m_wndToolBar.Create(this) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1; // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\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;
}

Then write the array of status bar' indicators as following:


UINT CChildFrame::indicators[4] =
{
	ID_SEPARATOR, // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

Now you can build the project and play with it. However, the menu item "Tool Bar" does not work properly at this stage. When there is any child frame in the main frame, the menu effects the tool bar in the child frame only. You can't toggle the show/hide of the tool bar in the main frame until you close all the child frames. Now I am going to create 2 new menu terms, Main Frame Tool Bar, and Child Frame Tool Bar, to toggle show/hide of the 2 tool bars. Use ClassWizard to add following 2 member functions in CMainFrame.


void CMainFrame::OnViewMainframetoolbar() 
{
	// TODO: Add your command handler code here
	if(m_wndToolBar.IsWindowVisible())
		m_wndToolBar.ShowWindow(SW_HIDE);
	else
		m_wndToolBar.ShowWindow(SW_SHOW);
	this->SendMessage(WM_SIZE, 0, 0);
}

void CMainFrame::OnUpdateViewMainframetoolbar(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_wndToolBar.IsWindowVisible());
}

Similarly add following member functions in CChildFrame.


void CChildFrame::OnViewChildframetoolbar() 
{
	// TODO: Add your command handler code here
	if(m_wndToolBar.IsWindowVisible())
		m_wndToolBar.ShowWindow(SW_HIDE);
	else
		m_wndToolBar.ShowWindow(SW_SHOW);
	this->SendMessage(WM_SIZE, 0, 0);
}

void CChildFrame::OnUpdateViewChildframetoolbar(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_wndToolBar.IsWindowVisible());	
}

Build the project and see how the menu items work. The same technique works for ReBar. If you have any qestion, feel free to drop me a line.

Download demo project - 23KB