一直都想已久一下vs2008SP1中MFC的Ribbon功能,终于这个周末把图形学课第一节的试验搬到VS2008上来了··
帅气的Ribbon功能有着强大有诱惑力。这个程序还是比较简陋的,主要是来测试一下Ribbon的。
上面的工具栏的代码是在MainFrm.CPP中生成的
主要分成3层结构:
第一层:分类(容器) CMFCRibbonCategory
第二层:面板(容器) CMFCRibbonPanel
第三层:元素 CMFCRibbonBaseElem
如下图所示元素可以是CMFCRibbonButton,CMFCRibbonGallery,CMFCRibbonGroup类的或者其派生类的对象
下面举个简单的例子:
加入主分类
Code
bNameValid = strTemp.LoadString(IDS_RIBBON_HOME);//IDS_RIBBON_HOME为分类名在String Table中定义
ASSERT(bNameValid);
CMFCRibbonCategory* pCategoryHome = m_wndRibbonBar.AddCategory(strTemp, IDB_WRITESMALL, IDB_WRITELARGE2);
bNameValid = strTemp.LoadString(IDS_RIBBON_HOME);//IDS_RIBBON_HOME为分类名在String Table中定义
ASSERT(bNameValid);
CMFCRibbonCategory* pCategoryHome = m_wndRibbonBar.AddCategory(strTemp, IDB_WRITESMALL, IDB_WRITELARGE2);
在分类中加入面板
Code
bNameValid = strTemp.LoadString(IDS_RIBBON_CLIPBOARD);
ASSERT(bNameValid);
CMFCRibbonPanel* pPanelClipboard = pCategoryHome->AddPanel(strTemp, m_PanelImages.ExtractIcon(27));
bNameValid = strTemp.LoadString(IDS_RIBBON_CLIPBOARD);
ASSERT(bNameValid);
CMFCRibbonPanel* pPanelClipboard = pCategoryHome->AddPanel(strTemp, m_PanelImages.ExtractIcon(27));
在面板中加入元素
Code
bNameValid = strTemp.LoadString(IDS_RIBBON_PASTE);
ASSERT(bNameValid);
CMFCRibbonButton* pBtnPaste = new CMFCRibbonButton(ID_EDIT_PASTE, strTemp, 0, 0);
pPanelClipboard->Add(pBtnPaste);
bNameValid = strTemp.LoadString(IDS_RIBBON_CUT);
ASSERT(bNameValid);
pPanelClipboard->Add(new CMFCRibbonButton(ID_EDIT_CUT, strTemp, 1));
bNameValid = strTemp.LoadString(IDS_RIBBON_COPY);
ASSERT(bNameValid);
pPanelClipboard->Add(new CMFCRibbonButton(ID_EDIT_COPY, strTemp, 2));
bNameValid = strTemp.LoadString(IDS_RIBBON_UNDO);
ASSERT(bNameValid);
pPanelClipboard->Add(new CMFCRibbonButton(ID_EDIT_UNDO, strTemp,10));
bNameValid = strTemp.LoadString(IDS_RIBBON_PASTE);
ASSERT(bNameValid);
CMFCRibbonButton* pBtnPaste = new CMFCRibbonButton(ID_EDIT_PASTE, strTemp, 0, 0);
pPanelClipboard->Add(pBtnPaste);
bNameValid = strTemp.LoadString(IDS_RIBBON_CUT);
ASSERT(bNameValid);
pPanelClipboard->Add(new CMFCRibbonButton(ID_EDIT_CUT, strTemp, 1));
bNameValid = strTemp.LoadString(IDS_RIBBON_COPY);
ASSERT(bNameValid);
pPanelClipboard->Add(new CMFCRibbonButton(ID_EDIT_COPY, strTemp, 2));
bNameValid = strTemp.LoadString(IDS_RIBBON_UNDO);
ASSERT(bNameValid);
pPanelClipboard->Add(new CMFCRibbonButton(ID_EDIT_UNDO, strTemp,10));
主要添加就这三个步骤啦
其他的细节方面可以参看我的源代码:里面有注释
http://www.codeplex.com/MFCDRAW2008/Release/ProjectReleases.aspx?ReleaseId=18548