MFC工具栏
Posted freemindblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC工具栏相关的知识,希望对你有一定的参考价值。
CToolBar m_wndToolBar;
CToolBar m_wndTestBar;
CComboBoxEx m_oEdit;
CToolBar m_wndGoToBar;
CReBar m_oReBar;
CStatusBar m_wndStatusBar;
CChildView m_wndView;
CToolBar m_wndTestToolBar1;
CToolBar m_wndTestToolBar2;
CToolBar m_wndTestToolBar3;
CReBar m_wndGroup;
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
EnableDocking(CBRS_ALIGN_ANY);
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// 创建一个视图以占用框架的工作区
if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("未能创建视图窗口 ");
return -1;
}
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("未能创建工具栏 ");
return -1; // 未能创建
}
if (!m_wndStatusBar.Create(this))
{
TRACE0("未能创建状态栏 ");
return -1; // 未能创建
}
// https://www.xuebuyuan.com/647176.html
// 1. 创建工具栏对象
m_wndTestBar.CreateEx(this);
m_wndGoToBar.CreateEx(this);
//给工具栏设置图片:
//IDB_BMP_TOOLBAR为图片资源ID,
//22表示每个图片单元的宽度,CImageList通过这个宽度分割图片
//#ff00ff表示图片的背景掩码色
/*CImageList img;
img.Create(IDR_MAINFRAME, 48, 0, #ffffff);
m_wndTestBar.GetToolBarCtrl().SetImageList(&img);
img.Detach();*/
//设置工具栏三个按钮的图片和文本
m_wndTestBar.SetButtons(NULL, 3);
m_wndTestBar.SetButtonInfo(0, ID_1, TBSTYLE_BUTTON, -1);
m_wndTestBar.SetButtonText(0, L"1");
m_wndTestBar.SetButtonInfo(1, ID_2, TBSTYLE_BUTTON, 1);
m_wndTestBar.SetButtonText(1, L"2");
m_wndTestBar.SetButtonInfo(2, ID_3, TBSTYLE_BUTTON, 2);
m_wndTestBar.SetButtonText(2, L"3");
m_wndGoToBar.SetButtons(NULL, 2);
m_wndGoToBar.SetButtonInfo(0, ID_4, TBSTYLE_BUTTON, 3);
m_wndGoToBar.SetButtonText(0, L"4");
m_wndGoToBar.SetButtonInfo(1, ID_5, TBSTYLE_BUTTON, 4);
m_wndGoToBar.SetButtonText(1, L"5");
//设置工具栏的属性
m_wndTestBar.SetBarStyle(m_wndTestBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY);
m_wndGoToBar.SetBarStyle(m_wndTestBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY);
// 2. 创建编辑框对象
m_oEdit.Create(WS_CHILD | WS_VISIBLE, CRect(0, 0, 300, 20), this, 0);
// 3. 创建CReBar对象
m_oReBar.Create(this);
// 4. 把工具栏和编辑框绑定到CReBar上
m_oReBar.AddBar(&m_wndTestBar, NULL, NULL, RBBS_NOGRIPPER | RBBS_FIXEDBMP ); // | RBBS_USECHEVRON | RBBS_CHILDEDGE
m_oReBar.AddBar(&m_wndGoToBar, NULL, NULL, RBBS_NOGRIPPER | RBBS_FIXEDBMP); // | RBBS_USECHEVRON
m_oReBar.AddBar(&m_oEdit, NULL, NULL, RBBS_NOGRIPPER);
//m_oReBar.SetBarStyle(m_oReBar.GetBarStyle() | CBRS_SIZE_FIXED);
m_oReBar.SetBarStyle(m_oReBar.GetBarStyle() );
// 5. 设置工具栏在CReBar中的属性
CRect rectToolBar;
m_wndTestBar.GetToolBarCtrl().GetWindowRect(&rectToolBar);
CRect rectToolBarItem;
m_wndTestBar.GetItemRect(0, &rectToolBarItem);
REBARBANDINFO rbbi;
rbbi.cbSize = sizeof(rbbi);
// https://docs.microsoft.com/zh-cn/windows/desktop/api/commctrl/ns-commctrl-tagrebarbandinfoa
rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE | RBBIM_ID;
rbbi.cxMinChild = rectToolBarItem.Width();
rbbi.cyMinChild = rectToolBarItem.Height();
rbbi.cx = rbbi.cxIdeal = rectToolBar.Width() * 1;
rbbi.wID = 0;
m_oReBar.GetReBarCtrl().SetBandInfo(0, &rbbi);
rbbi.wID = 0;
m_oReBar.GetReBarCtrl().SetBandInfo(2, &rbbi);
//最后把工具栏显示出来
//m_oReBar.SetWindowPos(NULL, 0, 0, 260, 32, SWP_SHOWWINDOW);
m_wndGoToBar.GetToolBarCtrl().HideButton(ID_5, TRUE);
m_wndGoToBar.GetToolBarCtrl().HideButton(ID_5, FALSE);
return 0;
}
int CMainFrame::DoToolBar()
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// https://www.xuebuyuan.com/zh-tw/1919985.html
m_wndToolBar.SetHeight(100);
m_wndStatusBar.SetIndicators(indicators, sizeof(indicators) / sizeof(UINT));
if (!m_wndTestToolBar1.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_FIXED))
{
TRACE0("未能创建状态栏 ");
return -1;
}
if (!m_wndTestToolBar2.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_FIXED))
{
TRACE0("未能创建状态栏 ");
return -1;
}
if (!m_wndTestToolBar3.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_FIXED))
{
TRACE0("未能创建状态栏 ");
return -1;
}
if (!m_wndGroup.Create(this, RBS_FIXEDORDER) || !m_wndGroup.AddBar(&m_wndTestToolBar1, NULL, NULL, NULL, RBBS_FIXEDBMP | RBBS_NOGRIPPER)
|| !m_wndGroup.AddBar(&m_wndTestToolBar2, NULL, NULL, NULL, RBBS_FIXEDBMP)
|| !m_wndGroup.AddBar(&m_wndTestToolBar3, NULL, NULL, NULL, RBBS_FIXEDBMP))
{
TRACE0("未能创建状态栏 ");
return -1;
}
int iCount = m_wndGroup.GetReBarCtrl().GetBandCount();
m_wndTestToolBar1.SetHeight(100);
m_wndTestToolBar2.SetHeight(100);
m_wndTestToolBar3.SetHeight(100);
m_wndTestToolBar3.GetToolBarCtrl();
int iHeight = m_wndGroup.GetReBarCtrl().GetBarHeight();
m_wndTestToolBar1.SetButtons(uiTestIDs, 1);
m_wndTestToolBar1.SetButtonInfo(0, TESTBTNID, TBSTYLE_BUTTON, 0);
m_wndTestToolBar1.SetButtonText(0, L"dfscvbncxcvncvbndf");
// 取得按钮状态GetState
//m_wndTestToolBar1.GetToolBarCtrl().SetState(0, TBSTATE_HIDDEN);
// 隐藏按钮
m_wndTestToolBar1.GetToolBarCtrl().HideButton(TESTBTNID, TRUE);
m_wndTestToolBar1.GetToolBarCtrl().EnableButton(TESTBTNID, TRUE);
//DockControlBar(&m_wndTestToolBar1, AFX_IDW_DOCKBAR_TOP);
/*m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);*/
// 设带的高度 宽度
// https://bbs.csdn.net/topics/390397805
//改变一些属性
REBARBANDINFO rbbi;
rbbi.cbSize = sizeof(REBARBANDINFO); //这个是必须填
rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE;// | RBBIM_BACKGROUND;
//工具条的宽度
//rbbi.cxMinChild = rectToolBar.Width();
rbbi.cxMinChild = 100;
//高度
//rbbi.cyMinChild = rectToolBar.Height();
rbbi.cyMinChild = 32;
//下面这行代码是为工具条加入背景位图,请注意上rbbi.fMask中RBBIM_BACKGROUND标志
//rbbi.hbmBack = LoadBitmap(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_TOOLBARBKMAP));
rbbi.cx = rbbi.cxIdeal = 30;
m_wndGroup.GetReBarCtrl().SetBandInfo(0, &rbbi);
rbbi.cxMinChild = 100; //工具条的宽度
m_wndGroup.GetReBarCtrl().SetBandInfo(1, &rbbi);
rbbi.cxMinChild = 1000; //工具条的宽度
m_wndGroup.GetReBarCtrl().SetBandInfo(2, &rbbi);
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
}
CToolBar m_wndTestBar;
CComboBoxEx m_oEdit;
CToolBar m_wndGoToBar;
CReBar m_oReBar;
CStatusBar m_wndStatusBar;
CChildView m_wndView;
CToolBar m_wndTestToolBar1;
CToolBar m_wndTestToolBar2;
CToolBar m_wndTestToolBar3;
CReBar m_wndGroup;
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
EnableDocking(CBRS_ALIGN_ANY);
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// 创建一个视图以占用框架的工作区
if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("未能创建视图窗口 ");
return -1;
}
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("未能创建工具栏 ");
return -1; // 未能创建
}
if (!m_wndStatusBar.Create(this))
{
TRACE0("未能创建状态栏 ");
return -1; // 未能创建
}
// https://www.xuebuyuan.com/647176.html
// 1. 创建工具栏对象
m_wndTestBar.CreateEx(this);
m_wndGoToBar.CreateEx(this);
//给工具栏设置图片:
//IDB_BMP_TOOLBAR为图片资源ID,
//22表示每个图片单元的宽度,CImageList通过这个宽度分割图片
//#ff00ff表示图片的背景掩码色
/*CImageList img;
img.Create(IDR_MAINFRAME, 48, 0, #ffffff);
m_wndTestBar.GetToolBarCtrl().SetImageList(&img);
img.Detach();*/
//设置工具栏三个按钮的图片和文本
m_wndTestBar.SetButtons(NULL, 3);
m_wndTestBar.SetButtonInfo(0, ID_1, TBSTYLE_BUTTON, -1);
m_wndTestBar.SetButtonText(0, L"1");
m_wndTestBar.SetButtonInfo(1, ID_2, TBSTYLE_BUTTON, 1);
m_wndTestBar.SetButtonText(1, L"2");
m_wndTestBar.SetButtonInfo(2, ID_3, TBSTYLE_BUTTON, 2);
m_wndTestBar.SetButtonText(2, L"3");
m_wndGoToBar.SetButtons(NULL, 2);
m_wndGoToBar.SetButtonInfo(0, ID_4, TBSTYLE_BUTTON, 3);
m_wndGoToBar.SetButtonText(0, L"4");
m_wndGoToBar.SetButtonInfo(1, ID_5, TBSTYLE_BUTTON, 4);
m_wndGoToBar.SetButtonText(1, L"5");
//设置工具栏的属性
m_wndTestBar.SetBarStyle(m_wndTestBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY);
m_wndGoToBar.SetBarStyle(m_wndTestBar.GetBarStyle() |
CBRS_TOOLTIPS | CBRS_FLYBY);
// 2. 创建编辑框对象
m_oEdit.Create(WS_CHILD | WS_VISIBLE, CRect(0, 0, 300, 20), this, 0);
// 3. 创建CReBar对象
m_oReBar.Create(this);
// 4. 把工具栏和编辑框绑定到CReBar上
m_oReBar.AddBar(&m_wndTestBar, NULL, NULL, RBBS_NOGRIPPER | RBBS_FIXEDBMP ); // | RBBS_USECHEVRON | RBBS_CHILDEDGE
m_oReBar.AddBar(&m_wndGoToBar, NULL, NULL, RBBS_NOGRIPPER | RBBS_FIXEDBMP); // | RBBS_USECHEVRON
m_oReBar.AddBar(&m_oEdit, NULL, NULL, RBBS_NOGRIPPER);
//m_oReBar.SetBarStyle(m_oReBar.GetBarStyle() | CBRS_SIZE_FIXED);
m_oReBar.SetBarStyle(m_oReBar.GetBarStyle() );
// 5. 设置工具栏在CReBar中的属性
CRect rectToolBar;
m_wndTestBar.GetToolBarCtrl().GetWindowRect(&rectToolBar);
CRect rectToolBarItem;
m_wndTestBar.GetItemRect(0, &rectToolBarItem);
REBARBANDINFO rbbi;
rbbi.cbSize = sizeof(rbbi);
// https://docs.microsoft.com/zh-cn/windows/desktop/api/commctrl/ns-commctrl-tagrebarbandinfoa
rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE | RBBIM_ID;
rbbi.cxMinChild = rectToolBarItem.Width();
rbbi.cyMinChild = rectToolBarItem.Height();
rbbi.cx = rbbi.cxIdeal = rectToolBar.Width() * 1;
rbbi.wID = 0;
m_oReBar.GetReBarCtrl().SetBandInfo(0, &rbbi);
rbbi.wID = 0;
m_oReBar.GetReBarCtrl().SetBandInfo(2, &rbbi);
//最后把工具栏显示出来
//m_oReBar.SetWindowPos(NULL, 0, 0, 260, 32, SWP_SHOWWINDOW);
m_wndGoToBar.GetToolBarCtrl().HideButton(ID_5, TRUE);
m_wndGoToBar.GetToolBarCtrl().HideButton(ID_5, FALSE);
return 0;
}
int CMainFrame::DoToolBar()
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// https://www.xuebuyuan.com/zh-tw/1919985.html
m_wndToolBar.SetHeight(100);
m_wndStatusBar.SetIndicators(indicators, sizeof(indicators) / sizeof(UINT));
if (!m_wndTestToolBar1.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_FIXED))
{
TRACE0("未能创建状态栏 ");
return -1;
}
if (!m_wndTestToolBar2.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_FIXED))
{
TRACE0("未能创建状态栏 ");
return -1;
}
if (!m_wndTestToolBar3.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_SIZE_FIXED))
{
TRACE0("未能创建状态栏 ");
return -1;
}
if (!m_wndGroup.Create(this, RBS_FIXEDORDER) || !m_wndGroup.AddBar(&m_wndTestToolBar1, NULL, NULL, NULL, RBBS_FIXEDBMP | RBBS_NOGRIPPER)
|| !m_wndGroup.AddBar(&m_wndTestToolBar2, NULL, NULL, NULL, RBBS_FIXEDBMP)
|| !m_wndGroup.AddBar(&m_wndTestToolBar3, NULL, NULL, NULL, RBBS_FIXEDBMP))
{
TRACE0("未能创建状态栏 ");
return -1;
}
int iCount = m_wndGroup.GetReBarCtrl().GetBandCount();
m_wndTestToolBar1.SetHeight(100);
m_wndTestToolBar2.SetHeight(100);
m_wndTestToolBar3.SetHeight(100);
m_wndTestToolBar3.GetToolBarCtrl();
int iHeight = m_wndGroup.GetReBarCtrl().GetBarHeight();
m_wndTestToolBar1.SetButtons(uiTestIDs, 1);
m_wndTestToolBar1.SetButtonInfo(0, TESTBTNID, TBSTYLE_BUTTON, 0);
m_wndTestToolBar1.SetButtonText(0, L"dfscvbncxcvncvbndf");
// 取得按钮状态GetState
//m_wndTestToolBar1.GetToolBarCtrl().SetState(0, TBSTATE_HIDDEN);
// 隐藏按钮
m_wndTestToolBar1.GetToolBarCtrl().HideButton(TESTBTNID, TRUE);
m_wndTestToolBar1.GetToolBarCtrl().EnableButton(TESTBTNID, TRUE);
//DockControlBar(&m_wndTestToolBar1, AFX_IDW_DOCKBAR_TOP);
/*m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);*/
// 设带的高度 宽度
// https://bbs.csdn.net/topics/390397805
//改变一些属性
REBARBANDINFO rbbi;
rbbi.cbSize = sizeof(REBARBANDINFO); //这个是必须填
rbbi.fMask = RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE;// | RBBIM_BACKGROUND;
//工具条的宽度
//rbbi.cxMinChild = rectToolBar.Width();
rbbi.cxMinChild = 100;
//高度
//rbbi.cyMinChild = rectToolBar.Height();
rbbi.cyMinChild = 32;
//下面这行代码是为工具条加入背景位图,请注意上rbbi.fMask中RBBIM_BACKGROUND标志
//rbbi.hbmBack = LoadBitmap(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_TOOLBARBKMAP));
rbbi.cx = rbbi.cxIdeal = 30;
m_wndGroup.GetReBarCtrl().SetBandInfo(0, &rbbi);
rbbi.cxMinChild = 100; //工具条的宽度
m_wndGroup.GetReBarCtrl().SetBandInfo(1, &rbbi);
rbbi.cxMinChild = 1000; //工具条的宽度
m_wndGroup.GetReBarCtrl().SetBandInfo(2, &rbbi);
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
}
以上是关于MFC工具栏的主要内容,如果未能解决你的问题,请参考以下文章