最近工作一直在做界面,用到了toolbar,有点小心得写出来。代码如下:
if (!m_wndToolBar.CreateEx(AfxGetMainWnd(), TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))// ||
//!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
}
m_wndToolBar.SetButtons(NULL,7);
bitmap.LoadBitmap(IDB_PHONEBAR);
int i=imageList.Create(16, 15, ILC_COLOR24, 13, 1);
imageList.Add(&bitmap, (CBitmap*)NULL);
m_wndToolBar.SendMessage(TB_SETIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
m_wndToolBar.SendMessage(TB_SETDISABLEDIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
imageList.Detach();
bitmap.Detach();
m_wndToolBar.SetButtonText(0, "连接");
m_wndToolBar.SetButtonText(2, "播放");
m_wndToolBar.SetButtonText(3, "保存");
m_wndToolBar.SetButtonText(5, "全选");
m_wndToolBar.SetButtonText(6, "删除");
m_wndToolBar.SetSizes(CSize(50,40),CSize(20,20));
m_wndToolBar.GetToolBarCtrl().SetButtonWidth(40,50);
m_wndToolBar.SetButtonInfo(0, ID_CONNECT, TBSTYLE_BUTTON, 0);
m_wndToolBar.SetButtonInfo(1, 0, TBBS_SEPARATOR, 0);
m_wndToolBar.SetButtonInfo(2, ID_VIDEO_PLAY, TBSTYLE_BUTTON, 1);
m_wndToolBar.SetButtonInfo(3, ID_VIDEO_SAVE, TBSTYLE_BUTTON, 2);
m_wndToolBar.SetButtonInfo(4, 0, TBBS_SEPARATOR, 0);
m_wndToolBar.SetButtonInfo(5, ID_VIDEO_SELECTALL, TBSTYLE_BUTTON, 3);
m_wndToolBar.SetButtonInfo(6, ID_VIDEO_DELETE, TBSTYLE_BUTTON, 4);
其中
m_wndToolBar.SetButtons(NULL,7);设置toolbar的按钮个数(包括
SEPARATOR).
m_wndToolBar.SendMessage(TB_SETIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
m_wndToolBar.SendMessage(TB_SETDISABLEDIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
分别设置正常的和无效时的image list 还有一个TB_SETHOTIMAGELIST设置当鼠标在按钮上时的image list .
SetButtonText设置按钮的文字.
SetSizes设置按钮和图片的大小,这个函数一定要在SetButtonText后调用要不然会看不到文字
GetToolBarCtrl().SetButtonWidth(40,50);设置按钮的宽度一定要调用,否则在toolbar buttib无效是,button的宽度会调成最大button文字长度的宽度,而不是你预想的宽度
SetButtonInfo 设置button的图片和nID(用来关联消息可在on_commond中使用)
本文地址:http://com.8s8s.com/it/it34929.htm