如何设置CToolBar使每个按钮的宽度和显示的文字宽度一致而不是统一的宽度?

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

关键词: CToolBar 工具栏 按钮 Button 宽度 文本

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航