Toolbar button sizes
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Toolbar button sizes

  1. #1
    Join Date
    Dec 2008
    Posts
    29

    Toolbar button sizes

    I've created a toolbar that has text to the right of the icon using the TBSTYLE_LIST style and added text to a button using SetButtonText(). Initially, the button size is not changed to reflect the fact that there is now text on the button. Various pages online solve this by using SetSizes on the toolbar to set the button size correctly.

    My problem is that this sets all the button sizes to the same width. Only the first two buttons on my toolbar have text, the others I want to only have icons.

    Is there a way to set the size (width) for individual buttons on the toolbar?

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,259

    Re: Toolbar button sizes

    Try CToolBarCtrl::SetButtonInfo
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2008
    Posts
    29

    Re: Toolbar button sizes

    As far as I'm aware, and according to the msdn docs, SetButtonInfo only sets the width of a separator. For all other types it sets the index of the image to use.

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,259

    Re: Toolbar button sizes

    Quote Originally Posted by rioch View Post
    As far as I'm aware, and according to the msdn docs, SetButtonInfo only sets the width of a separator. For all other types it sets the index of the image to use.
    I pointed out that you should try CToolBarCtrl::SetButtonInfo method, not a CToolBar::SetButtonInfo
    Victor Nijegorodov

  5. #5
    Join Date
    Dec 2008
    Posts
    29

    Re: Toolbar button sizes

    My mistake. So to use this I'll have to derive from CToolBar and add some functions to set the button sizes. Is there a good place to do this such that all button size according to text width? Perhaps CToolBar::OnSize?

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,259

    Re: Toolbar button sizes

    Quote Originally Posted by rioch View Post
    My mistake. So to use this I'll have to derive from CToolBar and add some functions to set the button sizes. Is there a good place to do this such that all button size according to text width? Perhaps CToolBar::OnSize?
    You don't need to derive.
    But if you want to - what is the problem?
    Victor Nijegorodov

  7. #7
    Join Date
    Dec 2008
    Posts
    29

    Re: Toolbar button sizes

    I've created the toolbar using the style TBSTYLE_LIST. I've tried the code below, but neither the text appears nor the button size changes. If I remove the text from the buttonInfo and set it instead with m_wndToolBar1.SetButtonText() the text appears (to the right) but all the buttons take the same width again - in fact, in this case, it chops off the end of the toolbar.

    TBBUTTONINFO buttonInfo;
    buttonInfo.cbSize = sizeof(buttonInfo);
    buttonInfo.dwMask = TBIF_SIZE | TBIF_STYLE;
    buttonInfo.fsStyle = TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE;
    buttonInfo.cx = 150;
    buttonInfo.pszText = "Something...";
    m_wndToolBar1.GetToolBarCtrl().SetButtonInfo(1, &buttonInfo);

  8. #8
    Join Date
    Dec 2008
    Posts
    29

    Re: Toolbar button sizes

    It seems I needed the flat TBIF_BYINDEX. However, this only works correctly if I don't set the text. If I set the text (either method) it causes all buttons to change size. It even pushes other controls on the toolbar past the edge (the toolbar itself doesn't resize along with the button).

  9. #9
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,259

    Re: Toolbar button sizes

    Perhaps, you should set TBSTYLE_AUTOSIZE style to all toolbat buttons?
    Victor Nijegorodov

  10. #10
    Join Date
    Dec 2008
    Posts
    29

    Re: Toolbar button sizes

    In the end I did the following:

    m_wndToolBar1.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_MIXEDBUTTONS);
    CString strButtonText = "Something...";
    TBBUTTONINFO tbbi;
    tbbi.cbSize = sizeof(tbbi);
    tbbi.dwMask = TBIF_BYINDEX | TBIF_STYLE;
    m_wndToolBar1.GetToolBarCtrl().GetButtonInfo(1, &tbbi);
    tbbi.dwMask = TBIF_BYINDEX | TBIF_STYLE | TBIF_TEXT;
    tbbi.pszText = (LPTSTR)((LPCTSTR)strButtonText);
    tbbi.cchText = strButtonText.GetLength();
    tbbi.fsStyle |= TBSTYLE_AUTOSIZE | BTNS_SHOWTEXT;
    m_wndToolBar1.GetToolBarCtrl().SetButtonInfo(1, &tbbi);

    The TBSTYLE_EX_MIXEDBUTTONS style allows optional text on buttons. The BTNS_SHOWTEXT shows it for that button. The only problem now is that a combo box which has been added after this button is being clipped by the end on the toolbar.

  11. #11
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,259

    Re: Toolbar button sizes

    Not sure I understand your problem with a combobox, but perhaps, RecalcLayout could help?
    Victor Nijegorodov

  12. #12
    Join Date
    Dec 2008
    Posts
    29

    Re: Toolbar button sizes

    RecalcLayout didn't help. Let me explain some more.

    I have a toolbar which has three buttons and a combo box. All the buttons have an icon, the first button also has text to the right of the icon. The combo box is added by calling SetButtonInfo on the toolbar (replacing a separator).

    If I do not have text on the first button, everything fits nicely in the toolbar. If I add text, everything moves along to accomodate the text in the button, but the toolbar doesn't grow in width. The result is that the combobox is cut off at the end (see attached screenshot).
    Attached Images Attached Images  

  13. #13
    Join Date
    Dec 2008
    Posts
    29

    Re: Toolbar button sizes

    Any help on this? I'm stumped.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center