• 元素(CMFCRibbonBaseElement)
图3 元素
“元素”就是我们通常意义上的控件、这些控件根据各自的功能,被分组放置在各个“面板”上,负责完成具体的交互任务。Visual Studio 2010提供的Ribbon界面“元素”主要包括命令按钮(CMFCRibbonButton)。工具廊(CMFCRibbonGallery)、颜色按钮(CMFCRibbonColorButton)、编辑框(CMFCRibbonEdit)、进度条(CMFCRibbonProgressBar)等等。这些类都派生自CMFCRibbonBaseElement。
图4 丰富的Ribbon控件
下面我们就来详细介绍各种Ribbon控件的使用。
命令按钮
命令按钮可以说是我们最常用的Ribbon控件了,我们通常都是通过命令按钮来发送某个命令,执行某个动作。它代替了过去的菜单命令,成为使用最频繁的Ribbon控件。在Ribbon界面中,主要有三种形式的命令按钮:大图标按钮,小图标按钮以及表示选择的复选按钮(CheckBox)。
图5 命令按钮
按照上回我们介绍的Ribbon界面开发流程,我们需要先准备菜单资源,图标资源,实现消息响应函数等,这里我们就不再赘述这一过程,而把重点放在如何创建Ribbon界面。下面的代码分别演示了这三种按钮的创建过程:
// 创建一个新的面板,用于放置大图标按钮
CMFCRibbonPanel* pPanel1 = pCategory->AddPanel(_T("Large Buttons"));
// 创建按钮
CMFCRibbonButton* pBtn1 = new CMFCRibbonButton(ID_RIBBON_BTN_1, _T("Button"), 0, 0);
// 指定使用大图标
pBtn1->SetAlwaysLargeImage();
// 将按钮添加到面板中
pPanel1->Add(pBtn1);
CMFCRibbonButton* pBtn2 = new CMFCRibbonButton(ID_RIBBON_BTN_2, _T("Menu Button"), 1, 1);
// 可以通过SetMenu()函数为按钮设置一个子菜单
pBtn2->SetMenu(IDR_RIBBON_MENU_1);
pBtn2->SetAlwaysLargeImage();
pPanel1->Add(pBtn2);
CMFCRibbonButton* pBtn3 = new CMFCRibbonButton(ID_RIBBON_BTN_3, _T("Split Button"), 2, 2);
pBtn3->SetMenu(IDR_RIBBON_MENU_1, TRUE);
pBtn3->SetAlwaysLargeImage();
// 可以通过RemoveSubItem()和AddSubItem()动态地改变按钮的子项目
pBtn3->RemoveSubItem(0);
pBtn3->AddSubItem(new CMFCRibbonButton(ID_RIBBON_MBTN_1, _T("Item 1"), 2), 0);
pPanel1->Add(pBtn3);
// 创建新的面板,用于放置小图标按钮
CMFCRibbonPanel* pPanel2 = pCategory->AddPanel(_T("Small"));
// 创建小图标按钮
CMFCRibbonButton* pBtn4 = new CMFCRibbonButton(ID_RIBBON_BTN_4, _T("Button"), 3);
pPanel2->Add(pBtn4);
CMFCRibbonButton* pBtn5 = new CMFCRibbonButton(ID_RIBBON_BTN_5, _T("Menu Button"), 4);
pBtn5->SetMenu(IDR_RIBBON_MENU_1);
pPanel2->Add(pBtn5);
CMFCRibbonButton* pBtn6 = new CMFCRibbonButton(ID_RIBBON_BTN_6, _T("Split Button"), 5);
pBtn6->SetMenu(IDR_RIBBON_MENU_1, TRUE);
pBtn6->SetAlwaysLargeImage();
pBtn6->RemoveSubItem(1);
pBtn6->AddSubItem(new CMFCRibbonButton(ID_RIBBON_MBTN_2, _T("Item 2"), 5), 1);
pPanel2->Add(pBtn6);
//创建新的面板,用于放置复选按钮
CMFCRibbonPanel* pPanel3 = pCategory->AddPanel(_T("Check Boxes"));
pPanel3->Add(new CMFCRibbonCheckBox(ID_RIBBON_BTN_7, _T("Check Box 1")));
pPanel3->Add(new CMFCRibbonCheckBox(ID_RIBBON_BTN_8, _T("Check Box 2")));
pPanel3->Add(new CMFCRibbonCheckBox(ID_RIBBON_BTN_9, _T("Check Box 3")));
CMFCRibbonPanel* pPanel1 = pCategory->AddPanel(_T("Large Buttons"));
// 创建按钮
CMFCRibbonButton* pBtn1 = new CMFCRibbonButton(ID_RIBBON_BTN_1, _T("Button"), 0, 0);
// 指定使用大图标
pBtn1->SetAlwaysLargeImage();
// 将按钮添加到面板中
pPanel1->Add(pBtn1);
CMFCRibbonButton* pBtn2 = new CMFCRibbonButton(ID_RIBBON_BTN_2, _T("Menu Button"), 1, 1);
// 可以通过SetMenu()函数为按钮设置一个子菜单
pBtn2->SetMenu(IDR_RIBBON_MENU_1);
pBtn2->SetAlwaysLargeImage();
pPanel1->Add(pBtn2);
CMFCRibbonButton* pBtn3 = new CMFCRibbonButton(ID_RIBBON_BTN_3, _T("Split Button"), 2, 2);
pBtn3->SetMenu(IDR_RIBBON_MENU_1, TRUE);
pBtn3->SetAlwaysLargeImage();
// 可以通过RemoveSubItem()和AddSubItem()动态地改变按钮的子项目
pBtn3->RemoveSubItem(0);
pBtn3->AddSubItem(new CMFCRibbonButton(ID_RIBBON_MBTN_1, _T("Item 1"), 2), 0);
pPanel1->Add(pBtn3);
// 创建新的面板,用于放置小图标按钮
CMFCRibbonPanel* pPanel2 = pCategory->AddPanel(_T("Small"));
// 创建小图标按钮
CMFCRibbonButton* pBtn4 = new CMFCRibbonButton(ID_RIBBON_BTN_4, _T("Button"), 3);
pPanel2->Add(pBtn4);
CMFCRibbonButton* pBtn5 = new CMFCRibbonButton(ID_RIBBON_BTN_5, _T("Menu Button"), 4);
pBtn5->SetMenu(IDR_RIBBON_MENU_1);
pPanel2->Add(pBtn5);
CMFCRibbonButton* pBtn6 = new CMFCRibbonButton(ID_RIBBON_BTN_6, _T("Split Button"), 5);
pBtn6->SetMenu(IDR_RIBBON_MENU_1, TRUE);
pBtn6->SetAlwaysLargeImage();
pBtn6->RemoveSubItem(1);
pBtn6->AddSubItem(new CMFCRibbonButton(ID_RIBBON_MBTN_2, _T("Item 2"), 5), 1);
pPanel2->Add(pBtn6);
//创建新的面板,用于放置复选按钮
CMFCRibbonPanel* pPanel3 = pCategory->AddPanel(_T("Check Boxes"));
pPanel3->Add(new CMFCRibbonCheckBox(ID_RIBBON_BTN_7, _T("Check Box 1")));
pPanel3->Add(new CMFCRibbonCheckBox(ID_RIBBON_BTN_8, _T("Check Box 2")));
pPanel3->Add(new CMFCRibbonCheckBox(ID_RIBBON_BTN_9, _T("Check Box 3")));