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 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)

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read