Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Toolbar with text on right.

Posted on 2002-07-04
9
Medium Priority
?
1,004 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:joakimf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7131284
If yo are specifying TBIF_STATE, yuo should specify fsState also
like
tbInfo.fsState = TBSTATE_ENABLED | TBSTATE_PRESSED | TBSTATE_INDETERMINATE;

GOOD LUCK
0
 
LVL 1

Author Comment

by:joakimf
ID: 7131335
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7131386
Tell me the base class of CToolBarEx

CToolBarCtrl or CToolBar

Roshmon
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7131390
Okay I understool its from CToolBar,

Looking into the problem....

0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7131427
Can you show me the cTbInfo class
0
 
LVL 1

Author Comment

by:joakimf
ID: 7131440
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
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7131448
check "HOWTO: Add Text to Toolbar Buttons" in MSDN, I think, that will work.
0
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 800 total points
ID: 7131467
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
 
LVL 1

Author Comment

by:joakimf
ID: 7131508
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question