Virtual Developer Workshop: Containerized Development with Docker

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

	// Add the ToolBars.	
	if (!m_toolBar1.Create( this ) ||
			TRACE0("Failed to create toolbar1\n");
			return;      // fail to create	

	if (!m_toolBar2.Create( this ) ||
			TRACE0("Failed to create tube toolbar2\n");
			return;      // fail to create	

	if (!m_toolBar3.Create( this ) ||
			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.


	// Get the window placements of each toolbar holder
	// and set each toolbar to its correspondent area.



	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


Download demo project - (9 KB)

Download source - (21 KB)


  • How can get toolbarbutton's event ? Please add that button's event handler , and give me

    Posted by Legacy on 12/16/2002 08:00am

    Originally posted by: kim yong kyu

    How Can make button's event's hanlder in source .?

  • Very nice

    Posted by Legacy on 10/11/2002 07:00am

    Originally 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

  • When you have more than one CFormView the toolbar isn't displayed correctly... Fix.

    Posted by Legacy on 09/06/2002 07:00am

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


    btw, giving focus to the toolbar on OnInitialUpdate is also a good idea :)

  • an object will be visible when an event (clicking a button) will take place

    Posted by Legacy on 06/12/2002 07:00am

    Originally posted by: Rohit Roy

    Provide me this solution "an object will be visible when an event (clicking a button) will take place"

  • why the toolbar disappears when i change to another view(eg:IDD_FORMVIEW)

    Posted by Legacy on 01/01/2002 08:00am

    Originally posted by: david

    i want it's always visible,how can i do?thanks a lot!

  • How to use TTN_NEEDTEXT?

    Posted by Legacy on 08/26/2000 07:00am

    Originally 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!

  • Nice

    Posted by Legacy on 03/07/2000 08:00am

    Originally posted by: Dave

    Nicely done.
    this should be a great help

  • OnInitialUpdate too early for vertical toolbar

    Posted by Legacy on 12/16/1999 08:00am

    Originally 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.


    BOOL CToolBarsApp::InitInstance()
    // The one and only window has been initialized ...

    // NOW it's OK
    CWnd* pWnd = m_pMainWnd->GetDescendantWindow(AFX_IDW_PANE_FIRST, TRUE);
    if (pWnd != NULL && pWnd->IsKindOf(RUNTIME_CLASS(CToolBarsView)))
    CToolBarsView *pView = (CToolBarsView*)pWnd;

  • Works almost..

    Posted by Legacy on 08/30/1999 07:00am

    Originally posted by: Camilla


    I have had great help from your code to add toolbars to
    CFormView. 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 07:00am

    Originally posted by: Phil Cremer

    The toolbar works great except for when you want to open an existing file.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date