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

对话框与窗口

开发平台:

Visual C++

  1. // Styler.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "Styler.h"
  5. #include "MainFrm.h"
  6. #include "ChildFrm.h"
  7. #include "StylerDoc.h"
  8. #include "StylerView.h"
  9. #include "FilterDialog.h"
  10. #include "PopupFilter.h"
  11. #include "CWebBrowser2.h"
  12. #include "MouseManager.h"
  13. #include "PageSearch.h"
  14. //#include "CustSite.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #endif
  18. // CStylerApp
  19. BEGIN_MESSAGE_MAP(CStylerApp, CWinApp)
  20. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  21. // Standard file based document commands
  22. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  23. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  24. // Standard print setup command
  25. ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  26. END_MESSAGE_MAP()
  27. // CStylerApp construction
  28. CStylerApp::CStylerApp()
  29. {
  30. // TODO: add construction code here,
  31. // Place all significant initialization in InitInstance
  32. }
  33. // The one and only CStylerApp object
  34. CStylerApp theApp;
  35. // CStylerApp initialization
  36. BOOL CStylerApp::InitInstance()
  37. {
  38. // InitCommonControls() is required on Windows XP if an application
  39. // manifest specifies use of ComCtl32.dll version 6 or later to enable
  40. // visual xtps.  Otherwise, any window creation will fail.
  41. InitCommonControls();
  42. CWinApp::InitInstance();
  43. // Initialize OLE libraries
  44. if (!AfxOleInit())
  45. {
  46. AfxMessageBox(IDP_OLE_INIT_FAILED);
  47. return FALSE;
  48. }
  49. AfxEnableControlContainer();
  50. CXTPWinDwmWrapper().SetProcessDPIAware();
  51. // Standard initialization
  52. // If you are not using these features and wish to reduce the size
  53. // of your final executable, you should remove from the following
  54. // the specific initialization routines you do not need
  55. // Change the registry key under which our settings are stored
  56. // TODO: You should modify this string to be something appropriate
  57. // such as the name of your company or organization
  58. SetRegistryKey(_T("Styler"));
  59. LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)
  60. CFilterDialog::LoadFilterList();
  61. CPopupFilter::LoadFilterList();
  62. CStylerView::Load();
  63. CMainFrame::Load();
  64. CPageSearch::Load();
  65. // Register the application's document templates.  Document templates
  66. //  serve as the connection between documents, frame windows and views
  67. CMultiDocTemplate* pDocTemplate;
  68. pDocTemplate = new CMultiDocTemplate(IDR_MAINFRAME,
  69. RUNTIME_CLASS(CStylerDoc),
  70. RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  71. RUNTIME_CLASS(CStylerView));
  72. AddDocTemplate(pDocTemplate);
  73. // call DragAcceptFiles only if there's a suffix
  74. //  In an MDI app, this should occur immediately after setting m_pMainWnd
  75. // Parse command line for standard shell commands, DDE, file open
  76. CCommandLineInfo cmdInfo;
  77. ParseCommandLine(cmdInfo);
  78. if (!OneInstance(cmdInfo))
  79. return FALSE;
  80. // create main MDI Frame window
  81. CMainFrame* pMainFrame = new CMainFrame;
  82. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  83. return FALSE;
  84. m_pMainWnd = pMainFrame;
  85. if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew &&
  86. (CMainFrame::m_nStartup == STARTUP_NOTHING || CMainFrame::m_nStartup == STARTUP_LASTVISITED))
  87. cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
  88. // Dispatch commands specified on the command line.  Will return FALSE if
  89. // app was launched with /RegServer, /Register, /Unregserver or /Unregister.
  90. if (!ProcessShellCommand(cmdInfo))
  91. return FALSE;
  92. if (!CMouseManager::Load())
  93. {
  94. ACCEL accel = {FCONTROL, 0, ID_FILE_CLOSE};
  95. CMouseManager::AddCommand(WM_LBUTTONDOWN, accel, MOUSE_AREA_TAB);
  96. }
  97. if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNothing && CMainFrame::m_nStartup == STARTUP_LASTVISITED)
  98. ((CMainFrame*)pMainFrame)->OpenLastVisited();
  99. // The main window has been initialized, so show and update it
  100. //pMainFrame->ShowWindow(m_nCmdShow/*, CMainFrame::m_bRestoreWindowPos*/);
  101. CWindowPlacement(pMainFrame, _T("Main Frame")).ShowWindow(m_nCmdShow, CMainFrame::m_bRestoreWindowPos);
  102. pMainFrame->UpdateWindow();
  103. m_pMainWnd->DragAcceptFiles();
  104. return TRUE;
  105. }
  106. BOOL CStylerApp::ProcessShellCommand(CCommandLineInfo& cmdInfo)
  107. {
  108. if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileOpen)
  109. {
  110. CString strURL = cmdInfo.m_strFileName;
  111. if(strURL.Right(4).CompareNoCase(_T(".url")) == 0)
  112. {
  113. TCHAR buf[2048];
  114. ::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"),
  115. _T(""), buf, 2048, strURL);
  116. OpenWindow(buf);
  117. return TRUE;
  118. }
  119. if (strURL.Find(_T("://")) != -1)
  120. {
  121. OpenWindow(strURL);
  122. return TRUE;
  123. }
  124. return CWinApp::ProcessShellCommand(cmdInfo);
  125. }
  126. BOOL CStylerApp::ExitInstance()
  127. {
  128. CStylerView::Save();
  129. CMainFrame::Save();
  130. CMouseManager::Save();
  131. CPageSearch::Save();
  132. return CWinApp::ExitInstance();
  133. }
  134. BOOL CStylerApp::OneInstance(CCommandLineInfo& cmdInfo)
  135. {
  136. HANDLE hSem = CreateSemaphore(NULL, 1, 1, m_pszAppName);
  137. if (GetLastError() == ERROR_ALREADY_EXISTS && CMainFrame::m_bOneInstance)
  138. {
  139. CloseHandle(hSem);
  140. HWND hWndPrevious = ::FindWindow(_T("StylerMainWindow"), NULL);
  141. if (hWndPrevious)
  142. {
  143. if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileOpen )
  144. {
  145. //if (::GetLastActivePopup(hWndPrevious) == hWndPrevious)
  146. {
  147. LPTSTR lpData;
  148. //mutex
  149. HANDLE hMutex = CreateMutex(NULL, TRUE, _T("StylerMutex"));
  150. WaitForSingleObject(hMutex, INFINITE);
  151. HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
  152. PAGE_READWRITE, 0, 2560, _T("STYLERSHARE"));
  153. if (hMapping == NULL)
  154. {
  155. TRACE(_T("App:Fail to create share memory!"));
  156. }
  157. else
  158. {
  159. lpData = (LPTSTR) MapViewOfFile(hMapping,FILE_MAP_ALL_ACCESS,0,0,0);
  160. if (lpData == NULL)
  161. {
  162. TRACE(_T("App:MapViewOfFile Fail"));
  163. }
  164. else 
  165. {
  166. #if (_MSC_VER > 1310) // VS2005
  167. _stprintf_s(lpData, 2560, _T("%s"), cmdInfo.m_strFileName);
  168. #else
  169. _stprintf(lpData, _T("%s"), cmdInfo.m_strFileName);
  170. #endif
  171. ::SendMessage(hWndPrevious,WM_USER_SHELL_OPEN,NULL,NULL);
  172. UnmapViewOfFile(lpData);
  173. }
  174. CloseHandle(hMapping);
  175. }
  176. ReleaseMutex(hMutex);
  177. }
  178. if (::IsIconic(hWndPrevious))
  179. ::ShowWindow(hWndPrevious,SW_RESTORE);
  180. ::SetForegroundWindow(::GetLastActivePopup(hWndPrevious));
  181. return FALSE;
  182. }
  183. return TRUE;
  184. }
  185. // App command to run the dialog
  186. void CStylerApp::OnAppAbout()
  187. {
  188. CAboutDlg aboutDlg;
  189. aboutDlg.DoModal();
  190. }
  191. // CStylerApp message handlers
  192. CString GetModuleDir()
  193. {
  194. TCHAR lpszModule[MAX_PATH];
  195. if (!GetModuleFileName(AfxGetResourceHandle(), lpszModule, MAX_PATH))
  196. return _T("");
  197. CString strPath(lpszModule);
  198. int nIndex = strPath.ReverseFind('\');
  199. if (nIndex != -1 && strPath.GetLength() != 3)
  200. return strPath.Left(nIndex + 1) + _T("Config\");
  201. return _T("Config\");
  202. }