lewaimai_dispatch_windows.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "pch/pch.h"
  2. #include <exdisp.h>
  3. #include <comdef.h>
  4. #include "ControlEx.h"
  5. class C360SafeFrameWnd : public CWindowWnd, public INotifyUI
  6. {
  7. public:
  8. C360SafeFrameWnd() { };
  9. LPCTSTR GetWindowClassName() const { return _T("UIMainFrame"); };
  10. UINT GetClassStyle() const { return CS_DBLCLKS; };
  11. void OnFinalMessage(HWND /*hWnd*/) { delete this; };
  12. void Init() {
  13. m_pCloseBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("closebtn")));
  14. m_pMaxBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("maxbtn")));
  15. m_pRestoreBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("restorebtn")));
  16. m_pMinBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("minbtn")));
  17. }
  18. void OnPrepare() {
  19. }
  20. void Notify(TNotifyUI& msg)
  21. {
  22. if (msg.sType == _T("windowinit")) OnPrepare();
  23. else if (msg.sType == _T("click")) {
  24. if (msg.pSender == m_pCloseBtn) {
  25. PostQuitMessage(0);
  26. return;
  27. }
  28. else if (msg.pSender == m_pMinBtn) {
  29. SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0); return;
  30. }
  31. else if (msg.pSender == m_pMaxBtn) {
  32. SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0); return;
  33. }
  34. else if (msg.pSender == m_pRestoreBtn) {
  35. SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0); return;
  36. }
  37. }
  38. else if (msg.sType == _T("selectchanged"))
  39. {
  40. CDuiString name = msg.pSender->GetName();
  41. CTabLayoutUI* pControl = static_cast<CTabLayoutUI*>(m_pm.FindControl(_T("switch")));
  42. if (name == _T("examine"))
  43. pControl->SelectItem(0);
  44. else if (name == _T("trojan"))
  45. pControl->SelectItem(1);
  46. else if (name == _T("plugins"))
  47. pControl->SelectItem(2);
  48. else if (name == _T("vulnerability"))
  49. pControl->SelectItem(3);
  50. else if (name == _T("rubbish"))
  51. pControl->SelectItem(4);
  52. else if (name == _T("cleanup"))
  53. pControl->SelectItem(5);
  54. else if (name == _T("fix"))
  55. pControl->SelectItem(6);
  56. else if (name == _T("tool"))
  57. pControl->SelectItem(7);
  58. }
  59. }
  60. LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  61. {
  62. LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  63. styleValue &= ~WS_CAPTION;
  64. ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  65. m_pm.Init(m_hWnd);
  66. CDialogBuilder builder;
  67. CDialogBuilderCallbackEx cb;
  68. CControlUI* pRoot = builder.Create(_T("skin.xml"), (UINT)0, &cb, &m_pm);
  69. ASSERT(pRoot && "Failed to parse XML");
  70. m_pm.AttachDialog(pRoot);
  71. m_pm.AddNotifier(this);
  72. Init();
  73. return 0;
  74. }
  75. LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  76. {
  77. bHandled = FALSE;
  78. return 0;
  79. }
  80. LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  81. {
  82. ::PostQuitMessage(0L);
  83. bHandled = FALSE;
  84. return 0;
  85. }
  86. LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  87. {
  88. if (::IsIconic(*this)) bHandled = FALSE;
  89. return (wParam == 0) ? TRUE : FALSE;
  90. }
  91. LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  92. {
  93. return 0;
  94. }
  95. LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  96. {
  97. return 0;
  98. }
  99. LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  100. {
  101. POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
  102. ::ScreenToClient(*this, &pt);
  103. RECT rcClient;
  104. ::GetClientRect(*this, &rcClient);
  105. // if( !::IsZoomed(*this) ) {
  106. // RECT rcSizeBox = m_pm.GetSizeBox();
  107. // if( pt.y < rcClient.top + rcSizeBox.top ) {
  108. // if( pt.x < rcClient.left + rcSizeBox.left ) return HTTOPLEFT;
  109. // if( pt.x > rcClient.right - rcSizeBox.right ) return HTTOPRIGHT;
  110. // return HTTOP;
  111. // }
  112. // else if( pt.y > rcClient.bottom - rcSizeBox.bottom ) {
  113. // if( pt.x < rcClient.left + rcSizeBox.left ) return HTBOTTOMLEFT;
  114. // if( pt.x > rcClient.right - rcSizeBox.right ) return HTBOTTOMRIGHT;
  115. // return HTBOTTOM;
  116. // }
  117. // if( pt.x < rcClient.left + rcSizeBox.left ) return HTLEFT;
  118. // if( pt.x > rcClient.right - rcSizeBox.right ) return HTRIGHT;
  119. // }
  120. RECT rcCaption = m_pm.GetCaptionRect();
  121. if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
  122. && pt.y >= rcCaption.top && pt.y < rcCaption.bottom) {
  123. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
  124. if (pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0 &&
  125. _tcscmp(pControl->GetClass(), DUI_CTR_OPTION) != 0 &&
  126. _tcscmp(pControl->GetClass(), DUI_CTR_TEXT) != 0)
  127. return HTCAPTION;
  128. }
  129. return HTCLIENT;
  130. }
  131. LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  132. {
  133. SIZE szRoundCorner = m_pm.GetRoundCorner();
  134. if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) {
  135. CDuiRect rcWnd;
  136. ::GetWindowRect(*this, &rcWnd);
  137. rcWnd.Offset(-rcWnd.left, -rcWnd.top);
  138. rcWnd.right++; rcWnd.bottom++;
  139. HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
  140. ::SetWindowRgn(*this, hRgn, TRUE);
  141. ::DeleteObject(hRgn);
  142. }
  143. bHandled = FALSE;
  144. return 0;
  145. }
  146. LRESULT OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  147. {
  148. MONITORINFO oMonitor = {};
  149. oMonitor.cbSize = sizeof(oMonitor);
  150. ::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTOPRIMARY), &oMonitor);
  151. CDuiRect rcWork = oMonitor.rcWork;
  152. rcWork.Offset(-oMonitor.rcMonitor.left, -oMonitor.rcMonitor.top);
  153. LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
  154. lpMMI->ptMaxPosition.x = rcWork.left;
  155. lpMMI->ptMaxPosition.y = rcWork.top;
  156. lpMMI->ptMaxSize.x = rcWork.right;
  157. lpMMI->ptMaxSize.y = rcWork.bottom;
  158. bHandled = FALSE;
  159. return 0;
  160. }
  161. LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  162. {
  163. // 有时会在收到WM_NCDESTROY后收到wParam为SC_CLOSE的WM_SYSCOMMAND
  164. if (wParam == SC_CLOSE) {
  165. ::PostQuitMessage(0L);
  166. bHandled = TRUE;
  167. return 0;
  168. }
  169. BOOL bZoomed = ::IsZoomed(*this);
  170. LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  171. if (::IsZoomed(*this) != bZoomed) {
  172. if (!bZoomed) {
  173. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));
  174. if (pControl) pControl->SetVisible(false);
  175. pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));
  176. if (pControl) pControl->SetVisible(true);
  177. }
  178. else {
  179. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));
  180. if (pControl) pControl->SetVisible(true);
  181. pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));
  182. if (pControl) pControl->SetVisible(false);
  183. }
  184. }
  185. return lRes;
  186. }
  187. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  188. {
  189. LRESULT lRes = 0;
  190. BOOL bHandled = TRUE;
  191. switch (uMsg) {
  192. case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
  193. case WM_CLOSE: lRes = OnClose(uMsg, wParam, lParam, bHandled); break;
  194. case WM_DESTROY: lRes = OnDestroy(uMsg, wParam, lParam, bHandled); break;
  195. case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
  196. case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
  197. case WM_NCPAINT: lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
  198. case WM_NCHITTEST: lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
  199. case WM_SIZE: lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
  200. case WM_GETMINMAXINFO: lRes = OnGetMinMaxInfo(uMsg, wParam, lParam, bHandled); break;
  201. case WM_SYSCOMMAND: lRes = OnSysCommand(uMsg, wParam, lParam, bHandled); break;
  202. default:
  203. bHandled = FALSE;
  204. }
  205. if (bHandled) return lRes;
  206. if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;
  207. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  208. }
  209. public:
  210. CPaintManagerUI m_pm;
  211. private:
  212. CButtonUI* m_pCloseBtn;
  213. CButtonUI* m_pMaxBtn;
  214. CButtonUI* m_pRestoreBtn;
  215. CButtonUI* m_pMinBtn;
  216. //...
  217. };
  218. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
  219. {
  220. //初始化日志
  221. //CLewaimaiLog log;
  222. //log.Init();
  223. //读取配置文件
  224. //CConfigReader::ReadConfigFile();
  225. CPaintManagerUI::SetInstance(hInstance);
  226. CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + _T("skin"));
  227. CPaintManagerUI::SetResourceZip(_T("360SafeRes.zip"));
  228. HRESULT Hr = ::CoInitialize(NULL);
  229. if (FAILED(Hr)) return 0;
  230. C360SafeFrameWnd* pFrame = new C360SafeFrameWnd();
  231. if (pFrame == NULL) return 0;
  232. pFrame->Create(NULL, _T("360安全卫士"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 800, 572);
  233. pFrame->CenterWindow();
  234. ::ShowWindow(*pFrame, SW_SHOW);
  235. CPaintManagerUI::MessageLoop();
  236. ::CoUninitialize();
  237. return 0;
  238. }