CGameFrameWnd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. else if (name == _T("roomswitch")) {
  80. CTabLayoutUI* pControl = static_cast<CTabLayoutUI*>(m_pm.FindControl(_T("switch")));
  81. if (pControl && pControl->GetCurSel() != 1) {
  82. pControl->SelectItem(1);
  83. DeskListUI* pDeskList = static_cast<DeskListUI*>(m_pm.FindControl(_T("destlist")));
  84. pDeskList->SetFocus();
  85. CRichEditUI* pRichEdit = static_cast<CRichEditUI*>(m_pm.FindControl(_T("chatmsglist")));
  86. if (pRichEdit) {
  87. pRichEdit->SetText(_T("欢迎进入XXX游戏,祝游戏愉快!\n\n"));
  88. long lSelBegin = 0, lSelEnd = 0;
  89. CHARFORMAT2 cf;
  90. ZeroMemory(&cf, sizeof(CHARFORMAT2));
  91. cf.cbSize = sizeof(cf);
  92. cf.dwReserved = 0;
  93. cf.dwMask = CFM_COLOR;
  94. cf.crTextColor = RGB(255, 0, 0);
  95. lSelEnd = pRichEdit->GetTextLength();
  96. pRichEdit->SetSel(lSelBegin, lSelEnd);
  97. pRichEdit->SetSelectionCharFormat(cf);
  98. }
  99. }
  100. }
  101. }
  102. else if (msg.sType == _T("itemclick")) {
  103. GameListUI* pGameList = static_cast<GameListUI*>(m_pm.FindControl(_T("gamelist")));
  104. if (pGameList->GetItemIndex(msg.pSender) != -1)
  105. {
  106. if (_tcscmp(msg.pSender->GetClass(), DUI_CTR_LISTLABELELEMENT) == 0) {
  107. GameListUI::Node* node = (GameListUI::Node*)msg.pSender->GetTag();
  108. POINT pt = { 0 };
  109. ::GetCursorPos(&pt);
  110. ::ScreenToClient(m_pm.GetPaintWindow(), &pt);
  111. pt.x -= msg.pSender->GetX();
  112. SIZE sz = pGameList->GetExpanderSizeX(node);
  113. if (pt.x >= sz.cx && pt.x < sz.cy)
  114. pGameList->ExpandNode(node, !node->data()._expand);
  115. }
  116. }
  117. }
  118. else if (msg.sType == _T("itemactivate")) {
  119. GameListUI* pGameList = static_cast<GameListUI*>(m_pm.FindControl(_T("gamelist")));
  120. if (pGameList->GetItemIndex(msg.pSender) != -1)
  121. {
  122. if (_tcscmp(msg.pSender->GetClass(), DUI_CTR_LISTLABELELEMENT) == 0) {
  123. GameListUI::Node* node = (GameListUI::Node*)msg.pSender->GetTag();
  124. pGameList->ExpandNode(node, !node->data()._expand);
  125. if (node->data()._level == 3) {
  126. COptionUI* pControl = static_cast<COptionUI*>(m_pm.FindControl(_T("roomswitch")));
  127. if (pControl) {
  128. pControl->SetVisible(true);
  129. pControl->SetText(node->parent()->parent()->data()._text);
  130. pControl->Activate();
  131. }
  132. }
  133. }
  134. }
  135. }
  136. else if (msg.sType == _T("itemselect")) {
  137. if (msg.pSender->GetName() == _T("chatCombo")) {
  138. CEditUI* pChatEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("chatEdit")));
  139. if (pChatEdit) pChatEdit->SetText(msg.pSender->GetText());
  140. static_cast<CComboUI*>(msg.pSender)->SelectItem(-1);
  141. }
  142. }
  143. }
  144. LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  145. {
  146. LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  147. styleValue &= ~WS_CAPTION;
  148. ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  149. m_pm.Init(m_hWnd);
  150. CDialogBuilder builder;
  151. CDialogBuilderCallbackEx cb;
  152. CControlUI* pRoot = builder.Create(_T("main.xml"), (UINT)0, &cb, &m_pm);
  153. ASSERT(pRoot && "Failed to parse XML");
  154. m_pm.AttachDialog(pRoot);
  155. m_pm.AddNotifier(this);
  156. Init();
  157. return 0;
  158. }
  159. LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  160. {
  161. bHandled = FALSE;
  162. return 0;
  163. }
  164. LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  165. {
  166. ::PostQuitMessage(0L);
  167. bHandled = FALSE;
  168. return 0;
  169. }
  170. LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  171. {
  172. if (::IsIconic(*this)) bHandled = FALSE;
  173. return (wParam == 0) ? TRUE : FALSE;
  174. }
  175. LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  176. {
  177. return 0;
  178. }
  179. LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  180. {
  181. return 0;
  182. }
  183. LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  184. {
  185. POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
  186. ::ScreenToClient(*this, &pt);
  187. RECT rcClient;
  188. ::GetClientRect(*this, &rcClient);
  189. if (!::IsZoomed(*this)) {
  190. RECT rcSizeBox = m_pm.GetSizeBox();
  191. if (pt.y < rcClient.top + rcSizeBox.top) {
  192. if (pt.x < rcClient.left + rcSizeBox.left) return HTTOPLEFT;
  193. if (pt.x > rcClient.right - rcSizeBox.right) return HTTOPRIGHT;
  194. return HTTOP;
  195. }
  196. else if (pt.y > rcClient.bottom - rcSizeBox.bottom) {
  197. if (pt.x < rcClient.left + rcSizeBox.left) return HTBOTTOMLEFT;
  198. if (pt.x > rcClient.right - rcSizeBox.right) return HTBOTTOMRIGHT;
  199. return HTBOTTOM;
  200. }
  201. if (pt.x < rcClient.left + rcSizeBox.left) return HTLEFT;
  202. if (pt.x > rcClient.right - rcSizeBox.right) return HTRIGHT;
  203. }
  204. RECT rcCaption = m_pm.GetCaptionRect();
  205. if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
  206. && pt.y >= rcCaption.top && pt.y < rcCaption.bottom) {
  207. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
  208. if (pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0 &&
  209. _tcscmp(pControl->GetClass(), DUI_CTR_OPTION) != 0 &&
  210. _tcscmp(pControl->GetClass(), DUI_CTR_TEXT) != 0)
  211. return HTCAPTION;
  212. }
  213. return HTCLIENT;
  214. }
  215. LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  216. {
  217. SIZE szRoundCorner = m_pm.GetRoundCorner();
  218. if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) {
  219. CDuiRect rcWnd;
  220. ::GetWindowRect(*this, &rcWnd);
  221. rcWnd.Offset(-rcWnd.left, -rcWnd.top);
  222. rcWnd.right++; rcWnd.bottom++;
  223. HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
  224. ::SetWindowRgn(*this, hRgn, TRUE);
  225. ::DeleteObject(hRgn);
  226. }
  227. bHandled = FALSE;
  228. return 0;
  229. }
  230. LRESULT OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  231. {
  232. int primaryMonitorWidth = ::GetSystemMetrics(SM_CXSCREEN);
  233. int primaryMonitorHeight = ::GetSystemMetrics(SM_CYSCREEN);
  234. MONITORINFO oMonitor = {};
  235. oMonitor.cbSize = sizeof(oMonitor);
  236. ::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTOPRIMARY), &oMonitor);
  237. CDuiRect rcWork = oMonitor.rcWork;
  238. rcWork.Offset(-oMonitor.rcMonitor.left, -oMonitor.rcMonitor.top);
  239. if (rcWork.right > primaryMonitorWidth) rcWork.right = primaryMonitorWidth;
  240. if (rcWork.bottom > primaryMonitorHeight) rcWork.right = primaryMonitorHeight;
  241. LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
  242. lpMMI->ptMaxPosition.x = rcWork.left;
  243. lpMMI->ptMaxPosition.y = rcWork.top;
  244. lpMMI->ptMaxSize.x = rcWork.right;
  245. lpMMI->ptMaxSize.y = rcWork.bottom;
  246. bHandled = FALSE;
  247. return 0;
  248. }
  249. LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  250. {
  251. // 有时会在收到WM_NCDESTROY后收到wParam为SC_CLOSE的WM_SYSCOMMAND
  252. if (wParam == SC_CLOSE) {
  253. ::PostQuitMessage(0L);
  254. bHandled = TRUE;
  255. return 0;
  256. }
  257. BOOL bZoomed = ::IsZoomed(*this);
  258. LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  259. if (::IsZoomed(*this) != bZoomed) {
  260. if (!bZoomed) {
  261. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));
  262. if (pControl) pControl->SetVisible(false);
  263. pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));
  264. if (pControl) pControl->SetVisible(true);
  265. }
  266. else {
  267. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));
  268. if (pControl) pControl->SetVisible(true);
  269. pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));
  270. if (pControl) pControl->SetVisible(false);
  271. }
  272. }
  273. return lRes;
  274. }
  275. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  276. {
  277. LRESULT lRes = 0;
  278. BOOL bHandled = TRUE;
  279. switch (uMsg) {
  280. case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
  281. case WM_CLOSE: lRes = OnClose(uMsg, wParam, lParam, bHandled); break;
  282. case WM_DESTROY: lRes = OnDestroy(uMsg, wParam, lParam, bHandled); break;
  283. case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
  284. case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
  285. case WM_NCPAINT: lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
  286. case WM_NCHITTEST: lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
  287. case WM_SIZE: lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
  288. case WM_GETMINMAXINFO: lRes = OnGetMinMaxInfo(uMsg, wParam, lParam, bHandled); break;
  289. case WM_SYSCOMMAND: lRes = OnSysCommand(uMsg, wParam, lParam, bHandled); break;
  290. default:
  291. bHandled = FALSE;
  292. }
  293. if (bHandled) return lRes;
  294. if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;
  295. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  296. }
  297. public:
  298. CPaintManagerUI m_pm;
  299. private:
  300. CButtonUI* m_pCloseBtn;
  301. CButtonUI* m_pMaxBtn;
  302. CButtonUI* m_pRestoreBtn;
  303. CButtonUI* m_pMinBtn;
  304. //...
  305. };