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

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join Seagate Cloud …

  • Corporate e-Learning technology has a long and diverse pedigree. As far back as the 1980s, companies were adopting computer-based training to supplement traditional classroom activities. More recently, rich web-based applications have added streaming audio and video, real-time collaboration and other new tools to the e-Learning mix. At the same time, the growing availability of informal learning tools--a category that includes everything from web searches to social media posts--are having a major impact on …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds