Toolbar with text on right.

I'm trying to put text on the right side on some toolbar buttons. The text is displayed if the button is enabled, but if it's disabled there is no text displayed. Also, if the button is enabled and the text shows, and I then disable it, the text is replaced with junk characters, and even if I enable it again, the text is still junk. If I use GetButtonInfo and look at the text, it really is junk, why do this happen? What have I missed? Here's the code I use:

//Create the toolbar
     if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_LIST | CCS_ADJUSTABLE, WS_CHILD | WS_VISIBLE | CBRS_TOP
          | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_FIXED ) ||
          !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
     {
          TRACE0("Failed to create toolbar\n");
          return -1;      // fail to create
     }

// Add text to some buttons, TRUE indicates display text.
     m_wndToolBar.SetButtonTextEx ( ID_FILE_NEW, _T("New"), TRUE );
     m_wndToolBar.SetButtonTextEx ( ID_FILE_OPEN, _T("Open"), TRUE );
     m_wndToolBar.SetButtonTextEx ( ID_FILE_SAVE, _T("Save"), TRUE );
     m_wndToolBar.SetButtonTextEx ( ID_EDIT_CUT, _T("Cut"), FALSE );
     m_wndToolBar.SetButtonTextEx ( ID_EDIT_COPY, _T("Copy"), FALSE );
     m_wndToolBar.SetButtonTextEx ( ID_EDIT_PASTE, _T("Paste"), FALSE );
     m_wndToolBar.SetButtonTextEx ( ID_FILE_PRINT, _T("Print"), FALSE );
     m_wndToolBar.SetButtonTextEx ( ID_APP_ABOUT, _T("About..."), TRUE );

// SetButtonTextEx sets the text on the buttons.
// CTbInfo is a ownerdefined class, to add as lParam on each button.
BOOL CToolBarEx::SetButtonTextEx ( UINT uButtonID, LPCTSTR lpszButtonText, BOOL bShowSelectiveText /*= FALSE*/ )
{
     CTbInfo* lpTbInfo = new CTbInfo;
     if ( lpTbInfo == NULL )
          return FALSE;

     lpTbInfo->m_strButtonText = lpszButtonText;

     TBBUTTONINFO tbInfo = { 0 };
     tbInfo.cbSize = sizeof ( TBBUTTONINFO );
     tbInfo.dwMask = TBIF_STATE;
     GetToolBarCtrl ().GetButtonInfo ( uButtonID, &tbInfo );

     tbInfo.dwMask = TBIF_TEXT | TBIF_STYLE | TBIF_LPARAM | TBIF_STATE;
     tbInfo.pszText = bShowSelectiveText ? (LPTSTR)lpszButtonText : 0x00;
     tbInfo.cchText = bShowSelectiveText ? _tcslen ( lpszButtonText ) : 0;
     tbInfo.fsStyle = TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE;
     tbInfo.lParam = (DWORD_PTR)lpTbInfo;

     return GetToolBarCtrl ().SetButtonInfo ( uButtonID, &tbInfo );
}

Anyone has any ideas? Thanks.

/Joakim
LVL 1
joakimfAsked:
Who is Participating?
 
Roshan DavisConnect With a Mentor Commented:
MSDN Says

A toolbar can display text labels in two different ways: below the button icon or to the right of the icon. In order to display it at the right of the icon the style bit TBSTYLE_LIST needs to be specified when creating the toolbar.

The following steps show how to add button labels and resize the standard toolbar for an AppWizard generated MFC SDI or MDI application. The code handles the case of a regular toolbar or a toolbar with the list style set. TBSTYLE_LIST raises one problem that you need to be aware of: when hiding the text labels you need to remove this style bit in order for the button icons to be drawn correctly.



Create an SDI or MDI MFC AppWizard project.


Add a class derived from CToolBar to the project; in the code below, this is CMyToolBar.


Copy the following code to the .cpp file for the CMyToolBar class.



   struct tbblabel
   {
      UINT id;
      char *label;
   };

   static tbblabel standardBarLabels[] =
   {
      { ID_FILE_NEW, "File New" },
      { ID_FILE_OPEN, "File Open" },
      { ID_FILE_SAVE, "File Save" },
      { ID_EDIT_CUT, "Cut" },
      { ID_EDIT_COPY, "Copy" },
      { ID_EDIT_PASTE, "Paste" },
      { ID_FILE_PRINT, "Print" },
      { ID_APP_ABOUT, "About" }
   };

   void CMyToolBar::SetLabels(BOOL bShow /*= TRUE*/)
   {
      if (m_rBigBtn.Size() != CSize(0, 0))
         return;

      // Get the first button size before adding the labels.
      GetItemRect(0, &m_rSmallBtn);

      int cButtons = sizeof(standardBarLabels) / sizeof(standardBarLabels[0]);
      for( int nButton = 0; nButton < cButtons; nButton++ )
      {
         int nButtonIndex = CommandToIndex( standardBarLabels[nButton].id );
         if (!SetButtonText( nButtonIndex, standardBarLabels[nButton].label ))
            TRACE("failed SetButtonText(%d, \"%s\").\n",
                   nButtonIndex, standardBarLabels[nButton].label);
      }

      // Get the first button size after adding the labels.
      GetItemRect(0, &m_rBigBtn);

      ShowLabels(bShow);
   }

   void CMyToolBar::ShowLabels(BOOL bShow /*= TRUE*/)
   {
      // Remember that if the toolbar had the TBSTYLE_LIST set
      //    the first time ShowLabels() was called.
      static BOOL bIsList = GetStyle() & TBSTYLE_LIST;
      // Set the toolbar button sizes.
      if (bShow)
      {
         if (bIsList)
            ModifyStyle(0, TBSTYLE_LIST);
         SetSizes(m_rBigBtn.Size(), m_sizeImage);
      }
      else
      {
         if (bIsList)
            ModifyStyle(TBSTYLE_LIST, 0);
         SetSizes(m_rSmallBtn.Size(), m_sizeImage);
      }
   }
Note that the last statement in CMyToolBar::ShowLabels() uses the protected member variable of CToolBar, m_sizeImage. As this is an undocumented member variable of CToolBar, this variable may not exist in future implementations of CToolBar.


Add a declaration for the SetLabels() and ShowLabels() functions to the CMyToolBar class. Also add two protected data members of type CRect as shown below:


public:
     void SetLabels(BOOL bShow /*= TRUE*/);
     void ShowLabels(BOOL bShow /*= TRUE*/);
protected:
     CRect     m_rBigBtn;
     CRect     m_rSmallBtn;
Change the type of the CMainFrame::m_wndToolBar member to CMyToolBar. The file where CMyToolBar is defined must be #included in the MainFrm.h file.


After the toolbar has been created in CMainFrame::OnCreate() call SetLabels() like this:
m_wndToolBar.SetLabels(m_wndToolBar.GetStyle() & TBSTYLE_LIST);
This call ensures that the toolbar is initially displayed with the text labels if the toolbar has the TBSTYLE_LIST style or without, if the toolbar does not have this style.


You can now add a message handler to your main frame class that toggles the toolbar labels on/off by calling CMyToolBar::ShowLabels() and RecalcLayout().



m_wndToolBar.ShowLabels(TRUE);
RecalcLayout(TRUE);

GOOD LUCK
0
 
Roshan DavisCommented:
If yo are specifying TBIF_STATE, yuo should specify fsState also
like
tbInfo.fsState = TBSTATE_ENABLED | TBSTATE_PRESSED | TBSTATE_INDETERMINATE;

GOOD LUCK
0
 
joakimfAuthor Commented:
Hi,
I call
    TBBUTTONINFO tbInfo = { 0 };
    tbInfo.cbSize = sizeof ( TBBUTTONINFO );
    tbInfo.dwMask = TBIF_STATE;
    GetToolBarCtrl ().GetButtonInfo ( uButtonID, &tbInfo );

to get the current state of the button, so I don't have to set the fsState since it's already set.

/Joakim
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Roshan DavisCommented:
Tell me the base class of CToolBarEx

CToolBarCtrl or CToolBar

Roshmon
0
 
Roshan DavisCommented:
Okay I understool its from CToolBar,

Looking into the problem....

0
 
Roshan DavisCommented:
Can you show me the cTbInfo class
0
 
joakimfAuthor Commented:
Here's the class you wanted, it only contains one variable so far:
     class CTbInfo
     {
     public:
          CTbInfo ()
          {
               m_strButtonText = _T("");
          }
          CString m_strButtonText;
     };

Thanks.

/Joakim
0
 
Roshan DavisCommented:
check "HOWTO: Add Text to Toolbar Buttons" in MSDN, I think, that will work.
0
 
joakimfAuthor Commented:
Thanks, however one side effect with that code is that all buttons receive the same width, that's why I want to use the ToolBarCtrl, then the button is resized based on the text width.

Well, got some new ideas from this and that will propbably help. Thanks again.

/Joakim
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.