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();
}

Comments
How can get toolbarbutton's event ? Please add that button's event handler , and give me
Posted by Legacy on 12/16/2002 12:00amOriginally posted by: kim yong kyu
How Can make button's event's hanlder in source .?
ReplyVery nice
Posted by Legacy on 10/11/2002 12:00amOriginally posted by: Ninaad
This is a very nice piece of code. I had tried trapping the TTN_NEEDTEXT event, but that didnt work. Excellent piece of code. thanks
ReplyWhen you have more than one CFormView the toolbar isn't displayed correctly... Fix.
Posted by Legacy on 09/06/2002 12:00amOriginally posted by: Jos� Leandro Massada
well... the toolbar buttons display grey if you click on another cformview (for example) the solution that worked in my case was not to let any view except the one with the toolbar to get activated, i just overloaded OnMouseActivate in those views like this:
int CSomeFormView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
pDesktopWnd->SetActiveWindow();
return MA_NOACTIVATE;//RHtmlView::OnMouseActivate(pDesktopWnd, nHitTest, message);
}
pDesktopWnd->SetActiveWindow() activates the top most window (in case the window is not active and the user clicks the view).
returning MA_NOACTIVATE doesn't allows the view to activate, but allows the mouse click to continue, so the view still gets the mouse click.
See MFC Documentation on OnMouseActivate for more info.
sorry for the quick, ugly, english, but i'm kinda busy :D
regards
btw, giving focus to the toolbar on OnInitialUpdate is also a good idea :)
Replyan object will be visible when an event (clicking a button) will take place
Posted by Legacy on 06/12/2002 12:00amOriginally posted by: Rohit Roy
Provide me this solution "an object will be visible when an event (clicking a button) will take place"
Replywhy the toolbar disappears when i change to another view(eg:IDD_FORMVIEW)
Posted by Legacy on 01/01/2002 12:00amOriginally posted by: david
i want it's always visible,how can i do?thanks a lot!
ReplyHow to use TTN_NEEDTEXT?
Posted by Legacy on 08/26/2000 12:00amOriginally posted by: Maxim A. Sidorov
It helped much, thanks. But it's a very hard to add each button into tooltip control. Have you ever try to implement EnableToolTip() & handler for TTN_NEEDTEXT ? It should be great!
ReplyNice
Posted by Legacy on 03/07/2000 12:00amOriginally posted by: Dave
Nicely done.
Replythis should be a great help
OnInitialUpdate too early for vertical toolbar
Posted by Legacy on 12/16/1999 12:00amOriginally posted by: Rick Shide
You're going to have to wait until after the mainframe is initially drawn to add tooltips that will work for verical scrollbars. The toolbar positions are not yet finalized.
e.g.
BOOL CToolBarsApp::InitInstance()
{
...
// The one and only window has been initialized ...
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
// NOW it's OK
ReplyCWnd* pWnd = m_pMainWnd->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CToolBarsView)))
{
CToolBarsView *pView = (CToolBarsView*)pWnd;
pView->FunctionToInitToolTipsInToolbars();
}
Works almost..
Posted by Legacy on 08/30/1999 12:00amOriginally posted by: Camilla
Hi!
I have had great help from your code to add toolbars to
ReplyCFormView. Unfortunately the tooltips won't work.
So I downloaded your demoprogram, and there all the tooltips, EXCEPT the ones on toolbar nr 3 button 1 and 2....works.
This is really strange, and I can't find the reason to this problem.
Maybe someone have an idea?
Won't let you load files
Posted by Legacy on 07/20/1999 12:00amOriginally posted by: Phil Cremer
The toolbar works great except for when you want to open an existing file.
ReplyLoading, Please Wait ...