Displaying text on a Toolbar

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;
	TBBUTTON	tb;

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

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

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

		strText.LoadString(tb.idCommand);

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

			while(lpszButtonText)
			{
				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);

		rc.NormalizeRect();
		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



Comments

  • how can i obtain the code

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

    s

    Reply
  • Little enhancement

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

    Originally posted by: Radu

    Hello,

    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:

    SetSizes(sizeMax,CSize(imageCxSize,imageCySize));


    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.

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

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

    Originally posted by: Martin Mace

    Hi,

    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.

    Martin

    Reply
  • Displaying text on a Toolbar

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

    Originally posted by: Jafeel R

    Hi
    
    

    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.

    jafeel

    Reply
  • How to Hide/Show ToolText?

    Posted by Legacy on 10/09/2000 12: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

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

    Posted by Legacy on 03/31/2000 12: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!

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

    Posted by Legacy on 01/15/2000 12: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.

    Sincerely,
    Erich J. Ruth

    Reply
  • How can I change the font??

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

    Originally posted by: Volkan

    Title says it all.
    Please help.

    Thanks.

    -V-

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

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

    Originally posted by: Carl

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

    Reply
  • Text set but not displayed bug

    Posted by Legacy on 05/19/1999 12: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.

    Brian

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

  • On-demand Event Event Date: August 27, 2015 With the cloud enabling companies to spin up servers and stand up data stores more quickly, mobile apps can be created faster, reducing the time-to-value. But three major obstacles stand in the way for many organizations: The backlog of app project requests confronting every enterprise regardless of their internal app development capabilities Finding and employing better, faster tools to speed and simplify the process of developing those apps. The emergence of …

Most Popular Programming Stories

More for Developers

RSS Feeds

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