学习VS2010 ------ 多彩界面,Ribbon编程

多彩界面,Ribbon编程

  Ribbon是类似于office2007样式的界面,它替代了传统的MFC程序里的菜单和工具栏,MFC默认生成的Ribbon功能少,需要我们自己添加一些控件和图片等元素使界面好看。与菜单资源相比,Ribbon的设计使界面更加直观,带有的图标也使界面看起来更加美观。Ribbon界面设计的重要方面就是要设计所需的图标,设计图标时,需要设计小图标和大图标,Ribbon类的添加有两种方式。

方法一:资源创建

可以利用VS2010带有的资源编辑器创建Ribbon资源,并在程序中调用,调用方式:

m_wndRibbonBar.LoadFromResource(IDR_RIBBON)

在右边的工具栏中,具有与Ribbon界面相关的各种工具,可以选择相应的Ribbon工具按照工具进行添加,可以先添加category,再添加Pane,再根据自己的需要添加各种按钮、按钮集、复选框等。

Ribbon工具栏及其包含的控件如下:


在利用资源编辑的方式编辑Ribbon界面时,注意先利用位图的方式引入category需要的图标,然后再在具体的控件中填写引用的序号,并选用相应的ID,针对下拉式按钮,在创建时,可以利用“Menu Item”项目创建子项目。


   在资源中创建Ribbon界面之后,可以利用ClassWizard添加各个控件的Ribbon变量,并针对各个按钮创建实现的代码函数,实现相应的程序设计。

方法二:编程创建

1.      Ribbon主菜单的设计

       m_MainButton.SetImage(IDB_RIBBON_MAIN);

         m_MainButton.SetToolTipText(_T("File"));

         m_MainButton.SetText(_T("\nf"));

         m_wndRibbonBar.SetApplicationButton(&m_MainButton, CSize (45, 45));

         CMFCRibbonMainPanel*pMainPanel = m_wndRibbonBar.AddMainCategory (_T("File"),IDB_RIBBON_FILE_SMALL, IDB_RIBBON_FILE_LARGE);

         pMainPanel->Add(new CMFCRibbonButton (ID_FILE_NEW, _T("&New"), 0, 0));

         pMainPanel->Add(new CMFCRibbonButton (ID_FILE_OPEN, _T("&Open..."), 1, 1));

         pMainPanel->Add(new CMFCRibbonButton (ID_FILE_SAVE, _T("&Save"), 2, 2));

         pMainPanel->Add(new CMFCRibbonButton (ID_FILE_SAVE_AS, _T("Save &As..."), 3, 3));

         std::auto_ptr<CMFCRibbonButton>apBtnPrint(new CMFCRibbonButton (ID_FILE_PRINT,_T("&Print"), 4, 4));

         apBtnPrint->AddSubItem(new CMFCRibbonLabel (_T("Preview and print the document")));

         apBtnPrint->AddSubItem(new CMFCRibbonButton (ID_FILE_PRINT, _T("&Print"), 4, 4, TRUE));

         apBtnPrint->AddSubItem(new CMFCRibbonButton (ID_FILE_PRINT_DIRECT,_T("&Quick Print"), 6, 6,TRUE));

         apBtnPrint->AddSubItem(new CMFCRibbonButton (ID_FILE_PRINT_PREVIEW,_T("Print Pre&view"), 7, 7,TRUE));

         apBtnPrint->SetKeys (_T("p"),_T("w"));

         pMainPanel->Add (apBtnPrint.release());

         pMainPanel->AddRecentFilesList(_T("Recent Documents"));

         pMainPanel->AddToBottom(new CMFCRibbonMainPanelButton(ID_TOOLS_OPTIONS, _T("Opt&ions"),8));

         pMainPanel->AddToBottom(new CMFCRibbonMainPanelButton (ID_APP_EXIT,_T("E&xit"), 9));

