/3rd_party/wtl/AppWizCE/Files/Templates/1033/Frame.h

https://code.google.com/p/softart/ · C Header · 341 lines · 267 code · 37 blank · 37 comment · 24 complexity · 312731ed2a768975307175ddf7783836 MD5 · raw file

  1. // [!output WTL_FRAME_FILE].h : interface of the [!output WTL_FRAME_CLASS] class
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. #pragma once
  5. class [!output WTL_FRAME_CLASS] : public [!output WTL_FRAME_BASE_CLASS]<[!output WTL_FRAME_CLASS]>, public CUpdateUI<[!output WTL_FRAME_CLASS]>,
  6. public CMessageFilter, public CIdleHandler
  7. {
  8. public:
  9. DECLARE_FRAME_WND_CLASS(NULL, IDR_MAINFRAME)
  10. [!if WTL_APPTYPE_SDI || WTL_APPTYPE_MTSDI]
  11. [!if WTL_USE_VIEW]
  12. [!output WTL_VIEW_CLASS] m_view;
  13. [!endif]
  14. [!endif]
  15. [!if WTL_USE_CPP_FILES]
  16. virtual BOOL PreTranslateMessage(MSG* pMsg);
  17. [!else]
  18. virtual BOOL PreTranslateMessage(MSG* pMsg)
  19. {
  20. [!if WTL_USE_VIEW]
  21. if([!output WTL_FRAME_BASE_CLASS]<[!output WTL_FRAME_CLASS]>::PreTranslateMessage(pMsg))
  22. return TRUE;
  23. return m_view.PreTranslateMessage(pMsg);
  24. [!else]
  25. return [!output WTL_FRAME_BASE_CLASS]<[!output WTL_FRAME_CLASS]>::PreTranslateMessage(pMsg);
  26. [!endif]
  27. }
  28. [!endif]
  29. [!if WTL_USE_CPP_FILES]
  30. virtual BOOL OnIdle();
  31. [!else]
  32. virtual BOOL OnIdle()
  33. {
  34. [!if WTL_USE_TOOLBAR]
  35. UIUpdateToolBar();
  36. [!endif]
  37. return FALSE;
  38. }
  39. [!endif]
  40. BEGIN_UPDATE_UI_MAP([!output WTL_FRAME_CLASS])
  41. [!if WTL_USE_TOOLBAR && !WTL_USE_AYGSHELL]
  42. UPDATE_ELEMENT(ID_VIEW_TOOLBAR, UPDUI_MENUPOPUP)
  43. [!endif]
  44. [!if WTL_USE_STATUSBAR]
  45. UPDATE_ELEMENT(ID_VIEW_STATUS_BAR, UPDUI_MENUPOPUP)
  46. [!endif]
  47. END_UPDATE_UI_MAP()
  48. BEGIN_MSG_MAP([!output WTL_FRAME_CLASS])
  49. MESSAGE_HANDLER(WM_CREATE, OnCreate)
  50. [!if WTL_COM_SERVER]
  51. MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
  52. [!endif]
  53. [!if WTL_USE_SP03_COMPAT_MENUS]
  54. COMMAND_ID_HANDLER(ID_ACTION, OnAction)
  55. [!endif]
  56. COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
  57. COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
  58. [!if WTL_APPTYPE_MTSDI]
  59. COMMAND_ID_HANDLER(ID_FILE_NEW_WINDOW, OnFileNewWindow)
  60. [!endif]
  61. [!if WTL_USE_TOOLBAR && !WTL_USE_AYGSHELL]
  62. COMMAND_ID_HANDLER(ID_VIEW_TOOLBAR, OnViewToolBar)
  63. [!endif]
  64. [!if WTL_USE_STATUSBAR]
  65. COMMAND_ID_HANDLER(ID_VIEW_STATUS_BAR, OnViewStatusBar)
  66. [!endif]
  67. COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
  68. CHAIN_MSG_MAP(CUpdateUI<[!output WTL_FRAME_CLASS]>)
  69. CHAIN_MSG_MAP([!output WTL_FRAME_BASE_CLASS]<[!output WTL_FRAME_CLASS]>)
  70. END_MSG_MAP()
  71. // Handler prototypes (uncomment arguments if needed):
  72. // LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  73. // LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  74. // LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)
  75. [!if WTL_USE_CPP_FILES]
  76. LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
  77. [!else]
  78. LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  79. {
  80. [!if WTL_USE_AYGSHELL]
  81. [!if !WTL_USE_SP03_COMPAT_MENUS]
  82. CreateSimpleCEMenuBar(IDR_MAINFRAME);
  83. [!else]
  84. CreateSimpleCEMenuBar();
  85. [!endif]
  86. [!else]
  87. CreateSimpleCECommandBar(MAKEINTRESOURCE(IDR_MAINFRAME));
  88. [!endif]
  89. [!if WTL_USE_TOOLBAR]
  90. CreateSimpleToolBar();
  91. [!endif]
  92. [!if WTL_USE_STATUSBAR]
  93. CreateSimpleStatusBar();
  94. [!endif]
  95. [!if WTL_APPTYPE_SDI || WTL_APPTYPE_MTSDI]
  96. [!if WTL_USE_VIEW]
  97. [!if WTL_VIEWTYPE_FORM]
  98. m_hWndClient = m_view.Create(m_hWnd);
  99. [!else]
  100. [!if WTL_VIEWTYPE_HTML]
  101. //TODO: Replace with a URL of your choice
  102. m_hWndClient = m_view.Create(m_hWnd, rcDefault, _T("http://www.microsoft.com"), [!output WTL_VIEW_STYLES]);
  103. [!else]
  104. m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, [!output WTL_VIEW_STYLES]);
  105. [!endif]
  106. [!endif]
  107. [!endif]
  108. [!endif]
  109. [!if WTL_USE_TOOLBAR]
  110. UIAddToolBar(m_hWndToolBar);
  111. [!if !WTL_USE_AYGSHELL]
  112. UISetCheck(ID_VIEW_TOOLBAR, 1);
  113. [!endif]
  114. [!endif]
  115. [!if WTL_USE_STATUSBAR]
  116. UISetCheck(ID_VIEW_STATUS_BAR, 1);
  117. [!endif]
  118. // register object for message filtering and idle updates
  119. CMessageLoop* pLoop = _Module.GetMessageLoop();
  120. ATLASSERT(pLoop != NULL);
  121. pLoop->AddMessageFilter(this);
  122. pLoop->AddIdleHandler(this);
  123. return 0;
  124. }
  125. [!endif]
  126. [!if WTL_COM_SERVER]
  127. [!if WTL_USE_CPP_FILES]
  128. LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
  129. [!else]
  130. LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
  131. {
  132. // unregister message filtering and idle updates
  133. CMessageLoop* pLoop = _Module.GetMessageLoop();
  134. ATLASSERT(pLoop != NULL);
  135. pLoop->RemoveMessageFilter(this);
  136. pLoop->RemoveIdleHandler(this);
  137. // if UI is the last thread, no need to wait
  138. if(_Module.GetLockCount() == 1)
  139. {
  140. _Module.m_dwTimeOut = 0L;
  141. _Module.m_dwPause = 0L;
  142. }
  143. _Module.Unlock();
  144. [!if WTL_APPTYPE_MTSDI]
  145. ::PostQuitMessage(1);
  146. [!endif]
  147. return 0;
  148. }
  149. [!endif]
  150. [!endif]
  151. [!if WTL_USE_SP03_COMPAT_MENUS]
  152. [!if WTL_USE_CPP_FILES]
  153. LRESULT OnAction(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
  154. [!else]
  155. LRESULT OnAction(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  156. {
  157. // TODO: add code
  158. return 0;
  159. }
  160. [!endif]
  161. [!endif]
  162. [!if WTL_USE_CPP_FILES]
  163. LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
  164. [!else]
  165. LRESULT OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  166. {
  167. PostMessage(WM_CLOSE);
  168. return 0;
  169. }
  170. [!endif]
  171. [!if WTL_USE_CPP_FILES]
  172. LRESULT OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
  173. [!else]
  174. LRESULT OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  175. {
  176. // TODO: add code to initialize document
  177. return 0;
  178. }
  179. [!endif]
  180. [!if WTL_APPTYPE_MTSDI]
  181. [!if WTL_USE_CPP_FILES]
  182. LRESULT OnFileNewWindow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
  183. [!else]
  184. LRESULT OnFileNewWindow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  185. {
  186. ::PostThreadMessage(_Module.m_dwMainThreadID, WM_USER, 0, 0L);
  187. return 0;
  188. }
  189. [!endif]
  190. [!endif]
  191. [!if WTL_USE_TOOLBAR && !WTL_USE_AYGSHELL]
  192. [!if WTL_USE_CPP_FILES]
  193. LRESULT OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
  194. [!else]
  195. LRESULT OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  196. {
  197. BOOL bVisible = !::IsWindowVisible(m_hWndToolBar);
  198. ::ShowWindow(m_hWndToolBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
  199. UISetCheck(ID_VIEW_TOOLBAR, bVisible);
  200. UpdateLayout();
  201. return 0;
  202. }
  203. [!endif]
  204. [!endif]
  205. [!if WTL_USE_STATUSBAR]
  206. [!if WTL_USE_CPP_FILES]
  207. LRESULT OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
  208. [!else]
  209. LRESULT OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  210. {
  211. BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
  212. ::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
  213. UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
  214. UpdateLayout();
  215. return 0;
  216. }
  217. [!endif]
  218. [!endif]
  219. [!if WTL_USE_CPP_FILES]
  220. LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
  221. [!else]
  222. LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
  223. {
  224. CAboutDlg dlg;
  225. dlg.DoModal();
  226. return 0;
  227. }
  228. [!endif]
  229. [!if WTL_USE_SINGLE_APP_INSTANCE]
  230. [!if WTL_USE_CPP_FILES]
  231. static HRESULT ActivatePreviousInstance(HINSTANCE hInstance);
  232. [!else]
  233. static HRESULT ActivatePreviousInstance(HINSTANCE hInstance)
  234. {
  235. CFrameWndClassInfo& classInfo = [!output WTL_FRAME_CLASS]::GetWndClassInfo();
  236. ATLVERIFY(::LoadString(hInstance, IDR_MAINFRAME, classInfo.m_szAutoName, sizeof(classInfo.m_szAutoName)/sizeof(classInfo.m_szAutoName[0])));
  237. classInfo.m_wc.lpszClassName = classInfo.m_szAutoName;
  238. const TCHAR* pszClass = classInfo.m_wc.lpszClassName;
  239. if(pszClass == NULL || *pszClass == _T('\0'))
  240. {
  241. return E_FAIL;
  242. }
  243. // Orginally 500ms in SmartPhone 2003 App Wizard generated code
  244. // A lower value will result in a more responsive start-up of
  245. // the existing instance or termination of this instance.
  246. const DWORD dRetryInterval = 100;
  247. // Orginally 5 in SmartPhone 2003 App Wizard generated code
  248. // Multiplied by 5, since wait time was divided by 5.
  249. const int iMaxRetries = 25;
  250. for(int i = 0; i < iMaxRetries; ++i)
  251. {
  252. // Don't need ownership of the mutex
  253. HANDLE hMutex = CreateMutex(NULL, FALSE, pszClass);
  254. DWORD dw = GetLastError();
  255. if(hMutex == NULL)
  256. {
  257. // ERROR_INVALID_HANDLE - A non-mutex object with this name already exists.
  258. HRESULT hr = (dw == ERROR_INVALID_HANDLE) ? E_INVALIDARG : E_FAIL;
  259. return hr;
  260. }
  261. // If the mutex already exists, then there should be another instance running
  262. if(dw == ERROR_ALREADY_EXISTS)
  263. {
  264. // Just needed the error result, in this case, so close the handle.
  265. CloseHandle(hMutex);
  266. // Try to find the other instance, don't need to close HWND's.
  267. // Don't check title in case it is changed by app after init.
  268. HWND hwnd = FindWindow(pszClass, NULL);
  269. if(hwnd == NULL)
  270. {
  271. // It's possible that the other istance is in the process of starting up or shutting down.
  272. // So wait a bit and try again.
  273. Sleep(dRetryInterval);
  274. continue;
  275. }
  276. else
  277. {
  278. // Set the previous instance as the foreground window
  279. // The "| 0x1" in the code below activates the correct owned window
  280. // of the previous instance's main window according to the SmartPhone 2003
  281. // wizard generated code.
  282. if(SetForegroundWindow(reinterpret_cast<HWND>(reinterpret_cast<ULONG>(hwnd) | 0x1)) != 0)
  283. {
  284. // S_FALSE indicates that another instance was activated, so this instance should terminate.
  285. return S_FALSE;
  286. }
  287. }
  288. }
  289. else
  290. {
  291. // This is the first istance, so return S_OK.
  292. // Don't close the mutext handle here.
  293. // Do it on app shutdown instead.
  294. return S_OK;
  295. }
  296. }
  297. // The mutex was created by another instance, but it's window couldn't be brought
  298. // to the foreground, so ssume it's not a invalid instance (not this app, hung, etc.)
  299. // and let this one start.
  300. return S_OK;
  301. }
  302. [!endif]
  303. [!endif]
  304. };