Displaying text on a Toolbar


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

This article was contributed by Michael Brannan.

Here is a little bit of code for adding text to your toolbars. In the sample there is a CToolBar derived class that overrides LoadToolBar(...) and adds the text to the toolbar according to the command id of the button. The text added in the example is the last line of text from the resource string whose id is the same as the command id of the button. So for ID_FILE_NEW we have the standard string "Create a new document\nNew" which would give us a button text of "New" where as if we change ID_FILE_NEW to "Create a new document\nNew\nNew File" this will cause "New File" to be displayed on the toolbar button.

After the button text has been added the buttons are resized so that you can see the text proplerly. The main code for this is as follows:

BOOL CTextToolBar::LoadToolBar(LPCTSTR lpszResourceName)
	BOOL bReturn = CToolBar::LoadToolBar(lpszResourceName);

	// Check if we loaded the toolbar.
	if (bReturn == FALSE)
		return bReturn;

	// Make it flat.
	ModifyStyle(0, GetStyle()|TBSTYLE_FLAT);

	// Set the text for each button
	CToolBarCtrl& bar = GetToolBarCtrl();

	// Remove the string map in case we are loading another toolbar into this control
	if (m_pStringMap)
		delete m_pStringMap;
		m_pStringMap = NULL;

	int		nIndex = 0;

	for (nIndex = bar.GetButtonCount() - 1; nIndex >= 0; nIndex--)
		ZeroMemory(&tb, sizeof(TBBUTTON));
		bar.GetButton(nIndex, &tb);

		// Do we have a separator?
		if ((tb.fsStyle & TBSTYLE_SEP) ==  TBSTYLE_SEP)

		// Have we got a valid command id?
		if (tb.idCommand == 0)

		// Get the resource string if there is one.
		CString strText;
		LPCTSTR lpszButtonText = NULL;
		CString	strButtonText(_T(""));
		_TCHAR	seps[] = _T("\n");


		if (!strText.IsEmpty())
			lpszButtonText = _tcstok((LPTSTR)(LPCTSTR)strText, seps);

				strButtonText = lpszButtonText;
				lpszButtonText = _tcstok(NULL, seps);

		if (!strButtonText.IsEmpty())
			SetButtonText(nIndex, strButtonText);

	// Resize the buttons so that the text will fit.
	CRect rc(0, 0, 0, 0);
	CSize sizeMax(0, 0);

	for (nIndex = bar.GetButtonCount() - 1; nIndex >= 0; nIndex--)
		bar.GetItemRect(nIndex, rc);

		sizeMax.cx = __max(rc.Size().cx, sizeMax.cx);
		sizeMax.cy = __max(rc.Size().cy, sizeMax.cy);
	SetSizes(sizeMax, CSize(16,15));

	return bReturn;

Last updated: 4 December 1998


  • how can i obtain the code

    Posted by liujuejue on 04/20/2006 07:00am


  • Little enhancement

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

    Originally posted by: Radu


    I would change the definition of the LoadToolBar method by adding two input parameters:

    BOOL CTextToolBar::LoadToolBar(LPCTSTR lpszResourceName, int imageCxSize, int imageCySize)

    where the two additional parameters mean the size of the image in the button. further SetSizes should be changed into:


    This allows to fit the toolbar to the particular image size used(when you create the toolbar buttons the sizes (found in View->Properties) may differ from user to user).
    Also, if you have more toolbars of different sizes this allow to display all of them correctly.

  • How to combine buttons with bitmap and text and just bitmap toolbar buttons

    Posted by Legacy on 02/07/2002 08:00am

    Originally posted by: Martin Mace


    Like IE 6.0, I want to combine buttons with bitmap and text and just plain bitmap toolbar buttons on the same row (the text should appear next to the bitmap, not beneath it).
    The problem is that when I set the text for one button, it seems that it's set for all of them (at least the space for the text is allocated).

    I just somehow want to adjust the size of each button.

    Does anybody know a nice way of how to handle this?

    Thanks in advance.


  • Displaying text on a Toolbar

    Posted by Legacy on 06/14/2001 07:00am

    Originally posted by: Jafeel R


    Is it possible to add a bitmap to a button in the toolbar at runtime, if so how is it possible

    Is it possible to add a drop down button in a toolbar with a bitmap at run time.


  • How to Hide/Show ToolText?

    Posted by Legacy on 10/09/2000 07:00am

    Originally posted by: Sukhanandan

    I used
    m_wndToolBar.SetSizes(sizeButton, sizeImage);

    on ID_VIEW_TOOLTEXT menu item.
    It sets exact size but changes automatically after any COMMAND_UI calls

  • Text won't show if there is only one button in the toobar

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

    Originally posted by: Yi Ren

    Text won't show if there is only one button in the toobar. I added a second button in the toolbar, and it works. I would like to know why. Thanks!

  • I don't know where to put the code?

    Posted by Legacy on 01/15/2000 08:00am

    Originally posted by: Erich Ruth

    You have the following function:

    BOOL CTextToolBar::LoadToolBar(LPCTSTR lpszResourceName)

    Am I supposed to put this function within MainFrame.cpp, so I would have

    BOOL CMainFrame::LoadToolBar(LPRCTSTR lpszResourceName)

    and then call this function in CMainFrame::OnCreate().

    Please, I can't get the code to work because I am not certain where to put it or where and how to call it. Any response you can give me will be greatly appreciated.

    Erich J. Ruth

  • How can I change the font??

    Posted by Legacy on 09/27/1999 07:00am

    Originally posted by: Volkan

    Title says it all.
    Please help.



  • Why the text is sit at the right of image, not the bottom?

    Posted by Legacy on 09/24/1999 07:00am

    Originally posted by: Carl

    Why the text is sit at the right of image, not the bottom?

  • Text set but not displayed bug

    Posted by Legacy on 05/19/1999 07:00am

    Originally posted by: Brian Contario

    There's a bug in the Cmnctr32 code, I think.  If you set the text of any of the buttons, and then set one of
    the buttons to NULL, none of the buttons will have text displayed, even though they are adjusted to the
    correct size.  You can do this blatantly:

    SetButtonText(nIndex, NULL));

    or not so blatantly:

    std::string buttontext;
    // Never initialize 'buttontext', assuming an empty
    // string is a safe default
    SetButtonText(nIndex, buttontext.c_str());
    // buttontext.c_str() returns (char*)0x00000000 here!

    Either way I believe you don't get any text, at least with the common control DLL that I'm using from VC6 and IE4.01 sp1.


  • 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