工具栏
在传统的菜单式界面中,工具栏作为菜单的有益补充,被广泛使用。我们通过将一些常用命令放置到工具栏上,可以让用户直观而快速地访问到常用功能,提高了效率。在Ribbon界面中,工具栏得到了进一步的加强。除了具备原来的工具栏功能外,因为使用命令按钮实现,还使得工具栏具备了下拉菜单等扩展功能。
图7 工具栏
如下的代码演示了如何创建Ribbon界面中的工具栏控件:
CMFCRibbonPanel* pPanel1 = pCategory->AddPanel(_T("From Toolbar"));
// 最简单的,通过AddToolBar()函数,指定一个工具栏资源而创建工具栏
pPanel1->AddToolBar(IDR_TOOLBAR);
// 手动创建工具栏
CMFCRibbonPanel* pPanel2 = pCategory->AddPanel(_T("Manual"));
// 创建一个按钮组
CMFCRibbonButtonsGroup* pButtonsGroup1 = new CMFCRibbonButtonsGroup;
// 将新的按钮添加到按钮组中
pButtonsGroup1->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_1, _T(""), 0));
pButtonsGroup1->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_2, _T(""), 1));
// 创建一个编辑框控件
CMFCRibbonEdit* pEdit = new CMFCRibbonEdit(ID_RIBBON_GBTN_3, 65);
// 设置默认文本
pEdit->SetEditText(_T("Edit"));
pButtonsGroup1->AddButton(pEdit);
pButtonsGroup1->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_4, _T(""), 2));
pButtonsGroup1->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_5, _T(""), 3));
// 将按钮组添加到面板中
pPanel2->Add(pButtonsGroup1);
// 添加新的按钮组和按钮
CMFCRibbonButtonsGroup* pButtonsGroup2 = new CMFCRibbonButtonsGroup;
pButtonsGroup2->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_6, _T(""), 4));
pButtonsGroup2->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_7, _T(""), 5));
pButtonsGroup2->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_8, _T(""), 6));
pButtonsGroup2->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_9, _T(""), 7));
pPanel2->Add(pButtonsGroup2);
CMFCRibbonButtonsGroup* pButtonsGroup3 = new CMFCRibbonButtonsGroup;
CMFCRibbonButton* pBtn10 = new CMFCRibbonButton(ID_RIBBON_GBTN_10, _T(""), 8);
pBtn10->SetMenu(IDR_RIBBON_MENU_1);
pButtonsGroup3->AddButton(pBtn10);
CMFCRibbonButton* pBtn11 = new CMFCRibbonButton(ID_RIBBON_GBTN_11, _T(""), 9);
// 为按钮指定一个子菜单
pBtn11->SetMenu(IDR_RIBBON_MENU_1, TRUE);
pButtonsGroup3->AddButton(pBtn11);
pPanel2->Add(pButtonsGroup3);
// 最简单的,通过AddToolBar()函数,指定一个工具栏资源而创建工具栏
pPanel1->AddToolBar(IDR_TOOLBAR);
// 手动创建工具栏
CMFCRibbonPanel* pPanel2 = pCategory->AddPanel(_T("Manual"));
// 创建一个按钮组
CMFCRibbonButtonsGroup* pButtonsGroup1 = new CMFCRibbonButtonsGroup;
// 将新的按钮添加到按钮组中
pButtonsGroup1->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_1, _T(""), 0));
pButtonsGroup1->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_2, _T(""), 1));
// 创建一个编辑框控件
CMFCRibbonEdit* pEdit = new CMFCRibbonEdit(ID_RIBBON_GBTN_3, 65);
// 设置默认文本
pEdit->SetEditText(_T("Edit"));
pButtonsGroup1->AddButton(pEdit);
pButtonsGroup1->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_4, _T(""), 2));
pButtonsGroup1->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_5, _T(""), 3));
// 将按钮组添加到面板中
pPanel2->Add(pButtonsGroup1);
// 添加新的按钮组和按钮
CMFCRibbonButtonsGroup* pButtonsGroup2 = new CMFCRibbonButtonsGroup;
pButtonsGroup2->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_6, _T(""), 4));
pButtonsGroup2->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_7, _T(""), 5));
pButtonsGroup2->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_8, _T(""), 6));
pButtonsGroup2->AddButton(new CMFCRibbonButton(ID_RIBBON_GBTN_9, _T(""), 7));
pPanel2->Add(pButtonsGroup2);
CMFCRibbonButtonsGroup* pButtonsGroup3 = new CMFCRibbonButtonsGroup;
CMFCRibbonButton* pBtn10 = new CMFCRibbonButton(ID_RIBBON_GBTN_10, _T(""), 8);
pBtn10->SetMenu(IDR_RIBBON_MENU_1);
pButtonsGroup3->AddButton(pBtn10);
CMFCRibbonButton* pBtn11 = new CMFCRibbonButton(ID_RIBBON_GBTN_11, _T(""), 9);
// 为按钮指定一个子菜单
pBtn11->SetMenu(IDR_RIBBON_MENU_1, TRUE);
pButtonsGroup3->AddButton(pBtn11);
pPanel2->Add(pButtonsGroup3);
编辑框
在传统的软件界面中,我们都是通过点击菜单项,或者工具栏上的按钮来简单执行某个命令。在新的Ribbon界面中,我们不仅可以点击按钮,还可以通过编辑框输入数据或者通过ComboBox快速地选择数据等等,完成更加复杂的交互。
图8 编辑框
如下的代码演示了Ribbon编辑框的创建过程: