标签:
首先,添加一个Ribbon资源
第一步:所有的CFrameWnd --->CFrameWndEx
第二步:CWinApp----> CWinAppEx
第三步:APP中添加
// 实现 UINT m_nAppLook;
第四步:MainFrm.h
protected: // 控件条嵌入成员 CMFCRibbonBar m_wndRibbonBar; //2 CMFCRibbonStatusBar m_wndStatusBar; //3
protected: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); //4--修改 afx_msg void OnApplicationLook(UINT id); //5 afx_msg void OnUpdateApplicationLook(CCmdUI* pCmdUI); //6
afx_msg void OnLink(); //7
第五步:MainFrm.cpp添加消息映射
ON_COMMAND_RANGE(ID_VIEW_APPLOOK_WIN_2000, ID_VIEW_APPLOOK_WINDOWS_7, &CMainFrame::OnApplicationLook) ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_APPLOOK_WIN_2000, ID_VIEW_APPLOOK_WINDOWS_7, &CMainFrame::OnUpdateApplicationLook)
第六步:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWndEx::OnCreate(lpCreateStruct) == -1) return -1; // 基于持久值设置视觉管理器和样式 OnApplicationLook(theApp.m_nAppLook); m_wndRibbonBar.Create(this); m_wndRibbonBar.LoadFromResource(IDR_RIBBON); if (!m_wndStatusBar.Create(this)) { TRACE0("未能创建状态栏\n"); return -1; // 未能创建 } m_wndStatusBar.AddElement(new CMFCRibbonLinkCtrl(ID_STATUSBAR_LINK, _T("众多创意软件 欢迎登陆古典小说网 www.gudianxiaoshuo.com"), _T("http://www.gudianxiaoshuo.com")), _T("Link to gudianxiaoshuo")); // 启用 Visual Studio 2005 样式停靠窗口行为 CDockingManager::SetDockingMode(DT_SMART); // 启用 Visual Studio 2005 样式停靠窗口自动隐藏行为 EnableAutoHidePanes(CBRS_ALIGN_ANY);
第七步
void CMainFrame::OnApplicationLook(UINT id) { CWaitCursor wait; theApp.m_nAppLook = id; switch (theApp.m_nAppLook) { case ID_VIEW_APPLOOK_WIN_2000: CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManager)); m_wndRibbonBar.SetWindows7Look(FALSE); break; case ID_VIEW_APPLOOK_OFF_XP: CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOfficeXP)); m_wndRibbonBar.SetWindows7Look(FALSE); break; case ID_VIEW_APPLOOK_WIN_XP: CMFCVisualManagerWindows::m_b3DTabsXPTheme = TRUE; CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows)); m_wndRibbonBar.SetWindows7Look(FALSE); break; case ID_VIEW_APPLOOK_OFF_2003: CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2003)); CDockingManager::SetDockingMode(DT_SMART); m_wndRibbonBar.SetWindows7Look(FALSE); break; case ID_VIEW_APPLOOK_VS_2005: CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerVS2005)); CDockingManager::SetDockingMode(DT_SMART); m_wndRibbonBar.SetWindows7Look(FALSE); break; case ID_VIEW_APPLOOK_VS_2008: CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerVS2008)); CDockingManager::SetDockingMode(DT_SMART); m_wndRibbonBar.SetWindows7Look(FALSE); break; case ID_VIEW_APPLOOK_WINDOWS_7: CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows7)); CDockingManager::SetDockingMode(DT_SMART); m_wndRibbonBar.SetWindows7Look(TRUE); break; default: switch (theApp.m_nAppLook) { case ID_VIEW_APPLOOK_OFF_2007_BLUE: CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_LunaBlue); break; case ID_VIEW_APPLOOK_OFF_2007_BLACK: CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_ObsidianBlack); break; case ID_VIEW_APPLOOK_OFF_2007_SILVER: CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Silver); break; case ID_VIEW_APPLOOK_OFF_2007_AQUA: CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Aqua); break; } CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2007)); CDockingManager::SetDockingMode(DT_SMART); m_wndRibbonBar.SetWindows7Look(FALSE); } RedrawWindow(NULL, NULL, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_UPDATENOW | RDW_FRAME | RDW_ERASE); theApp.WriteInt(_T("ApplicationLook"), theApp.m_nAppLook); } void CMainFrame::OnUpdateApplicationLook(CCmdUI* pCmdUI) { pCmdUI->SetRadio(theApp.m_nAppLook == pCmdUI->m_nID); } void CMainFrame::OnLink() { CMFCRibbonLinkCtrl* pLink = (CMFCRibbonLinkCtrl*) m_wndStatusBar.FindByID(ID_STATUSBAR_LINK); if (pLink != NULL) { pLink->OpenLink(); } }
第八步:RC添加头文件
#if !defined(_AFXDLL) #include "afxribbon.rc" // MFC ribbon and control bar resources #endif
添加位置很值得注意,此处也总是出问题
合理的位置应该是:
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS) LANGUAGE 4, 2 #pragma code_page(936) #include "res\Import.rc2" // 非 Microsoft Visual C++ 编辑资源 #include "afxres.rc" // 标准组件 #include "afxprint.rc" // 打印/打印预览资源 //添加RIBBON #if !defined(_AFXDLL) #include "afxribbon.rc" // MFC ribbon and control bar resources #endif #endif
但是,保存后又总是丢失,不知道什么原因,我用的VS2010,还没找到原因。
效果图对照:
早期项目
Ribbon--黑色
Ribbon--蓝色
Ribbon--银色
Ribbon---水银色
Ribbon---win7
标签:
原文地址:http://blog.csdn.net/shuilan0066/article/details/42196521