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 "MDI_InnerOuterBars.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_EDIT_COPY, OnEditCopy)
  18. ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  19. ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  20. //}}AFX_MSG_MAP
  21. ON_COMMAND_EX(ID_VIEW_MENUBAR, OnBarCheck )
  22. ON_UPDATE_COMMAND_UI(ID_VIEW_MENUBAR, OnUpdateControlBarMenu)
  23. ON_COMMAND_EX(ID_VIEW_UI_LOOK_BAR, OnBarCheck )
  24. ON_UPDATE_COMMAND_UI(ID_VIEW_UI_LOOK_BAR, OnUpdateControlBarMenu)
  25. ON_COMMAND_EX(ID_VIEW_MAINFRAME_BAR_0, OnBarCheck )
  26. ON_UPDATE_COMMAND_UI(ID_VIEW_MAINFRAME_BAR_0, OnUpdateControlBarMenu)
  27. ON_COMMAND_EX(ID_VIEW_MAINFRAME_BAR_1, OnBarCheck )
  28. ON_UPDATE_COMMAND_UI(ID_VIEW_MAINFRAME_BAR_1, OnUpdateControlBarMenu)
  29. ON_COMMAND_EX(ID_VIEW_MAINFRAME_BAR_2, OnBarCheck )
  30. ON_UPDATE_COMMAND_UI(ID_VIEW_MAINFRAME_BAR_2, OnUpdateControlBarMenu)
  31. ON_REGISTERED_MESSAGE(
  32. CExtPopupMenuWnd::g_nMsgPrepareMenu,
  33. OnExtMenuPrepare
  34. )
  35. END_MESSAGE_MAP()
  36. static UINT indicators[] =
  37. {
  38. ID_SEPARATOR,           // status line indicator
  39. ID_INDICATOR_CAPS,
  40. ID_INDICATOR_NUM,
  41. ID_INDICATOR_SCRL,
  42. };
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CMainFrame construction/destruction
  45. CMainFrame::CMainFrame()
  46. {
  47. CWinApp * pApp = ::AfxGetApp();
  48. ASSERT( pApp != NULL );
  49. ASSERT( pApp->m_pszRegistryKey != NULL );
  50. ASSERT( pApp->m_pszRegistryKey[0] != _T('') );
  51. ASSERT( pApp->m_pszProfileName != NULL );
  52. ASSERT( pApp->m_pszProfileName[0] != _T('') );
  53. if( ! g_PaintManager.PaintManagerStateLoad(
  54. pApp->m_pszRegistryKey,
  55. pApp->m_pszProfileName,
  56. pApp->m_pszProfileName
  57. )
  58. )
  59. g_PaintManager.InstallPaintManager(
  60. RUNTIME_CLASS(CExtPaintManagerNativeXP)
  61. );
  62. // window placement persistence
  63. ::memset( &m_dataFrameWP, 0, sizeof(WINDOWPLACEMENT) );
  64. m_dataFrameWP.length = sizeof(WINDOWPLACEMENT);
  65. m_dataFrameWP.showCmd = SW_HIDE;
  66. }
  67. CMainFrame::~CMainFrame()
  68. {
  69. }
  70. void CMainFrame::OnUpdateControlBarMenu(CCmdUI* pCmdUI)
  71. {
  72. CExtControlBar::DoFrameBarCheckUpdate( this, pCmdUI, true );
  73. }
  74. BOOL CMainFrame::OnBarCheck(UINT nID)
  75. {
  76. return CExtControlBar::DoFrameBarCheckCmd( this, nID, true );
  77. }
  78. static UINT g_statBasicCommands[] =
  79. {
  80. ID_APP_ABOUT,
  81. ID_APP_EXIT,
  82. ID_FILE_NEW,
  83. ID_VIEW_MENUBAR,
  84. ID_VIEW_TOOLBAR,
  85. ID_VIEW_UI_LOOK_BAR,
  86. ID_VIEW_MAINFRAME_BAR_0,
  87. ID_VIEW_MAINFRAME_BAR_1,
  88. ID_VIEW_MAINFRAME_BAR_2,
  89. ID_VIEW_CHILDFRAME_BAR_0,
  90. ID_VIEW_CHILDFRAME_BAR_1,
  91. ID_VIEW_CHILDFRAME_BAR_2,
  92. ID_VIEW_STATUS_BAR,
  93. ID_EDIT_COPY,
  94. ID_EDIT_CUT,
  95. ID_EDIT_PASTE,
  96. ID_WINDOW_CASCADE,
  97. ID_WINDOW_TILE_HORZ,
  98. 0, // end of list
  99. };
  100. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  101. {
  102. if( CExtNCW < CMDIFrameWnd > :: OnCreate( lpCreateStruct ) == -1 )
  103. return -1;
  104. CWinApp * pApp = ::AfxGetApp();
  105. ASSERT( pApp != NULL );
  106. ASSERT( pApp->m_pszRegistryKey != NULL );
  107. ASSERT( pApp->m_pszRegistryKey[0] != _T('') );
  108. ASSERT( pApp->m_pszProfileName != NULL );
  109. ASSERT( pApp->m_pszProfileName[0] != _T('') );
  110. ASSERT( pApp->m_pszProfileName != NULL );
  111. g_CmdManager->ProfileSetup(
  112. pApp->m_pszProfileName,
  113. GetSafeHwnd()
  114. );
  115. VERIFY(
  116. g_CmdManager->UpdateFromMenu(
  117. pApp->m_pszProfileName,
  118. IDR_MAINFRAME
  119. )
  120. );
  121. VERIFY(
  122. g_CmdManager->UpdateFromMenu(
  123. pApp->m_pszProfileName,
  124. IDR_MDITYPE
  125. )
  126. );
  127. VERIFY(
  128. g_CmdManager->UpdateFromToolBar(
  129. pApp->m_pszProfileName,
  130. IDR_TOOLBAR_CHILD0
  131. )
  132. );
  133. VERIFY(
  134. g_CmdManager->UpdateFromToolBar(
  135. pApp->m_pszProfileName,
  136. IDR_TOOLBAR_CHILD1
  137. )
  138. );
  139. m_wndMenuBar.SetMdiWindowPopupName( _T("Window") );
  140. if( !m_wndMenuBar.Create(
  141. NULL, // _T("Menu Bar"),
  142. this,
  143. ID_VIEW_MENUBAR
  144. )
  145. )
  146.     {
  147.         TRACE0("Failed to create menubarn");
  148.         return -1;      // failed to create
  149.     }
  150. if( (! m_wndToolBar.Create( NULL, this, AFX_IDW_TOOLBAR ) )
  151. || (! m_wndToolBar.LoadToolBar( IDR_MAINFRAME ) )
  152. )
  153. {
  154. TRACE0("Failed to create m_wndToolBar toolbarn");
  155. return -1;      // fail to create
  156. }
  157. if( (! m_wndToolBarUiLook.Create( NULL, this, ID_VIEW_UI_LOOK_BAR ) )
  158. || (! m_wndToolBarUiLook.ThemeSwitcherInit() )
  159. )
  160. {
  161. TRACE0("Failed to create m_wndToolBarUiLook toolbarn");
  162. return -1;      // fail to create
  163. }
  164. if( !m_wndResizableBar0.Create(
  165. _T("Resizable Bar 0"),
  166. this,
  167. ID_VIEW_MAINFRAME_BAR_0
  168. )
  169. || !m_wndListBox0.Create(
  170. WS_CHILD|WS_VISIBLE|LBS_NOINTEGRALHEIGHT,
  171. CRect( 0, 0, 0, 0 ),
  172. &m_wndResizableBar0,
  173. UINT( IDC_STATIC )
  174. )
  175. )
  176. {
  177. TRACE0("Failed to create m_wndResizableBar0n");
  178. return -1; // fail to create
  179. }
  180. m_wndListBox0.SetFont( CFont::FromHandle( (HFONT)::GetStockObject(DEFAULT_GUI_FONT) ) );
  181. m_wndListBox0.AddString( _T("CMainFrame::m_wndListBox0") );
  182. if( !m_wndResizableBar1.Create(
  183. _T("Resizable Bar 1"),
  184. this,
  185. ID_VIEW_MAINFRAME_BAR_1
  186. )
  187. || !m_wndListBox1.Create(
  188. WS_CHILD|WS_VISIBLE|LBS_NOINTEGRALHEIGHT,
  189. CRect( 0, 0, 0, 0 ),
  190. &m_wndResizableBar1,
  191. UINT( IDC_STATIC )
  192. )
  193. )
  194. {
  195. TRACE0("Failed to create m_wndResizableBar1n");
  196. return -1; // fail to create
  197. }
  198. m_wndListBox1.SetFont( CFont::FromHandle( (HFONT)::GetStockObject(DEFAULT_GUI_FONT) ) );
  199. m_wndListBox1.AddString( _T("CMainFrame::m_wndListBox1") );
  200. if( !m_wndResizableBar2.Create(
  201. _T("Resizable Bar 2"),
  202. this,
  203. ID_VIEW_MAINFRAME_BAR_2
  204. )
  205. || !m_wndListBox2.Create(
  206. WS_CHILD|WS_VISIBLE|LBS_NOINTEGRALHEIGHT,
  207. CRect( 0, 0, 0, 0 ),
  208. &m_wndResizableBar2,
  209. UINT( IDC_STATIC )
  210. )
  211. )
  212. {
  213. TRACE0("Failed to create m_wndResizableBar2n");
  214. return -1; // fail to create
  215. }
  216. m_wndListBox2.SetFont( CFont::FromHandle( (HFONT)::GetStockObject(DEFAULT_GUI_FONT) ) );
  217. m_wndListBox2.AddString( _T("CMainFrame::m_wndListBox2") );
  218. if (!m_wndStatusBar.Create(this) ||
  219. !m_wndStatusBar.SetIndicators(indicators,
  220.   sizeof(indicators)/sizeof(UINT)))
  221. {
  222. TRACE0("Failed to create status barn");
  223. return -1;      // fail to create
  224. }
  225. m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY);
  226. m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  227. m_wndToolBarUiLook.EnableDocking(CBRS_ALIGN_ANY);
  228. m_wndResizableBar0.EnableDocking(CBRS_ALIGN_ANY);
  229. m_wndResizableBar1.EnableDocking(CBRS_ALIGN_ANY);
  230. m_wndResizableBar2.EnableDocking(CBRS_ALIGN_ANY);
  231. if( !CExtControlBar::FrameEnableDocking(this) )
  232. {
  233. ASSERT( FALSE );
  234. return -1;
  235. }
  236. #if (!defined __EXT_MFC_NO_TAB_CONTROLBARS)
  237. if( !CExtControlBar::FrameInjectAutoHideAreas(this) )
  238. {
  239. ASSERT( FALSE );
  240. return -1;
  241. }
  242. #endif // (!defined __EXT_MFC_NO_TAB_CONTROLBARS)
  243. DockControlBar(&m_wndMenuBar);
  244. DockControlBar(&m_wndToolBar);
  245. DockControlBar(&m_wndToolBarUiLook,AFX_IDW_DOCKBAR_RIGHT);
  246. m_wndResizableBar0.DockControlBar( AFX_IDW_DOCKBAR_LEFT, 1 );
  247. m_wndResizableBar0.DockControlBar( &m_wndResizableBar1, true );
  248. m_wndResizableBar2.DockControlBar( AFX_IDW_DOCKBAR_RIGHT, 1 );
  249. VERIFY(
  250. g_CmdManager->SetBasicCommands(
  251. pApp->m_pszProfileName,
  252. g_statBasicCommands
  253. )
  254. );
  255. CExtControlBar::ProfileBarStateLoad(
  256. this,
  257. pApp->m_pszRegistryKey,
  258. pApp->m_pszProfileName,
  259. pApp->m_pszProfileName,
  260. &m_dataFrameWP
  261. );
  262. g_CmdManager->SerializeState(
  263. pApp->m_pszProfileName,
  264. pApp->m_pszRegistryKey,
  265. pApp->m_pszProfileName,
  266. false
  267. );
  268. PostMessage( WM_COMMAND, ID_FILE_NEW );
  269. return 0;
  270. }
  271. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  272. {
  273. if( ! CExtNCW < CMDIFrameWnd > :: PreCreateWindow( cs ) )
  274. return FALSE;
  275. return TRUE;
  276. }
  277. /////////////////////////////////////////////////////////////////////////////
  278. // CMainFrame diagnostics
  279. #ifdef _DEBUG
  280. void CMainFrame::AssertValid() const
  281. {
  282. CExtNCW < CMDIFrameWnd > :: AssertValid();
  283. }
  284. void CMainFrame::Dump(CDumpContext& dc) const
  285. {
  286. CExtNCW < CMDIFrameWnd > :: Dump( dc );
  287. }
  288. #endif //_DEBUG
  289. /////////////////////////////////////////////////////////////////////////////
  290. // CMainFrame message handlers
  291. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) 
  292. {
  293. if( m_wndToolBarUiLook.PreTranslateMessage( pMsg ) )
  294. return TRUE;
  295. if( m_wndMenuBar.TranslateMainFrameMessage( pMsg ) )
  296. return TRUE;
  297. return CExtNCW < CMDIFrameWnd > :: PreTranslateMessage( pMsg );
  298. }
  299. BOOL CMainFrame::DestroyWindow() 
  300. {
  301. HWND hWndMdiClient = ::GetDlgItem( m_hWnd, AFX_IDW_PANE_FIRST );
  302. if( hWndMdiClient != NULL )
  303. {
  304. HWND hWnd = ::GetWindow( hWndMdiClient, GW_CHILD );
  305. for( ; hWnd != NULL; hWnd = ::GetWindow( hWndMdiClient, GW_CHILD ) )
  306. {
  307. CWnd * pWnd = CWnd::FromHandlePermanent( hWnd );
  308. if( pWnd != NULL )
  309. pWnd->DestroyWindow();
  310. else
  311. ::DestroyWindow( hWnd );
  312. } // for( ; hWnd != NULL; hWnd = ::GetWindow( hWndMdiClient, GW_CHILD ) )
  313. } // if( hWndMdiClient != NULL )
  314. CWinApp * pApp = ::AfxGetApp();
  315. ASSERT( pApp != NULL );
  316. ASSERT( pApp->m_pszRegistryKey != NULL );
  317. ASSERT( pApp->m_pszRegistryKey[0] != _T('') );
  318. ASSERT( pApp->m_pszProfileName != NULL );
  319. ASSERT( pApp->m_pszProfileName[0] != _T('') );
  320. VERIFY(
  321. CExtControlBar::ProfileBarStateSave(
  322. this,
  323. pApp->m_pszRegistryKey,
  324. pApp->m_pszProfileName,
  325. pApp->m_pszProfileName
  326. )
  327. );
  328. VERIFY(
  329. g_CmdManager->SerializeState(
  330. pApp->m_pszProfileName,
  331. pApp->m_pszRegistryKey,
  332. pApp->m_pszProfileName,
  333. true
  334. )
  335. );
  336. VERIFY(
  337. g_PaintManager.PaintManagerStateSave(
  338. pApp->m_pszRegistryKey,
  339. pApp->m_pszProfileName,
  340. pApp->m_pszProfileName
  341. )
  342. );
  343. g_CmdManager->ProfileWndRemove( GetSafeHwnd() );
  344. return CExtNCW < CMDIFrameWnd > :: DestroyWindow();
  345. }
  346. void CMainFrame::ActivateFrame(int nCmdShow) 
  347. {
  348. // window placement persistence
  349. if( m_dataFrameWP.showCmd != SW_HIDE )
  350. {
  351. SetWindowPlacement( &m_dataFrameWP );
  352. CExtNCW < CMDIFrameWnd > :: ActivateFrame( m_dataFrameWP.showCmd );
  353. m_dataFrameWP.showCmd = SW_HIDE;
  354. return;
  355. }
  356. CExtNCW < CMDIFrameWnd > :: ActivateFrame( nCmdShow );
  357. }
  358. void CMainFrame::OnEditCopy() 
  359. {
  360. AfxMessageBox(
  361. _T("CMainFrame::OnEditCopy()")
  362. );
  363. }
  364. void CMainFrame::OnEditCut() 
  365. {
  366. AfxMessageBox(
  367. _T("CMainFrame::OnEditCut()")
  368. );
  369. }
  370. void CMainFrame::OnEditPaste() 
  371. {
  372. AfxMessageBox(
  373. _T("CMainFrame::OnEditPaste()")
  374. );
  375. }
  376. LRESULT CMainFrame::OnExtMenuPrepare(WPARAM wParam, LPARAM lParam)
  377. {
  378. wParam;
  379. lParam;
  380. //////////////////////////////////////////////////////////////////////////
  381. // Add "Windows..." command
  382. //////////////////////////////////////////////////////////////////////////
  383. CExtPopupMenuWnd::MsgPrepareMenuData_t * pData =
  384. reinterpret_cast
  385. < CExtPopupMenuWnd::MsgPrepareMenuData_t * >
  386. ( wParam );
  387. ASSERT( pData != NULL );
  388. CExtPopupMenuWnd * pPopup = pData->m_pPopup;
  389. ASSERT( pPopup != NULL );
  390. INT nItemPos;
  391. INT nNewPos = -1;
  392. nItemPos = pPopup->ItemFindPosForCmdID( __ID_MDIWND_DLGWINDOWS );
  393. if( nItemPos > 0 )
  394. {
  395. // "More Windows..." command found
  396. pPopup->ItemRemove( nItemPos );
  397. nNewPos = nItemPos;
  398. }
  399. else
  400. {
  401. int nMaxCmdID = 0;
  402. for( int nCmdID = __ID_MDIWNDLIST_FIRST; nCmdID <= __ID_MDIWNDLIST_LAST; nCmdID++ ){
  403. nItemPos = pPopup->ItemFindPosForCmdID( nCmdID );
  404. if( nItemPos > 0 ){
  405. if( nCmdID > nMaxCmdID ){
  406. nMaxCmdID = nCmdID;
  407. nNewPos = nItemPos;
  408. }
  409. }
  410. }
  411. if( nNewPos > 0 ){
  412. pPopup->ItemInsert(
  413. (UINT)CExtPopupMenuWnd::TYPE_SEPARATOR,
  414. ++nNewPos
  415. );
  416. nNewPos++;
  417. }
  418. }
  419. if( nNewPos > 0 )
  420. {
  421. UINT nCmdID = ID_WINDOWS_LIST;
  422. CExtCmdItem * pCmdItem =
  423. g_CmdManager->CmdGetPtr(
  424. g_CmdManager->ProfileNameFromWnd( this->GetSafeHwnd() ),
  425. nCmdID
  426. );
  427. if( pCmdItem == NULL ){
  428. pCmdItem = 
  429. g_CmdManager->CmdAllocPtr( 
  430. g_CmdManager->ProfileNameFromWnd( this->GetSafeHwnd() ), 
  431. nCmdID 
  432. );
  433. }
  434. ASSERT( pCmdItem != NULL );
  435. if( pCmdItem != NULL )
  436. {
  437. CExtSafeString sMoreWindows(_T("Windows..."));
  438. CExtSafeString sManageWindows(_T("Manages the currently open windows"));
  439. pCmdItem->m_sMenuText = sMoreWindows;
  440. pCmdItem->m_sToolbarText = pCmdItem->m_sMenuText;
  441. pCmdItem->m_sTipTool = sManageWindows;
  442. pCmdItem->m_sTipStatus = pCmdItem->m_sTipTool;
  443. pCmdItem->StateSetBasic( true );
  444. pPopup->ItemInsert(
  445. nCmdID,
  446. nNewPos,
  447. sMoreWindows,
  448. NULL,
  449. m_hWnd
  450. );
  451. }
  452. }
  453. return 1;
  454. }
  455. BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) 
  456. {
  457. if( nCode == CN_COMMAND )
  458. {
  459. if( nID == ID_WINDOWS_LIST )
  460. {
  461. CMyMdiWindowsListDlg dlgMdiWindowsList( this );
  462. dlgMdiWindowsList.DoModal();
  463. return TRUE;
  464. }
  465. }
  466. return CExtNCW < CMDIFrameWnd > :: OnCmdMsg( nID, nCode, pExtra, pHandlerInfo );
  467. }