CDialog上使用CToolBar+CReBar

最经在做一些用户界面的东西,对话框上有很多按钮和组合框,全部加起来差不多有20多个吧,界面非常凌乱,最后决定用CToolBar + CReBar来重新设计界面,为什么选用这个呢?一是因为看到IE用的也是这个,二是用CReBar+透明的CToolBar可以实现漂亮的换肤效果。

1、在对话框类中添加成员变量:

CStatic m_static;
CButton m_btn;
CComboBox m_combo;
CToolBar m_toolBar;
CReBar m_reBar;

在OnInitDialog()添加如下代码:

1、用于创建工具栏和ReBar

if (!m_reBar.Create(this))
	return FALSE;

if (!m_toolBar.CreateEx(this))
	return FALSE;


//TBSTYLE_TRANSPARENT是使CToolBar透明,可以显示CReBar的背景。
//TBSTYLE_LIST用于设置按钮文字时,文字在按钮的右边,默认情况下是文字在按钮的下部
m_toolBar.ModifyStyle(0, TBSTYLE_TRANSPARENT | TBSTYLE_LIST);

m_toolBar.GetToolBarCtrl().SetExtendedStyle(TBSTYLE_EX_DRAWDDARROWS);//设置下拉箭头样式

m_toolBar.SetButtons(NULL, 5);//设置ToolBar 按钮个数
	m_reBar.AddBar(&m_toolBar);

2、添加按钮:

CRect rect;
int nIndex = -1;

//添加文本
m_toolBar.SetButtonInfo(++nIndex, 0, TBSTYLE_BUTTON | BTNS_AUTOSIZE | TBSTYLE_AUTOSIZE | TBBS_DISABLED, -1);
//此处是为了增加按钮的宽度,可以更加自己的需要适当的调整,由于是不可见字符,因此是透明的
m_toolBar.SetButtonText(nIndex, _T(" "));
m_toolBar.GetItemRect(nIndex, &rect);
rect.top += 3;//此处是为了让文本垂直居中
m_static.Create(_T("工具栏"), WS_CHILD | WS_VISIBLE | SS_CENTER | SS_SIMPLE, rect, &m_toolBar);
m_static.SetFont(m_toolBar.GetFont());

//添加组合框
m_toolBar.SetButtonInfo(++nIndex, 0, TBSTYLE_SEP, 80);
m_toolBar.GetItemRect(nIndex, &rect);
rect.bottom += 100;
m_combo.Create(WS_CHILD | WS_VISIBLE | CBS_DROPDOWN, rect, &m_toolBar, 0);m_combo.SetFont(m_toolBar.GetFont());

//添加
CheckBoxm_toolBar.SetButtonInfo(++nIndex, 0,TBSTYLE_BUTTON | TBBS_DISABLED | TBSTYLE_AUTOSIZE, -1);
m_toolBar.SetButtonText(nIndex, _T(" "));
m_toolBar.GetItemRect(nIndex, &rect);
m_btn.Create(_T("你好"), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, rect, &m_toolBar, 0);
m_btn.SetFont(m_toolBar.GetFont());
//添加下拉按钮,这个-2表示不显示按钮位图,但是显示了文字,这个我通过跟踪IE8的CToolBar按钮得到的,微软完全没有说明。
//至于这个-2是怎么来的,我会在另一个文章中说明。
m_toolBar.SetButtonInfo(++nIndex, 0, TBSTYLE_AUTOSIZE | BTNS_WHOLEDROPDOWN, -2);
m_toolBar.SetButtonText(nIndex, _T("下拉列表"));

//显示ReBar和工具栏
CRect rcWnd;
GetClientRect(&rcWnd);
m_reBar.SetWindowPos(NULL, 0, 0, rcWnd.Width(), 24, SWP_SHOWWINDOW);//显示CReBar

显示效果如下:

"工具栏"是CStatic,如果要让他透明,直接从CStatic 派生一个CStaticExt类

添加宏ON_WM_CTLCOLOR_REFLECT()和函数

HBRUSH CTransparentStatic::CtlColor(CDC *pDC, UINT nCtlColor)
{
	pDC->SetBkMode(TRANSPARENT);
	return (HBRUSH)::GetStockObject(NULL_BRUSH);//直接返回空画刷,这样就透明了
}

4、让对话框能响应ON_UPDATE_COMMAND_UI消息

添加函数:

void OnKickIdle()
{
	if (m_toolBar.GetSafeHwnd() && m_toolBar.IsWindowVisibile())
		m_topBar.OnUpdateCmdUI((CFrameWnd *)this, FALSE);//响应工具栏按钮更新消息
}

添加宏ON_WM_INITMENUPOPUP()和函数:

void OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
	CDialogEx::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	if (!bSysMenu && pPopupMenu)
	{
		CCmdUI cmdUI;
		int nCount;
		UINT nID;

		cmdUI.m_pOther = NULL;
		cmdUI.m_pMenu = pPopupMenu;
		cmdUI.m_pSubMenu = NULL;

		nCount = pPopupMenu->GetMenuItemCount();
		cmdUI.m_nIndexMax = nCount;

		for (int i = 0; i < nCount; ++i)
		{
			nID = pPopupMenu->GetMenuItemID(i);
			if (nID == -1 || nID == 0)
				continue;

			cmdUI.m_nID = nID;
			cmdUI.m_nIndex = i;
			cmdUI.DoUpdate(this, FALSE);//菜单按钮更新消息
		}
	}
}

接下来我们就可以使用ON_UPDATE_COMMAND_UI宏来添加消息响应了,这里不再说明。

补充:通过m_reBar.GetReBarCtrl().ShowBand(1,bShow);函数来显示或隐藏Band,界面会刷新不正常。

解决办法:先重绘工具栏,再重绘子窗口。

m_reBar.RedrawWindow();
m_static.RedrawWindow();
m_combo.RedrawWindow();

   

原文地址:https://www.cnblogs.com/dongc/p/5225132.html