Toolbars with Tooltips in a CFormView derived class | CodeGuru

Toolbars with Tooltips in a CFormView derived class

This is to demonstrate how to place toolbars with tooltips in a CFormView derived class. The Toolbar will be placed exactly inside the bounding rectangle of a CStatic control contained in the form view resource dialog. Then we must add the tooltips to each of the button by creating a CToolTipCtrl and adding each of […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 24, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

This is to demonstrate how to place toolbars with tooltips in a CFormView derived class.

The Toolbar will be placed exactly inside the bounding rectangle of a
CStatic control contained in the form view resource dialog.
Then we must add
the tooltips to each of the button by creating a CToolTipCtrl and adding each
of the tools in all of the toolbars
that we are going to place on the form.
Then we associate each of the toolbars with the tooltip control.

Here are the Steps:

(1) On the FormView resource Insert a CStatic control for each of the toolbars

you want to insert.

(2) Make each control of type frame (this is the default).

(3) On General properties uncheck the Visible checkbox.

(4) Add a CToolBar member to the CFormView derived class for each of the toolbars.

(5) Add a CToolTipCtrl member to the CFormView derived class.

(6) Override OnInitialUpdate for the CFormView derived class.(See following Code)

void CToolBarsView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	// Add the ToolBars.
	if (!m_toolBar1.Create( this ) ||
			!m_toolBar1.LoadToolBar(IDR_TOOLBAR1))
	{
			TRACE0(“Failed to create toolbar1\n”);
			return;      // fail to create
	}
	if (!m_toolBar2.Create( this ) ||
			!m_toolBar2.LoadToolBar(IDR_TOOLBAR2))
	{
			TRACE0(“Failed to create tube toolbar2\n”);
			return;      // fail to create
	}
	if (!m_toolBar3.Create( this ) ||
			!m_toolBar3.LoadToolBar(IDR_TOOLBAR3))
	{
			TRACE0(“Failed to create toolbar3\n”);
			return;      // fail to create
	}
	// For right and left toolbars use CBRS_ALIGN_RIGHT or CBRS_ALIGN_LEFT
	// otherwise use CBRS_ALIGN_ANY. Whichever you use it must match the
	// orientation of your toolbar which must match the correspondent CStatic
	// control orientation. Remember though that each toolbar will be fixed to
	// to the location of the CStatic control.
	m_toolBar1.SetBarStyle(CBRS_ALIGN_ANY | CBRS_TOOLTIPS | CBRS_FLYBY);
	m_toolBar2.SetBarStyle(CBRS_ALIGN_ANY | CBRS_TOOLTIPS | CBRS_FLYBY);
	m_toolBar3.SetBarStyle(CBRS_ALIGN_RIGHT | CBRS_TOOLTIPS | CBRS_FLYBY);
	WINDOWPLACEMENT wndPlmnt;
	// Get the window placements of each toolbar holder
	// and set each toolbar to its correspondent area.
	m_ctrlTB1Holder.GetWindowPlacement(&wndPlmnt);
	m_toolBar1.GetToolBarCtrl().SetWindowPlacement(&wndPlmnt);
	m_ctrlTB2Holder.GetWindowPlacement(&wndPlmnt);
	m_toolBar2.GetToolBarCtrl().SetWindowPlacement(&wndPlmnt);
	m_ctrlTB3Holder.GetWindowPlacement(&wndPlmnt);
	m_toolBar3.GetToolBarCtrl().SetWindowPlacement(&wndPlmnt);
	m_toolTip.Create(this, TTS_ALWAYSTIP);
	CRect rect;
	// Set tooltips for toolbar1
	m_toolBar1.GetToolBarCtrl().GetItemRect(0, rect);
	m_toolTip.AddTool(&m_toolBar1, ID_TB1_ONE, rect, ID_TB1_ONE);
	m_toolBar1.GetToolBarCtrl().GetItemRect(1, rect);
	m_toolTip.AddTool(&m_toolBar1, ID_TB1_TWO, rect, ID_TB1_TWO);
	m_toolBar1.GetToolBarCtrl().GetItemRect(2, rect);
	m_toolTip.AddTool(&m_toolBar1, ID_TB1_THREE, rect, ID_TB1_THREE);
	// Set tooltips for toolbar2
	m_toolBar2.GetToolBarCtrl().GetItemRect(0, rect);
	m_toolTip.AddTool(&m_toolBar2, ID_TB2_ONE, rect, ID_TB2_ONE);
	m_toolBar2.GetToolBarCtrl().GetItemRect(1, rect);
	m_toolTip.AddTool(&m_toolBar2, ID_TB2_TWO, rect, ID_TB2_TWO);
	m_toolBar2.GetToolBarCtrl().GetItemRect(2, rect);
	m_toolTip.AddTool(&m_toolBar2, ID_TB2_THREE, rect, ID_TB2_THREE);
	m_toolBar2.GetToolBarCtrl().GetItemRect(3, rect);
	m_toolTip.AddTool(&m_toolBar2, ID_TB2_FOUR, rect, ID_TB2_FOUR);
	// Set tooltips for toolbar3
	m_toolBar3.GetToolBarCtrl().GetItemRect(0, rect);
	m_toolTip.AddTool(&m_toolBar3, ID_TB3_ONE, rect, ID_TB3_ONE);
	m_toolBar3.GetToolBarCtrl().GetItemRect(1, rect);
	m_toolTip.AddTool(&m_toolBar3, ID_TB3_TWO, rect, ID_TB3_TWO);
	m_toolBar3.GetToolBarCtrl().GetItemRect(2, rect);
	m_toolTip.AddTool(&m_toolBar3, ID_TB3_THREE, rect, ID_TB3_THREE);
	// Associate ToolTipCtrl with ToolBars
	m_toolBar1.GetToolBarCtrl().SetToolTips(&m_toolTip);
	m_toolBar2.GetToolBarCtrl().SetToolTips(&m_toolTip);
	m_toolBar3.GetToolBarCtrl().SetToolTips(&m_toolTip);
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
}

Download demo project – (9 KB)

Download source – (21 KB)

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.