CToolBar按钮自动大小

 原始作者: Hwycheng Leo(FlashBT@Hotmail.com)
作者网站: http://www.hwysoft.com/chs/
作者Blog: http://hwycheng.blogchina.com
作者简介: 开发了BitTorrent下载软件 - FlashBT(变态快车), 目前从事企业级的P2P/IM平台的设计和开发工作
参考:MSDN
转载说明: 你可以自由转载本文章,但是请保留此以上的声明和文字

默认情况下CToolBar的按钮就的大小可以使用SetSizes成员函数设置按钮的宽度和高度。但是效果是对所有的按钮就设置一样的宽度和高度。按钮中只显示图片时, 我们不会感觉有什么问题, 但是按钮上面显示文字,按钮数量较多,并且文字的个数不平均的时候就会出现所有按钮的宽度一律以最宽的那个按钮的宽度设置,造成工具栏松散视觉效果不佳,更可能的时行显示不下所有的按钮,给用户使用时带来许多不便。最近有人问起,如何设置来获取每个按钮的宽度独立于其它按钮,就是分别设置各个按钮就的宽度。其实问题不难,主要在于经验以及对于MSDN文档的了解。我们先来看一个例子:

------------------------------------------------------------------------------

CToolBar m_wndToolBar;

UINT arCmdIDs[] =
    {
        ID_FILE_NEW,
        ID_FILE_OPEN,
        ID_SEPARATOR,
        ID_TOOL_OPTIONS,
        ID_SEPARATOR,
        ID_APP_ABOUT,
        ID_APP_EXIT
    };

TCHAR *szCmdTexts[] =
        {
        _T( "New" ),
        _T( "Open" ),
        _T( "" ),
        _T( "Options" ),
        _T( "" ),
        _T( "About" ),
        _T( "Exit" )
        };
       
------------------------------------------------------------------------------

void SetButtons(CToolBar &wndToolBar, const UINT* lpIDArray, int nIDCount, BOOL bAutoSize/* = FALSE*/ )
{
        ASSERT( NULL != lpIDArray );
        ASSERT( nIDCount >= 1 );        // must be at least one of them

        TBBUTTON button;
        memset(&button, 0, sizeof(TBBUTTON));
        button.iString = -1;
       
        // add new buttons to the common control
        int iImage = 0;
        for ( int i = 0; i < nIDCount; i++ )
        {
                button.fsState = TBSTATE_ENABLED;
                if ( ( button.idCommand = *lpIDArray++) == ID_SEPARATOR )
                {
                        // separator
                        button.fsStyle = TBSTYLE_SEP;
                        button.iBitmap = 8;
                }
                else
                {
                        // a command button with image
                        button.fsStyle = bAutoSize ? TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE : TBSTYLE_BUTTON;
                        button.iBitmap = iImage++;
                }
               
                wndToolBar.AddButtons( 1, &button );
        }
}
      
------------------------------------------------------------------------------

int InitializeToolBar( BOOL bAutoSize/* = FALSE*/ )
{
        if ( !m_wndToolBar.CreateEx( this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
                                 | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY CBRS_SIZE_FIXED ) )
        {
                TRACE0( "Failed to create toolbar/n" );
                return -1;      // fail to create
        }
       
        m_wndToolBar.SetButtons( m_wndToolBar, arCmdIDs, sizeof( arCmdIDs ) / sizeof( UINT ), bAutoSize );
               
        for ( register int i = 0; i < m_wndToolBar.GetButtonCount(); i++ )
        {
                if ( arCmdIDs[ i ] == ID_SEPARATOR )
                {
                        continue;
                }
       
                m_wndToolBar.SetButtonText( m_wndToolBar.CommandToIndex( arCmdIDs[ i ] ), szCmdTexts[ i ] );
        }
       
        return 0;
}

------------------------------------------------------------------------------

调用 InitializeToolBar(), bAutoSize 参数设置为FALSE, 则工具栏上面所有的按钮的大小一致。bAutoSize 参数设置为TRUE, 则工具栏上面的按钮的大小不一致,各自维护一个自己的宽度,和自己显示的文本的长度相适应。

------------------------------------------------------------------------------

再来看一下MSDN中关于这个的描述:

TBSTYLE_AUTOSIZE
Equivalent to BTNS_AUTOSIZE. Use TBSTYLE_AUTOSIZE for version 4.72 and earlier.

BTNS_AUTOSIZE
Version 5.80. Specifies that the toolbar control should not assign the standard width to the button. Instead, the button's width will be calculated based on the width of the text plus the image of the button. Use the equivalent style flag, TBSTYLE_AUTOSIZE, for version 4.72 and earlier.

就是这么简单,如果你正好想实现此功能,将本文中的代码,拷贝,粘贴,稍微修改一下,使用即可。

 

 

注:其实要设置单个按钮为自动大小,只需要一句代码就够了:

m_wndToolBar.SetButtonStyle( 4, TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值