2.      Ribbon的category的添加:

         CMFCRibbonCategory*pCategory = m_wndRibbonBar.AddCategory(_T("&Buttons"),IDB_RIBBON_CATEGORY1_SMALL, IDB_RIBBON_CATEGORY1_LARGE);

         CMFCRibbonPanel*pPanel1 = pCategory->AddPanel(_T("LargeButtons"));

         std::auto_ptr<CMFCRibbonButton>apBtn1(new CMFCRibbonButton(ID_RIBBON_BTN_1,_T("Button"), 0, 0));

         apBtn1->SetAlwaysLargeImage();

         pPanel1->Add(apBtn1.release());

         std::auto_ptr<CMFCRibbonButton>apBtn2(new CMFCRibbonButton(ID_RIBBON_BTN_2,_T("Menu Button"), 1, 1));

         apBtn2->SetMenu(IDR_RIBBON_MENU_1);

         apBtn2->SetAlwaysLargeImage();

         pPanel1->Add(apBtn2.release());

         std::auto_ptr<CMFCRibbonButton>apBtn3(new CMFCRibbonButton(ID_RIBBON_BTN_3,_T("Split Button"), 2, 2));

         apBtn3->SetMenu(IDR_RIBBON_MENU_1,TRUE);

         apBtn3->SetAlwaysLargeImage();

         apBtn3->RemoveSubItem(0);

         apBtn3->AddSubItem(new CMFCRibbonButton(ID_RIBBON_MBTN_1, _T("Item 1"), 2), 0);

         pPanel1->Add(apBtn3.release());

         CMFCRibbonPanel*pPanel2 = pCategory->AddPanel(_T("Small"));

         std::auto_ptr<CMFCRibbonButton>apBtn4(new CMFCRibbonButton(ID_RIBBON_BTN_4,_T("Button"), 3));

         pPanel2->Add(apBtn4.release());

         std::auto_ptr<CMFCRibbonButton>apBtn5(new CMFCRibbonButton(ID_RIBBON_BTN_5,_T("Menu Button"), 4));

         apBtn5->SetMenu(IDR_RIBBON_MENU_1);

         pPanel2->Add(apBtn5.release());

         std::auto_ptr<CMFCRibbonButton>apBtn6(new CMFCRibbonButton(ID_RIBBON_BTN_6,_T("Split Button"), 5));

         apBtn6->SetMenu(IDR_RIBBON_MENU_1,TRUE);

         apBtn6->SetAlwaysLargeImage();

         apBtn6->RemoveSubItem(1);

         apBtn6->AddSubItem(new CMFCRibbonButton(ID_RIBBON_MBTN_2, _T("Item 2"), 5), 1);

         pPanel2->Add(apBtn6.release());

         CMFCRibbonPanel*pPanel3 = pCategory->AddPanel(_T("CheckBoxes"));

         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")));

         pPanel1->SetData(ID_RIBBON_SOURCE_CODE_1_1);

         pPanel2->SetData(ID_RIBBON_SOURCE_CODE_1_2);

         pPanel3->SetData(ID_RIBBON_SOURCE_CODE_1_3);

3.      其它category的添加

添加方法同2

4.      界面风格的变化:

Ribbon支持MFC风格界面的变化,可以根据设定的Style设定不同的界面风格。

switch (m_nAppLook)

{

caseID_VIEW_APPLOOK_2007:

caseID_VIEW_APPLOOK_2007_1:

caseID_VIEW_APPLOOK_2007_2:

caseID_VIEW_APPLOOK_2007_3:

switch (m_nAppLook)

{

caseID_VIEW_APPLOOK_2007:

CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_LunaBlue);

break;

caseID_VIEW_APPLOOK_2007_1:

CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_ObsidianBlack);

break;

caseID_VIEW_APPLOOK_2007_2:

CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Silver);

break;

caseID_VIEW_APPLOOK_2007_3:

         CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Aqua);

         break;

         }

CMFCVisualManager::SetDefaultManager (RUNTIME_CLASS(CMFCVisualManagerOffice2007));

CDockingManager::SetDockingMode (DT_SMART);

break;

default:

         ASSERT(FALSE);

}

最终界面:

猜你喜欢

转载自blog.csdn.net/mr_liyonghong/article/details/14446869