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 );
- CToolBar按钮自动大小
- CToolBar
- [VC++]CToolBar按钮不能响应ON_UPDATE_COMMAND_UI?
- 自动适应大小,适应各种浏览器的按钮
- MFC工具条CToolBar添加非按钮控件CComboBox
- MFC工具条CToolBar添加非按钮控件CComboBox
- VC MFC工具栏(CToolBar)控件以及如何设置按钮图片集
- MFC工具条CToolBar添加非按钮控件CComboBox
- CToolbar的按钮启用与禁用(enable/disable)
- 为CToolBar工具栏窗口按钮添加ICON图标需要注意的一点
- 按钮控制div大小
- button按钮大小
- CToolBar使用
- CToolBar使用
- MFC--CToolBar
- 工具栏CToolBar
- 设置按钮的大小 ,按钮变小手
- 【旧资料整理】MFC--去除浮动工具栏(CToolBar)或浮动边栏(CDialogBar)关闭按钮真正有效的方法
- 模板特化(template specialization)
- 一个Java反射机制例子
- Windows 程序移植到Linux平台的一波N折
- 我学设计模式
- JAVA静态块和非静态块的区别
- CToolBar按钮自动大小
- ResourceBundle的路径问题
- 编译指示指令
- UML类图关系
- Windows 2003 下 PHP5.2.5 + Apache2.2.8 + Oracle9i 的配置
- 资深潜水员5年减肥经验谈
- struts2默认配置文件 struts-default.xml 研究
- 在游戏中如何使用LUA脚本语言
- 是去工作呢,还是去培训呢,还是专于毕业设计呢?