MainFrm.cpp
上传用户:sesekoo
上传日期:2020-07-18
资源大小:21543k
文件大小:13k
源码类别:

界面编程

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "StateInFile.h"
  5. #include "MainFrm.h"
  6. #if (!defined __EXT_MFC_NO_SHELL)
  7. #if (!defined __EXT_CONTROLS_SHELL_H)
  8. #include <ExtControlsShell.h>
  9. #endif
  10. #endif
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CMainFrame
  18. IMPLEMENT_DYNAMIC( CMainFrame, CFrameWnd )
  19. BEGIN_MESSAGE_MAP( CMainFrame, CFrameWnd )
  20. //{{AFX_MSG_MAP(CMainFrame)
  21. ON_WM_CREATE()
  22. ON_WM_SETFOCUS()
  23. ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  24. ON_COMMAND(ID_FILE_SAVE, OnFileSave)
  25. //}}AFX_MSG_MAP
  26. ON_COMMAND_EX(ID_VIEW_MENUBAR, OnBarCheck )
  27. ON_UPDATE_COMMAND_UI(ID_VIEW_MENUBAR, OnUpdateControlBarMenu)
  28. ON_COMMAND_EX(ID_VIEW_UI_LOOK_BAR, OnBarCheck )
  29. ON_UPDATE_COMMAND_UI(ID_VIEW_UI_LOOK_BAR, OnUpdateControlBarMenu)
  30. ON_COMMAND_EX(ID_VIEW_RESIZABLE_BAR_0, OnBarCheck )
  31. ON_UPDATE_COMMAND_UI(ID_VIEW_RESIZABLE_BAR_0, OnUpdateControlBarMenu)
  32. ON_COMMAND_EX(ID_VIEW_RESIZABLE_BAR_1, OnBarCheck )
  33. ON_UPDATE_COMMAND_UI(ID_VIEW_RESIZABLE_BAR_1, OnUpdateControlBarMenu)
  34. END_MESSAGE_MAP()
  35. static UINT indicators[] =
  36. {
  37. ID_SEPARATOR,           // status line indicator
  38. ID_INDICATOR_CAPS,
  39. ID_INDICATOR_NUM,
  40. ID_INDICATOR_SCRL,
  41. };
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CMainFrame construction/destruction
  44. CMainFrame::CMainFrame()
  45. {
  46. CWinApp * pApp = ::AfxGetApp();
  47. ASSERT( pApp != NULL );
  48. ASSERT( pApp->m_pszRegistryKey != NULL );
  49. ASSERT( pApp->m_pszRegistryKey[0] != _T('') );
  50. ASSERT( pApp->m_pszProfileName != NULL );
  51. ASSERT( pApp->m_pszProfileName[0] != _T('') );
  52. if( ! g_PaintManager.PaintManagerStateLoad(
  53. pApp->m_pszRegistryKey,
  54. pApp->m_pszProfileName,
  55. pApp->m_pszProfileName
  56. )
  57. )
  58. g_PaintManager.InstallPaintManager(
  59. RUNTIME_CLASS(CExtPaintManagerOffice2007_R2_LunaBlue)
  60. );
  61. // window placement persistence
  62. ::memset( &m_dataFrameWP, 0, sizeof(WINDOWPLACEMENT) );
  63. m_dataFrameWP.length = sizeof(WINDOWPLACEMENT);
  64. m_dataFrameWP.showCmd = SW_HIDE;
  65. }
  66. CMainFrame::~CMainFrame()
  67. {
  68. }
  69. void CMainFrame::OnUpdateControlBarMenu(CCmdUI* pCmdUI)
  70. {
  71. CExtControlBar::DoFrameBarCheckUpdate( this, pCmdUI, true );
  72. }
  73. BOOL CMainFrame::OnBarCheck(UINT nID)
  74. {
  75. return CExtControlBar::DoFrameBarCheckCmd( this, nID, true );
  76. }
  77. static UINT g_statBasicCommands[] =
  78. {
  79. ID_APP_ABOUT,
  80. ID_APP_EXIT,
  81. // ID_VIEW_MENUBAR,
  82. ID_VIEW_TOOLBAR,
  83. ID_VIEW_UI_LOOK_BAR,
  84. // ID_VIEW_RESIZABLE_BAR_0,
  85. // ID_VIEW_RESIZABLE_BAR_1,
  86. ID_VIEW_STATUS_BAR,
  87. ID_FILE_OPEN,
  88. ID_FILE_SAVE,
  89. ID_EDIT_COPY,
  90. ID_EDIT_CUT,
  91. ID_EDIT_PASTE,
  92. 0, // end of list
  93. };
  94. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  95. {
  96. if( CExtNCW < CFrameWnd > :: OnCreate( lpCreateStruct ) == -1 )
  97. return -1;
  98. // create a view to occupy the client area of the frame
  99. if( ! m_wndView.Create(
  100. NULL,
  101. NULL,
  102. AFX_WS_DEFAULT_VIEW,
  103. CRect( 0, 0, 0, 0 ),
  104. this,
  105. AFX_IDW_PANE_FIRST,
  106. NULL
  107. )
  108. )
  109. {
  110. TRACE0("Failed to create view windown");
  111. return -1;
  112. }
  113. CWinApp * pApp = ::AfxGetApp();
  114. ASSERT( pApp != NULL );
  115. ASSERT( pApp->m_pszRegistryKey != NULL );
  116. ASSERT( pApp->m_pszRegistryKey[0] != _T('') );
  117. ASSERT( pApp->m_pszProfileName != NULL );
  118. ASSERT( pApp->m_pszProfileName[0] != _T('') );
  119. ASSERT( pApp->m_pszProfileName != NULL );
  120. g_CmdManager->ProfileSetup(
  121. pApp->m_pszProfileName,
  122. GetSafeHwnd()
  123. );
  124. VERIFY(
  125. g_CmdManager->UpdateFromMenu(
  126. pApp->m_pszProfileName,
  127. IDR_MAINFRAME
  128. )
  129. );
  130. if( !m_wndMenuBar.Create(
  131. NULL, // _T("Menu Bar"),
  132. this,
  133. ID_VIEW_MENUBAR
  134. )
  135. )
  136.     {
  137.         TRACE0("Failed to create menubarn");
  138.         return -1;      // failed to create
  139.     }
  140. if( (! m_wndToolBar.Create( NULL, this, AFX_IDW_TOOLBAR ) )
  141. || (! m_wndToolBar.LoadToolBar( IDR_MAINFRAME ) )
  142. )
  143. {
  144. TRACE0("Failed to create the m_wndToolBar toolbarn");
  145. return -1;      // fail to create
  146. }
  147. if( (! m_wndToolBarUiLook.Create( NULL, this, ID_VIEW_UI_LOOK_BAR ) )
  148. || (! m_wndToolBarUiLook.ThemeSwitcherInit() )
  149. )
  150. {
  151. TRACE0("Failed to create the m_wndToolBarUiLook toolbarn");
  152. return -1;      // fail to create
  153. }
  154. if( !m_wndResizableBar0.Create(
  155. _T("Resizable Bar 0"),
  156. this,
  157. ID_VIEW_RESIZABLE_BAR_0
  158. )
  159. )
  160. {
  161. TRACE0("Failed to create m_wndResizableBar0n");
  162. return -1; // fail to create
  163. }
  164. if( !m_wndResizableBar1.Create(
  165. _T("Resizable Bar 1"),
  166. this,
  167. ID_VIEW_RESIZABLE_BAR_1
  168. )
  169. )
  170. {
  171. TRACE0("Failed to create m_wndResizableBar1n");
  172. return -1; // fail to create
  173. }
  174. if (!m_wndStatusBar.Create(this) ||
  175. !m_wndStatusBar.SetIndicators(indicators,
  176.   sizeof(indicators)/sizeof(UINT)))
  177. {
  178. TRACE0("Failed to create status barn");
  179. return -1;      // fail to create
  180. }
  181. m_wndMenuBar.EnableDocking( CBRS_ALIGN_ANY );
  182. m_wndToolBar.EnableDocking( CBRS_ALIGN_ANY );
  183. m_wndToolBarUiLook.EnableDocking( CBRS_ALIGN_ANY );
  184. m_wndResizableBar0.EnableDocking( CBRS_ALIGN_ANY );
  185. m_wndResizableBar1.EnableDocking( CBRS_ALIGN_ANY );
  186. if( ! CExtControlBar::FrameEnableDocking(this) )
  187. {
  188. ASSERT( FALSE );
  189. return -1;
  190. }
  191. VERIFY(
  192. g_CmdManager->SetBasicCommands(
  193. pApp->m_pszProfileName,
  194. g_statBasicCommands
  195. )
  196. );
  197. if( ! CExtControlBar::ProfileBarStateLoad(
  198. this,
  199. pApp->m_pszRegistryKey,
  200. pApp->m_pszProfileName,
  201. pApp->m_pszProfileName,
  202. &m_dataFrameWP
  203. )
  204. )
  205. {
  206. DockControlBar( &m_wndMenuBar );
  207. DockControlBar( &m_wndToolBar );
  208. DockControlBar( &m_wndToolBarUiLook, AFX_IDW_DOCKBAR_RIGHT );
  209. m_wndResizableBar0.DockControlBar( AFX_IDW_DOCKBAR_LEFT, 1 );
  210. m_wndResizableBar1.DockControlBar( AFX_IDW_DOCKBAR_BOTTOM, 2 );
  211. }
  212. g_CmdManager->SerializeState(
  213. pApp->m_pszProfileName,
  214. pApp->m_pszRegistryKey,
  215. pApp->m_pszProfileName,
  216. false
  217. );
  218. return 0;
  219. }
  220. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  221. {
  222. if( ! CExtNCW < CFrameWnd > :: PreCreateWindow( cs ) )
  223. return FALSE;
  224. cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
  225. cs.lpszClass = AfxRegisterWndClass(0);
  226. return TRUE;
  227. }
  228. /////////////////////////////////////////////////////////////////////////////
  229. // CMainFrame diagnostics
  230. #ifdef _DEBUG
  231. void CMainFrame::AssertValid() const
  232. {
  233. CExtNCW < CFrameWnd > :: AssertValid();
  234. }
  235. void CMainFrame::Dump(CDumpContext& dc) const
  236. {
  237. CExtNCW < CFrameWnd > :: Dump( dc );
  238. }
  239. #endif //_DEBUG
  240. /////////////////////////////////////////////////////////////////////////////
  241. // CMainFrame message handlers
  242. void CMainFrame::OnSetFocus(CWnd* pOldWnd)
  243. {
  244. pOldWnd;
  245. // forward focus to the view window
  246. m_wndView.SetFocus();
  247. }
  248. BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
  249. {
  250. // let the view have first crack at the command
  251. if( m_wndView.OnCmdMsg( nID, nCode, pExtra, pHandlerInfo ) )
  252. return TRUE;
  253. // otherwise, do default handling
  254. return CExtNCW < CFrameWnd > :: OnCmdMsg( nID, nCode, pExtra, pHandlerInfo );
  255. }
  256. void CMainFrame::ActivateFrame(int nCmdShow) 
  257. {
  258. // window placement persistence
  259. if( m_dataFrameWP.showCmd != SW_HIDE )
  260. {
  261. SetWindowPlacement( &m_dataFrameWP );
  262. CExtNCW < CFrameWnd > :: ActivateFrame( m_dataFrameWP.showCmd );
  263. m_dataFrameWP.showCmd = SW_HIDE;
  264. return;
  265. }
  266. CExtNCW < CFrameWnd > :: ActivateFrame( nCmdShow );
  267. }
  268. BOOL CMainFrame::DestroyWindow() 
  269. {
  270. CWinApp * pApp = ::AfxGetApp();
  271. ASSERT( pApp != NULL );
  272. ASSERT( pApp->m_pszRegistryKey != NULL );
  273. ASSERT( pApp->m_pszRegistryKey[0] != _T('') );
  274. ASSERT( pApp->m_pszProfileName != NULL );
  275. ASSERT( pApp->m_pszProfileName[0] != _T('') );
  276. VERIFY(
  277. CExtControlBar::ProfileBarStateSave(
  278. this,
  279. pApp->m_pszRegistryKey,
  280. pApp->m_pszProfileName,
  281. pApp->m_pszProfileName
  282. )
  283. );
  284. VERIFY(
  285. g_CmdManager->SerializeState(
  286. pApp->m_pszProfileName,
  287. pApp->m_pszRegistryKey,
  288. pApp->m_pszProfileName,
  289. true
  290. )
  291. );
  292. VERIFY(
  293. g_PaintManager.PaintManagerStateSave(
  294. pApp->m_pszRegistryKey,
  295. pApp->m_pszProfileName,
  296. pApp->m_pszProfileName
  297. )
  298. );
  299. g_CmdManager->ProfileWndRemove( GetSafeHwnd() );
  300. return CExtNCW < CFrameWnd > :: DestroyWindow();
  301. }
  302. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) 
  303. {
  304. if( m_wndToolBarUiLook.PreTranslateMessage( pMsg ) )
  305. return TRUE;
  306. if( m_wndMenuBar.TranslateMainFrameMessage( pMsg ) )
  307. return TRUE;
  308. return CExtNCW < CFrameWnd > :: PreTranslateMessage( pMsg );
  309. }
  310. void CMainFrame::OnFileOpen() 
  311. {
  312. CString strPathName;
  313. #if ( ! defined __EXT_MFC_NO_SHELL_DIALOG_FILE )
  314. CExtShellDialogFile dlgShellFile(
  315. NULL,
  316. CExtShellDialogFile::__EFDT_OPEN_SINGLE
  317. );
  318. dlgShellFile.m_comboFileTypes.SetFilter(
  319. _T("UI state files (*.UI state)|*.UI state|All files (*.*)|*.*||")
  320. );
  321. dlgShellFile.m_nInitialFileTypeIndex = 0;
  322. dlgShellFile.m_bFilesMustExist = true;
  323. dlgShellFile.m_strCustomDialogCaption = _T("Open UI state from file");
  324. dlgShellFile.m_strEditorTextInitial = _T(".\Default.UI state");
  325. if( dlgShellFile.DoModal() != IDOK )
  326. return;
  327. ASSERT( dlgShellFile.m_arrRetValNames.GetSize() == 1 );
  328. strPathName = LPCTSTR(dlgShellFile.m_arrRetValNames[0]);
  329. #else // ( ! defined __EXT_MFC_NO_SHELL_DIALOG_FILE )
  330. CFileDialog dlgFile(
  331. TRUE,
  332. _T("UI state"),
  333. _T(".\Default.UI state"),
  334. OFN_ENABLESIZING|OFN_EXPLORER
  335. |OFN_LONGNAMES|OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST
  336. |OFN_NOCHANGEDIR|OFN_NODEREFERENCELINKS
  337. ,
  338. _T("UI state files (*.UI state)|*.UI state|All files (*.*)|*.*||"),
  339. this
  340. );
  341. dlgFile.m_ofn.lpstrTitle = _T("Open UI state from file");
  342. if( dlgFile.DoModal() != IDOK )
  343. return;
  344. strPathName = dlgFile.m_ofn.lpstrFile;
  345. ASSERT( ! strPathName.IsEmpty() );
  346. #endif // else from if ( ! defined __EXT_MFC_NO_SHELL_DIALOG_FILE )
  347. try
  348. {
  349. CFile _file(
  350. strPathName,
  351. CFile::modeRead|CFile::shareExclusive
  352. );
  353. CArchive ar( &_file, CArchive::load);
  354. CWinApp * pApp = ::AfxGetApp();
  355. ASSERT( pApp != NULL );
  356. ASSERT( pApp->m_pszRegistryKey != NULL );
  357. ASSERT( pApp->m_pszRegistryKey[0] != _T('') );
  358. ASSERT( pApp->m_pszProfileName != NULL );
  359. ASSERT( pApp->m_pszProfileName[0] != _T('') );
  360. ShowWindow( SW_HIDE );
  361. VERIFY(
  362. CExtControlBar::ProfileBarStateSerialize(
  363. ar,
  364. this,
  365. &m_dataFrameWP
  366. )
  367. );
  368. VERIFY(
  369. g_CmdManager->SerializeState(
  370. pApp->m_pszProfileName,
  371. ar
  372. )
  373. );
  374. ActivateFrame( SW_SHOW );
  375. } // try
  376. catch( CException * pXept )
  377. {
  378. CString sErrorDescription;
  379. VERIFY(
  380. pXept->GetErrorMessage(
  381. sErrorDescription.GetBuffer(8192), 8192
  382. )
  383. );
  384. sErrorDescription.ReleaseBuffer();
  385. ASSERT( !sErrorDescription.IsEmpty() );
  386. pXept->Delete();
  387. ActivateFrame( SW_SHOW );
  388. ::ProfUISMsgBox( GetSafeHwnd(), LPCTSTR(sErrorDescription), _T("State load failed"), MB_ICONERROR );
  389. } // catch( CException * pXept )
  390. catch( ... )
  391. {
  392. ActivateFrame( SW_SHOW );
  393. ::ProfUISMsgBox( GetSafeHwnd(), _T("Unknown exception caught"), _T("State load failed"), MB_ICONERROR );
  394. } // catch( ... )
  395. }
  396. void CMainFrame::OnFileSave() 
  397. {
  398. CString strPathName;
  399. #if ( ! defined __EXT_MFC_NO_SHELL_DIALOG_FILE )
  400. CExtShellDialogFile dlgShellFile(
  401. NULL,
  402. CExtShellDialogFile::__EFDT_SAVE_AS
  403. );
  404. dlgShellFile.m_comboFileTypes.SetFilter(
  405. _T("UI state files (*.UI state)|*.UI state|All files (*.*)|*.*||")
  406. );
  407. dlgShellFile.m_nInitialFileTypeIndex = 0;
  408. dlgShellFile.m_bPathMustExist = true;
  409. dlgShellFile.m_strCustomDialogCaption = _T("Save UI state to file");
  410. dlgShellFile.m_strEditorTextInitial = _T(".\Default.UI state");
  411. if( dlgShellFile.DoModal() != IDOK )
  412. return;
  413. ASSERT( dlgShellFile.m_arrRetValNames.GetSize() == 1 );
  414. strPathName = LPCTSTR(dlgShellFile.m_arrRetValNames[0]);
  415. #else // ( ! defined __EXT_MFC_NO_SHELL_DIALOG_FILE )
  416. CFileDialog dlgFile(
  417. FALSE,
  418. _T("UI state"),
  419. _T(".\Default.UI state"),
  420. OFN_ENABLESIZING|OFN_EXPLORER
  421. |OFN_LONGNAMES|OFN_PATHMUSTEXIST
  422. |OFN_NOCHANGEDIR|OFN_NODEREFERENCELINKS
  423. ,
  424. _T("UI state files (*.UI state)|*.UI state|All files (*.*)|*.*||"),
  425. this
  426. );
  427. dlgFile.m_ofn.lpstrTitle = _T("Save UI state to file");
  428. if( dlgFile.DoModal() != IDOK )
  429. return;
  430. strPathName = dlgFile.m_ofn.lpstrFile;
  431. ASSERT( ! strPathName.IsEmpty() );
  432. #endif // else from if ( ! defined __EXT_MFC_NO_SHELL_DIALOG_FILE )
  433. try
  434. {
  435. CFile _file(
  436. strPathName,
  437. CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive
  438. );
  439. CArchive ar( &_file, CArchive::store);
  440. CWinApp * pApp = ::AfxGetApp();
  441. ASSERT( pApp != NULL );
  442. ASSERT( pApp->m_pszRegistryKey != NULL );
  443. ASSERT( pApp->m_pszRegistryKey[0] != _T('') );
  444. ASSERT( pApp->m_pszProfileName != NULL );
  445. ASSERT( pApp->m_pszProfileName[0] != _T('') );
  446. VERIFY(
  447. CExtControlBar::ProfileBarStateSerialize(
  448. ar,
  449. this
  450. )
  451. );
  452. VERIFY(
  453. g_CmdManager->SerializeState(
  454. pApp->m_pszProfileName,
  455. ar
  456. )
  457. );
  458. } // try
  459. catch( CException * pXept )
  460. {
  461. CString sErrorDescription;
  462. VERIFY(
  463. pXept->GetErrorMessage(
  464. sErrorDescription.GetBuffer(8192), 8192
  465. )
  466. );
  467. sErrorDescription.ReleaseBuffer();
  468. ASSERT( !sErrorDescription.IsEmpty() );
  469. pXept->Delete();
  470. ::ProfUISMsgBox( GetSafeHwnd(), LPCTSTR(sErrorDescription), _T("State save failed"), MB_ICONERROR );
  471. } // catch( CException * pXept )
  472. catch( ... )
  473. {
  474. ::ProfUISMsgBox( GetSafeHwnd(), _T("Unknown exception caught"), _T("State save failed"), MB_ICONERROR );
  475. } // catch( ... )
  476. }