CGameFrameWnd.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #pragma once
  2. #include "../pch/pch.h"
  3. class CGameFrameWnd : public CWindowWnd, public INotifyUI
  4. {
  5. public:
  6. CGameFrameWnd() { };
  7. LPCTSTR GetWindowClassName() const { return _T("UIMainFrame"); };
  8. UINT GetClassStyle() const { return CS_DBLCLKS; };
  9. void OnFinalMessage(HWND /*hWnd*/) { delete this; };
  10. void Init()
  11. {
  12. m_pCloseBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("closebtn")));
  13. m_pMaxBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("maxbtn")));
  14. m_pRestoreBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("restorebtn")));
  15. m_pMinBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("minbtn")));
  16. }
  17. void OnPrepare()
  18. {
  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. COptionUI* pControl = static_cast<COptionUI*>(m_pm.FindControl(_T("hallswitch")));
  26. if (pControl && pControl->IsSelected() == false) {
  27. CControlUI* pFadeControl = m_pm.FindControl(_T("fadeEffect"));
  28. if (pFadeControl) pFadeControl->SetVisible(true);
  29. }
  30. else {
  31. /*Close()*/PostQuitMessage(0); // 因为activex的原因,使用close可能会出现错误
  32. }
  33. return;
  34. }
  35. else if (msg.pSender == m_pMinBtn) { SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0); return; }
  36. else if (msg.pSender == m_pMaxBtn) { SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0); return; }
  37. else if (msg.pSender == m_pRestoreBtn) { SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0); return; }
  38. CDuiString name = msg.pSender->GetName();
  39. if (name == _T("quitbtn")) {
  40. /*Close()*/PostQuitMessage(0); // 因为activex的原因,使用close可能会出现错误
  41. }
  42. else if (name == _T("returnhallbtn")) {
  43. CControlUI* pFadeControl = m_pm.FindControl(_T("fadeEffect"));
  44. if (pFadeControl) pFadeControl->SetVisible(false);
  45. COptionUI* pControl = static_cast<COptionUI*>(m_pm.FindControl(_T("hallswitch")));
  46. pControl->Activate();
  47. pControl = static_cast<COptionUI*>(m_pm.FindControl(_T("roomswitch")));
  48. if (pControl) pControl->SetVisible(false);
  49. }
  50. else if (name == _T("fontswitch")) {
  51. TFontInfo* pFontInfo = m_pm.GetDefaultFontInfo();
  52. if (pFontInfo->iSize < 18) {
  53. TFontInfo* pFontInfo = m_pm.GetFontInfo(0);
  54. if (pFontInfo)m_pm.SetDefaultFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold,
  55. pFontInfo->bUnderline, pFontInfo->bItalic);
  56. }
  57. else {
  58. TFontInfo* pFontInfo = m_pm.GetFontInfo(1);
  59. if (pFontInfo)m_pm.SetDefaultFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold,
  60. pFontInfo->bUnderline, pFontInfo->bItalic);
  61. }
  62. m_pm.GetRoot()->NeedUpdate();
  63. }
  64. else if (name == _T("leaveBtn") || name == _T("roomclosebtn")) {
  65. COptionUI* pControl = static_cast<COptionUI*>(m_pm.FindControl(_T("hallswitch")));
  66. if (pControl) {
  67. pControl->Activate();
  68. pControl = static_cast<COptionUI*>(m_pm.FindControl(_T("roomswitch")));
  69. if (pControl) pControl->SetVisible(false);
  70. }
  71. }
  72. }
  73. else if (msg.sType == _T("selectchanged")) {
  74. CDuiString name = msg.pSender->GetName();
  75. if (name == _T("hallswitch")) {
  76. CTabLayoutUI* pControl = static_cast<CTabLayoutUI*>(m_pm.FindControl(_T("switch")));
  77. if (pControl && pControl->GetCurSel() != 0) pControl->SelectItem(0);
  78. }
  79. }
  80. else if (msg.sType == _T("itemclick")) {
  81. }
  82. else if (msg.sType == _T("itemactivate")) {
  83. }
  84. else if (msg.sType == _T("itemselect")) {
  85. if (msg.pSender->GetName() == _T("chatCombo")) {
  86. CEditUI* pChatEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("chatEdit")));
  87. if (pChatEdit) pChatEdit->SetText(msg.pSender->GetText());
  88. static_cast<CComboUI*>(msg.pSender)->SelectItem(-1);
  89. }
  90. }
  91. }
  92. LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  93. {
  94. LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  95. styleValue &= ~WS_CAPTION;
  96. ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  97. m_pm.Init(m_hWnd);
  98. CDialogBuilder builder;
  99. CControlUI* pRoot = builder.Create(_T("main.xml"), (UINT)0, NULL, &m_pm);
  100. ASSERT(pRoot && "Failed to parse XML");
  101. m_pm.AttachDialog(pRoot);
  102. m_pm.AddNotifier(this);
  103. Init();
  104. return 0;
  105. }
  106. LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  107. {
  108. bHandled = FALSE;
  109. return 0;
  110. }
  111. LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  112. {
  113. ::PostQuitMessage(0L);
  114. bHandled = FALSE;
  115. return 0;
  116. }
  117. LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  118. {
  119. if (::IsIconic(*this)) bHandled = FALSE;
  120. return (wParam == 0) ? TRUE : FALSE;
  121. }
  122. LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  123. {
  124. return 0;
  125. }
  126. LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  127. {
  128. return 0;
  129. }
  130. LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  131. {
  132. POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
  133. ::ScreenToClient(*this, &pt);
  134. RECT rcClient;
  135. ::GetClientRect(*this, &rcClient);
  136. if (!::IsZoomed(*this)) {
  137. RECT rcSizeBox = m_pm.GetSizeBox();
  138. if (pt.y < rcClient.top + rcSizeBox.top) {
  139. if (pt.x < rcClient.left + rcSizeBox.left) return HTTOPLEFT;
  140. if (pt.x > rcClient.right - rcSizeBox.right) return HTTOPRIGHT;
  141. return HTTOP;
  142. }
  143. else if (pt.y > rcClient.bottom - rcSizeBox.bottom) {
  144. if (pt.x < rcClient.left + rcSizeBox.left) return HTBOTTOMLEFT;
  145. if (pt.x > rcClient.right - rcSizeBox.right) return HTBOTTOMRIGHT;
  146. return HTBOTTOM;
  147. }
  148. if (pt.x < rcClient.left + rcSizeBox.left) return HTLEFT;
  149. if (pt.x > rcClient.right - rcSizeBox.right) return HTRIGHT;
  150. }
  151. RECT rcCaption = m_pm.GetCaptionRect();
  152. if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
  153. && pt.y >= rcCaption.top && pt.y < rcCaption.bottom) {
  154. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
  155. if (pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0 &&
  156. _tcscmp(pControl->GetClass(), DUI_CTR_OPTION) != 0 &&
  157. _tcscmp(pControl->GetClass(), DUI_CTR_TEXT) != 0)
  158. return HTCAPTION;
  159. }
  160. return HTCLIENT;
  161. }
  162. LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  163. {
  164. SIZE szRoundCorner = m_pm.GetRoundCorner();
  165. if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) {
  166. CDuiRect rcWnd;
  167. ::GetWindowRect(*this, &rcWnd);
  168. rcWnd.Offset(-rcWnd.left, -rcWnd.top);
  169. rcWnd.right++; rcWnd.bottom++;
  170. HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
  171. ::SetWindowRgn(*this, hRgn, TRUE);
  172. ::DeleteObject(hRgn);
  173. }
  174. bHandled = FALSE;
  175. return 0;
  176. }
  177. LRESULT OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  178. {
  179. int primaryMonitorWidth = ::GetSystemMetrics(SM_CXSCREEN);
  180. int primaryMonitorHeight = ::GetSystemMetrics(SM_CYSCREEN);
  181. MONITORINFO oMonitor = {};
  182. oMonitor.cbSize = sizeof(oMonitor);
  183. ::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTOPRIMARY), &oMonitor);
  184. CDuiRect rcWork = oMonitor.rcWork;
  185. rcWork.Offset(-oMonitor.rcMonitor.left, -oMonitor.rcMonitor.top);
  186. if (rcWork.right > primaryMonitorWidth) rcWork.right = primaryMonitorWidth;
  187. if (rcWork.bottom > primaryMonitorHeight) rcWork.right = primaryMonitorHeight;
  188. LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
  189. lpMMI->ptMaxPosition.x = rcWork.left;
  190. lpMMI->ptMaxPosition.y = rcWork.top;
  191. lpMMI->ptMaxSize.x = rcWork.right;
  192. lpMMI->ptMaxSize.y = rcWork.bottom;
  193. bHandled = FALSE;
  194. return 0;
  195. }
  196. LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  197. {
  198. // 有时会在收到WM_NCDESTROY后收到wParam为SC_CLOSE的WM_SYSCOMMAND
  199. if (wParam == SC_CLOSE) {
  200. ::PostQuitMessage(0L);
  201. bHandled = TRUE;
  202. return 0;
  203. }
  204. BOOL bZoomed = ::IsZoomed(*this);
  205. LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  206. if (::IsZoomed(*this) != bZoomed) {
  207. if (!bZoomed) {
  208. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));
  209. if (pControl) pControl->SetVisible(false);
  210. pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));
  211. if (pControl) pControl->SetVisible(true);
  212. }
  213. else {
  214. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));
  215. if (pControl) pControl->SetVisible(true);
  216. pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));
  217. if (pControl) pControl->SetVisible(false);
  218. }
  219. }
  220. return lRes;
  221. }
  222. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  223. {
  224. LRESULT lRes = 0;
  225. BOOL bHandled = TRUE;
  226. switch (uMsg) {
  227. case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
  228. case WM_CLOSE: lRes = OnClose(uMsg, wParam, lParam, bHandled); break;
  229. case WM_DESTROY: lRes = OnDestroy(uMsg, wParam, lParam, bHandled); break;
  230. case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
  231. case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
  232. case WM_NCPAINT: lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
  233. case WM_NCHITTEST: lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
  234. case WM_SIZE: lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
  235. case WM_GETMINMAXINFO: lRes = OnGetMinMaxInfo(uMsg, wParam, lParam, bHandled); break;
  236. case WM_SYSCOMMAND: lRes = OnSysCommand(uMsg, wParam, lParam, bHandled); break;
  237. default:
  238. bHandled = FALSE;
  239. }
  240. if (bHandled) return lRes;
  241. if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;
  242. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  243. }
  244. public:
  245. CPaintManagerUI m_pm;
  246. private:
  247. CButtonUI* m_pCloseBtn;
  248. CButtonUI* m_pMaxBtn;
  249. CButtonUI* m_pRestoreBtn;
  250. CButtonUI* m_pMinBtn;
  251. //...
  252. };