MainFrm.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:15k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "RibbonMDISample.h"
  5. #include "MainFrm.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMainFrame
  13. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  14. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  15. //{{AFX_MSG_MAP(CMainFrame)
  16. ON_WM_CREATE()
  17. ON_COMMAND(ID_VIEW_FULLSCREEN, OnViewFullscreen)
  18. ON_COMMAND(ID_VIEW_NORMAL, OnViewNormal)
  19. ON_UPDATE_COMMAND_UI(ID_VIEW_NORMAL, OnUpdateViewNormal)
  20. ON_COMMAND(ID_WINDOW_ARRANGE, OnWindowArrange)
  21. ON_UPDATE_COMMAND_UI(ID_WINDOW_SWITCH, OnUpdateWindowSwitch)
  22. ON_COMMAND(ID_VIEW_WORKSPACE, OnViewWorkspace)
  23. ON_UPDATE_COMMAND_UI(ID_VIEW_WORKSPACE, OnUpdateViewWorkspace)
  24. ON_WM_CLOSE()
  25. //}}AFX_MSG_MAP
  26. ON_UPDATE_COMMAND_UI(XTP_ID_RIBBONCONTROLTAB, OnUpdateRibbonTab)
  27. ON_COMMAND(XTP_ID_CUSTOMIZE, OnCustomize)
  28. ON_COMMAND(XTP_ID_RIBBONCUSTOMIZE, OnCustomizeQuickAccess)
  29. ON_COMMAND(ID_OPTIONS_RIGHTTOLEFT, OnOptionsRighttoleft)
  30. ON_UPDATE_COMMAND_UI(ID_OPTIONS_RIGHTTOLEFT, OnUpdateOptionsRighttoleft)
  31. ON_COMMAND_RANGE(ID_OPTIONS_STYLEBLUE, ID_OPTIONS_STYLEAQUA, OnOptionsStyle)
  32. ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_STYLEBLUE, ID_OPTIONS_STYLEAQUA, OnUpdateOptionsStyle)
  33. END_MESSAGE_MAP()
  34. static UINT indicators[] =
  35. {
  36. ID_SEPARATOR,           // status line indicator
  37. ID_INDICATOR_CAPS,
  38. ID_INDICATOR_NUM,
  39. ID_INDICATOR_SCRL,
  40. };
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CMainFrame construction/destruction
  43. CMainFrame::CMainFrame()
  44. {
  45. m_nRibbonStyle = ID_OPTIONS_STYLEBLUE;
  46. }
  47. CMainFrame::~CMainFrame()
  48. {
  49. }
  50. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  51. {
  52. if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  53. return -1;
  54. if (!m_wndStatusBar.Create(this) ||
  55. !m_wndStatusBar.SetIndicators(indicators,
  56. sizeof(indicators)/sizeof(UINT)))
  57. {
  58. TRACE0("Failed to create status barn");
  59. return -1;      // fail to create
  60. }
  61. if (!InitCommandBars())
  62. return -1;
  63. CXTPCommandBars* pCommandBars = GetCommandBars();
  64. XTPPaintManager()->SetTheme(xtpThemeRibbon);
  65. m_wndStatusBar.SetDrawDisabledText(FALSE);
  66. m_wndStatusBar.SetCommandBars(pCommandBars);
  67. m_wndStatusBar.GetStatusBarCtrl().SetMinHeight(22);
  68. m_wndStatusBar.GetPane(0)->SetMargins(8, 1, 2, 1);
  69. //////////////////////////////////////////////////////////////////////////
  70. // IMPORTANT:
  71. // Ribbon bitmaps resources by default located in application executable. 
  72. //
  73. // You also can specify handle to find ribbon resources:
  74. //((CXTPOffice2007Theme*)XTPPaintManager())->SetImageHandle(hHandle);
  75. //HMODULE hModule = LoadLibrary(m_csStylesPath + _T("Office2007Blue.dll"));
  76. //((CXTPOffice2007Theme*)pCommandBars->GetPaintManager())->SetImageHandle(hModule);
  77. CXTPToolTipContext* pToolTipContext = GetCommandBars()->GetToolTipContext();
  78. pToolTipContext->SetStyle(xtpToolTipOffice2007);
  79. pToolTipContext->ShowTitleAndDescription();
  80. pToolTipContext->SetMargin(CRect(2, 2, 2, 2));
  81. pToolTipContext->SetMaxTipWidth(180);
  82. pToolTipContext->SetFont(pCommandBars->GetPaintManager()->GetIconFont());
  83. pCommandBars->GetCommandBarsOptions()->ShowKeyboardCues(xtpKeyboardCuesShowWindowsDefault);
  84. pCommandBars->GetCommandBarsOptions()->bToolBarAccelTips = TRUE;
  85. pCommandBars->GetShortcutManager()->SetAccelerators(IDR_MAINFRAME);
  86. LoadIcons();
  87. if (!CreateRibbonBar())
  88. {
  89. TRACE0("Failed to create ribbonn");
  90. return -1;
  91. }
  92. m_wndClient.Attach(this, FALSE);
  93. m_wndClient.GetToolTipContext()->SetStyle(xtpToolTipOffice2007);
  94. m_bShowWorkspace = TRUE;
  95. LoadCommandBars(_T("CommandBars"));
  96. return 0;
  97. }
  98. void CMainFrame::LoadIcons()
  99. {
  100. CXTPCommandBars* pCommandBars = GetCommandBars();
  101. pCommandBars->GetImageManager()->SetIcons(IDR_SMALLICONS);
  102. pCommandBars->GetImageManager()->SetIcons(IDR_LARGEICONS);
  103. UINT uiGroupClipboard[] = {ID_EDIT_PASTE, ID_EDIT_CUT, ID_EDIT_COPY, ID_FORMAT_PAINTER};
  104. pCommandBars->GetImageManager()->SetIcons(ID_GROUP_CLIPBOARD, uiGroupClipboard, _countof(uiGroupClipboard), CSize(16, 16));
  105. UINT uiGroupFind[] = {ID_EDIT_FIND, ID_EDIT_REPLACE, ID_EDIT_GOTO, ID_EDIT_SELECT};
  106. pCommandBars->GetImageManager()->SetIcons(ID_GROUP_EDITING, uiGroupFind, _countof(uiGroupFind), CSize(16, 16));
  107. UINT uCommand = {IDB_GEAR};
  108. pCommandBars->GetImageManager()->SetIcons(IDB_GEAR, &uCommand, 1, CSize(0, 0), xtpImageNormal);
  109. }
  110. //#define CREATE_FROM_XML
  111. BOOL CMainFrame::CreateRibbonBar()
  112. {
  113. CXTPCommandBars* pCommandBars = GetCommandBars();
  114. CMenu menu;
  115. menu.Attach(::GetMenu(m_hWnd));
  116. SetMenu(NULL);
  117. #ifndef CREATE_FROM_XML
  118. CXTPRibbonBar* pRibbonBar = (CXTPRibbonBar*)pCommandBars->Add(_T("The Ribbon"), xtpBarTop, RUNTIME_CLASS(CXTPRibbonBar));
  119. if (!pRibbonBar)
  120. {
  121. return FALSE;
  122. }
  123. pRibbonBar->EnableDocking(0);
  124. CXTPControlPopup* pControlFile = (CXTPControlPopup*)pRibbonBar->AddSystemButton(0);
  125. pControlFile->SetCommandBar(menu.GetSubMenu(0));
  126. pControlFile->SetIconId(IDB_GEAR);
  127. CXTPRibbonTab* pTabHome = pRibbonBar->AddTab(ID_TAB_HOME);
  128. CXTPRibbonGroup* pGroupFile = pTabHome->AddGroup(ID_GROUP_FILE);
  129. pGroupFile->Add(xtpControlButton, ID_FILE_NEW);
  130. pGroupFile->Add(xtpControlButton, ID_FILE_OPEN);
  131. pGroupFile->Add(xtpControlButton, ID_FILE_CLOSE);
  132. CXTPControlPopup* pControlSave = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_FILE_SAVE);
  133. pControlSave->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FILE_SAVE);
  134. pControlSave->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FILE_SAVE_AS);
  135. CXTPControlPopup* pControlPrint = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_FILE_PRINT);
  136. pControlPrint->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FILE_PRINT);
  137. pControlPrint->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FILE_PRINT_SETUP);
  138. pControlPrint->SetBeginGroup(TRUE);
  139. CXTPRibbonTab* pTabEdit = pRibbonBar->AddTab(ID_TAB_EDIT);
  140. CXTPRibbonGroup* pGroupClipborad = pTabEdit->AddGroup(ID_GROUP_CLIPBOARD);
  141. pGroupClipborad->ShowOptionButton();
  142. CXTPControlPopup* pControlPaste = (CXTPControlPopup*)pGroupClipborad->Add(xtpControlSplitButtonPopup, ID_EDIT_PASTE);
  143. pControlPaste->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_EDIT_PASTE);
  144. pControlPaste->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_EDIT_PASTE_SPECIAL);
  145. pGroupClipborad->Add(xtpControlButton, ID_EDIT_CUT);
  146. pGroupClipborad->Add(xtpControlButton, ID_EDIT_COPY);
  147. pGroupClipborad->Add(xtpControlButton, ID_FORMAT_PAINTER);
  148. CXTPRibbonGroup* pGroupEditing = pTabEdit->AddGroup(ID_GROUP_EDITING);
  149. pGroupEditing->Add(xtpControlButton, ID_EDIT_FIND);
  150. pGroupEditing->Add(xtpControlButton, ID_EDIT_REPLACE);
  151. pGroupEditing->Add(xtpControlButton, ID_EDIT_GOTO);
  152. CXTPControlPopup* pControlSelect = (CXTPControlPopup*)pGroupEditing->Add(xtpControlPopup, ID_EDIT_SELECT);
  153. pControlSelect->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_EDIT_SELECT_ALL);
  154. pControlSelect->SetFlags(xtpFlagManualUpdate);
  155. CXTPRibbonTab* pTabView = pRibbonBar->AddTab(ID_TAB_VIEW);
  156. CXTPRibbonGroup* pGroupDocumentView = pTabView->AddGroup(ID_GROUP_DOCUMENTVIEWS);
  157. pGroupDocumentView->Add(xtpControlButton, ID_VIEW_NORMAL);
  158. pGroupDocumentView->Add(xtpControlButton, ID_FILE_PRINT_PREVIEW);
  159. pGroupDocumentView->Add(xtpControlButton, ID_VIEW_FULLSCREEN);
  160. CXTPRibbonGroup* pGroupShowHide = pTabView->AddGroup(ID_GROUP_SHOWHIDE);
  161. pGroupShowHide->Add(xtpControlCheckBox, ID_VIEW_STATUS_BAR);
  162. pGroupShowHide->Add(xtpControlCheckBox, ID_VIEW_WORKSPACE);
  163. CXTPRibbonGroup* pGroupWindow = pTabView->AddGroup(ID_GROUP_WINDOW);
  164. pGroupWindow->Add(xtpControlButton, ID_WINDOW_NEW);
  165. pGroupWindow->Add(xtpControlButton, ID_WINDOW_ARRANGE);
  166. CXTPControlPopup* pControlSwitchWindows = (CXTPControlPopup*)pGroupWindow->Add(xtpControlPopup, ID_WINDOW_SWITCH);
  167. pControlSwitchWindows->GetCommandBar()->GetControls()->Add(xtpControlButton, XTP_ID_WINDOWLIST);
  168. CXTPControlPopup* pControlOptions = (CXTPControlPopup*)pRibbonBar->GetControls()->Add(xtpControlPopup, -1);
  169. pControlOptions->SetFlags(xtpFlagRightAlign);
  170. CMenu mnuOptions;
  171. mnuOptions.LoadMenu(IDR_MENU_OPTIONS);
  172. pControlOptions->SetCommandBar(mnuOptions.GetSubMenu(0));
  173. pControlOptions->SetCaption(_T("Options"));
  174. CXTPControl* pControlAbout = pRibbonBar->GetControls()->Add(xtpControlButton, ID_APP_ABOUT);
  175. pControlAbout->SetFlags(xtpFlagRightAlign);
  176. pRibbonBar->GetQuickAccessControls()->Add(xtpControlButton, ID_FILE_SAVE);
  177. pRibbonBar->GetQuickAccessControls()->Add(xtpControlButton, ID_EDIT_UNDO);
  178. pRibbonBar->GetQuickAccessControls()->Add(xtpControlButton, ID_FILE_PRINT);
  179. pRibbonBar->GetQuickAccessControls()->CreateOriginalControls();
  180. #else
  181. CXTPPropExchangeXMLNode px(TRUE, 0, _T("Settings"));
  182. if (px.LoadFromResource(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_LAYOUT), RT_HTML))
  183. {
  184. CXTPPropExchangeSection pxCommandBars(px.GetSection(_T("CommandBars")));
  185. XTP_COMMANDBARS_PROPEXCHANGE_PARAM param; 
  186. param.bSerializeControls = TRUE;
  187. GetCommandBars()->DoPropExchange(&pxCommandBars, &param);
  188. }
  189. else
  190. {
  191. AfxMessageBox(_T("Can't load Layout"));
  192. return FALSE;
  193. }
  194. CXTPRibbonBar* pRibbonBar = (CXTPRibbonBar*)GetCommandBars()->GetMenuBar();
  195. pRibbonBar->EnableCustomization(FALSE);
  196. pRibbonBar->GetQuickAccessControls()->SetOriginalControls(new CXTPOriginalControls());
  197. pRibbonBar->GetQuickAccessControls()->GetOriginalControls()->Add(xtpControlButton, ID_FILE_SAVE);
  198. pRibbonBar->GetQuickAccessControls()->GetOriginalControls()->Add(xtpControlButton, ID_EDIT_UNDO);
  199. pRibbonBar->GetQuickAccessControls()->GetOriginalControls()->Add(xtpControlButton, ID_FILE_PRINT);
  200. #endif
  201. pRibbonBar->EnableFrameTheme();
  202. return TRUE;
  203. }
  204. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  205. {
  206. if( !CMDIFrameWnd::PreCreateWindow(cs) )
  207. return FALSE;
  208. cs.lpszClass = _T("XTPMainFrame");
  209. CXTPDrawHelpers::RegisterWndClass(AfxGetInstanceHandle(), cs.lpszClass, 
  210. CS_DBLCLKS, AfxGetApp()->LoadIcon(IDR_MAINFRAME));
  211. return TRUE;
  212. }
  213. /////////////////////////////////////////////////////////////////////////////
  214. // CMainFrame diagnostics
  215. #ifdef _DEBUG
  216. void CMainFrame::AssertValid() const
  217. {
  218. CMDIFrameWnd::AssertValid();
  219. }
  220. void CMainFrame::Dump(CDumpContext& dc) const
  221. {
  222. CMDIFrameWnd::Dump(dc);
  223. }
  224. #endif //_DEBUG
  225. /////////////////////////////////////////////////////////////////////////////
  226. // CMainFrame message handlers
  227. void CMainFrame::OnUpdateRibbonTab(CCmdUI* pCmdUI)
  228. {
  229. CXTPRibbonControlTab* pControl = DYNAMIC_DOWNCAST(CXTPRibbonControlTab, CXTPControl::FromUI(pCmdUI));
  230. if (!pControl)
  231. return;
  232. CXTPRibbonTab* pTab = pControl->FindTab(ID_TAB_EDIT);
  233. if (!pTab)
  234. return;
  235. pTab->SetVisible(MDIGetActive() != NULL);
  236. }
  237. void CMainFrame::OnViewFullscreen() 
  238. {
  239. // TODO: Add your command handler code here
  240. }
  241. void CMainFrame::OnViewNormal() 
  242. {
  243. // TODO: Add your command handler code here
  244. }
  245. void CMainFrame::OnUpdateViewNormal(CCmdUI* pCmdUI) 
  246. {
  247. pCmdUI->SetCheck(1);
  248. }
  249. void CMainFrame::OnWindowArrange() 
  250. {
  251. ::SendMessage(m_hWndMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
  252. }
  253. void CMainFrame::OnUpdateWindowSwitch(CCmdUI* pCmdUI) 
  254. {
  255. pCmdUI->Enable(MDIGetActive() != NULL ? TRUE : FALSE);
  256. }
  257. void CMainFrame::OnViewWorkspace()
  258. {
  259. m_bShowWorkspace ^= 1;
  260. m_wndClient.ShowWorkspace(m_bShowWorkspace);
  261. }
  262. void CMainFrame::OnUpdateViewWorkspace(CCmdUI* pCmdUI)
  263. {
  264. pCmdUI->SetCheck(m_bShowWorkspace);
  265. }
  266. void CMainFrame::OnSetPreviewMode(BOOL bPreview, CPrintPreviewState* pState)
  267. {
  268. // Toggle CommandBars
  269. GetCommandBars()->OnSetPreviewMode(bPreview);
  270. // Toggle Tab Client
  271. m_wndClient.ShowWorkspace(!bPreview);
  272. CMDIFrameWnd::OnSetPreviewMode(bPreview, pState);
  273. }
  274. void CMainFrame::OnClose() 
  275. {
  276. /* CXTPPropExchangeXMLNode px(FALSE, 0, _T("Settings"));
  277. if (px.OnBeforeExchange()) 
  278. {
  279. CXTPPropExchangeSection pxCommandBars(px.GetSection(_T("CommandBars")));
  280. XTP_COMMANDBARS_PROPEXCHANGE_PARAM param; 
  281. param.bSerializeControls = TRUE; 
  282. param.bSaveOriginalControls = FALSE;
  283. param.bSerializeOptions = TRUE;
  284. GetCommandBars()->DoPropExchange(&pxCommandBars, &param);
  285. px.SaveToFile(_T("C:\save.xml"));
  286. }*/
  287. SaveCommandBars(_T("CommandBars"));
  288. CXTPMDIFrameWnd::OnClose();
  289. }
  290. void CMainFrame::ShowCustomizeDialog(int nSelectedPage)
  291. {
  292. CXTPCustomizeSheet cs(GetCommandBars());
  293. CXTPRibbonCustomizeQuickAccessPage pageQuickAccess(&cs);
  294. cs.AddPage(&pageQuickAccess);
  295. pageQuickAccess.AddCategories(IDR_MAINFRAME);
  296. CXTPCustomizeKeyboardPage pageKeyboard(&cs);
  297. cs.AddPage(&pageKeyboard);
  298. pageKeyboard.AddCategories(IDR_MAINFRAME);
  299. CXTPCustomizeOptionsPage pageOptions(&cs);
  300. cs.AddPage(&pageOptions);
  301. CXTPCustomizeCommandsPage* pCommands = cs.GetCommandsPage();
  302. pCommands->AddCategories(IDR_MAINFRAME);
  303. cs.SetActivePage(nSelectedPage);
  304. cs.DoModal();
  305. }
  306. void CMainFrame::OnCustomize()
  307. {
  308. ShowCustomizeDialog(0);
  309. }
  310. void CMainFrame::OnCustomizeQuickAccess()
  311. {
  312. ShowCustomizeDialog(2);
  313. }
  314. void CMainFrame::OnOptionsRighttoleft() 
  315. {
  316. if (GetExStyle() & WS_EX_LAYOUTRTL)
  317. {
  318. GetCommandBars()->SetLayoutRTL(FALSE);
  319. ModifyStyleEx(WS_EX_LAYOUTRTL, 0);
  320. m_wndClient.SetLayoutRTL(FALSE);
  321. }
  322. else
  323. {
  324. GetCommandBars()->SetLayoutRTL(TRUE);
  325. ModifyStyleEx(0, WS_EX_LAYOUTRTL);
  326. m_wndClient.SetLayoutRTL(TRUE);
  327. }
  328. }
  329. void CMainFrame::OnUpdateOptionsRighttoleft(CCmdUI* pCmdUI) 
  330. {
  331. if (XTPSystemVersion()->IsLayoutRTLSupported())
  332. {
  333. pCmdUI->SetCheck(GetExStyle() & WS_EX_LAYOUTRTL ? TRUE : FALSE);
  334. }
  335. else
  336. {
  337. pCmdUI->Enable(FALSE);
  338. }
  339. }
  340. void CMainFrame::OnUpdateOptionsStyle(CCmdUI* pCmdUI)
  341. {
  342. pCmdUI->SetCheck(pCmdUI->m_nID == m_nRibbonStyle ? 1 : 0);
  343. }
  344. void CMainFrame::OnOptionsStyle(UINT nStyle)
  345. {
  346. HMODULE hModule = AfxGetInstanceHandle();
  347. LPCTSTR lpszIniFile = 0;
  348. switch (nStyle)
  349. {
  350. case ID_OPTIONS_STYLEBLUE: 
  351. lpszIniFile = _T("OFFICE2007BLUE.INI"); break;
  352. case ID_OPTIONS_STYLEBLACK: 
  353. lpszIniFile = _T("OFFICE2007BLACK.INI"); break;
  354. case ID_OPTIONS_STYLEAQUA: 
  355. lpszIniFile = _T("OFFICE2007AQUA.INI"); break;
  356. case ID_OPTIONS_STYLESILVER: 
  357. lpszIniFile = _T("OFFICE2007SILVER.INI"); break;
  358. }
  359. ((CXTPOffice2007Theme*)GetCommandBars()->GetPaintManager())->SetImageHandle(hModule, lpszIniFile);
  360. GetCommandBars()->GetImageManager()->RefreshAll();
  361. GetCommandBars()->RedrawCommandBars();
  362. SendMessage(WM_NCPAINT);
  363. m_wndClient.GetPaintManager()->RefreshMetrics();
  364. RedrawWindow(0, 0, RDW_ALLCHILDREN|RDW_INVALIDATE);
  365. m_nRibbonStyle = nStyle;
  366. }