Link to home
Start Free TrialLog in
Avatar of joakimf
joakimf

asked on

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
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

If yo are specifying TBIF_STATE, yuo should specify fsState also
like
tbInfo.fsState = TBSTATE_ENABLED | TBSTATE_PRESSED | TBSTATE_INDETERMINATE;

GOOD LUCK
Avatar of joakimf
joakimf

ASKER

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
Tell me the base class of CToolBarEx

CToolBarCtrl or CToolBar

Roshmon
Okay I understool its from CToolBar,

Looking into the problem....

Can you show me the cTbInfo class
Avatar of joakimf

ASKER

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
check "HOWTO: Add Text to Toolbar Buttons" in MSDN, I think, that will work.
ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joakimf

ASKER

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