CToolBar按钮自动大小

来源:互联网 发布:java 开源bug管理系统 编辑:程序博客网 时间:2024/05/17 07:33

 原始作者: 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 );

原创粉丝点击