CLoginWnd.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #pragma once
  2. #include "pch/pch.h"
  3. #include "CGameFrameWnd.h"
  4. #include "rapidjson/document.h"
  5. #include "rapidjson/prettywriter.h"
  6. #include "rapidjson/writer.h"
  7. #include "rapidjson/stringbuffer.h"
  8. using namespace rapidjson;
  9. class CLoginFrameWnd : public CWindowWnd, public INotifyUI, public IMessageFilterUI
  10. {
  11. public:
  12. CLoginFrameWnd()
  13. {
  14. }
  15. LPCTSTR GetWindowClassName() const { return _T("UILoginFrame"); };
  16. UINT GetClassStyle() const { return UI_CLASSSTYLE_DIALOG; };
  17. void OnFinalMessage(HWND /*hWnd*/)
  18. {
  19. m_pm.RemovePreMessageFilter(this);
  20. delete this;
  21. };
  22. void Init() {
  23. CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
  24. if (pAccountEdit) pAccountEdit->SetText(_T(""));
  25. pAccountEdit->SetFocus();
  26. }
  27. void Notify(TNotifyUI& msg)
  28. {
  29. if (msg.sType == _T("click"))
  30. {
  31. if (msg.pSender->GetName() == _T("closebtn"))
  32. {
  33. PostQuitMessage(0);
  34. return;
  35. }
  36. else if (msg.pSender->GetName() == _T("loginBtn"))
  37. {
  38. HandleLogin();
  39. return;
  40. }
  41. }
  42. }
  43. LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  44. {
  45. LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
  46. styleValue &= ~WS_CAPTION;
  47. ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
  48. // 把自己的窗口句柄与窗口绘制管理器挂接在一起
  49. m_pm.Init(m_hWnd);
  50. m_pm.AddPreMessageFilter(this);
  51. CDialogBuilder builder;
  52. CDialogBuilderCallbackEx cb;
  53. CControlUI* pRoot = builder.Create(_T("login.xml"), (UINT)0, &cb, &m_pm);
  54. ASSERT(pRoot && "Failed to parse XML");
  55. // 把这些控件绘制到本窗口上
  56. m_pm.AttachDialog(pRoot);
  57. // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
  58. m_pm.AddNotifier(this);
  59. Init();
  60. return 0;
  61. }
  62. LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  63. {
  64. if (::IsIconic(*this)) bHandled = FALSE;
  65. return (wParam == 0) ? TRUE : FALSE;
  66. }
  67. LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  68. {
  69. return 0;
  70. }
  71. LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  72. {
  73. return 0;
  74. }
  75. LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  76. {
  77. POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
  78. ::ScreenToClient(*this, &pt);
  79. RECT rcClient;
  80. ::GetClientRect(*this, &rcClient);
  81. RECT rcCaption = m_pm.GetCaptionRect();
  82. if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
  83. && pt.y >= rcCaption.top && pt.y < rcCaption.bottom) {
  84. CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(pt));
  85. if (pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0)
  86. return HTCAPTION;
  87. }
  88. return HTCLIENT;
  89. }
  90. LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  91. {
  92. SIZE szRoundCorner = m_pm.GetRoundCorner();
  93. if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) {
  94. CDuiRect rcWnd;
  95. ::GetWindowRect(*this, &rcWnd);
  96. rcWnd.Offset(-rcWnd.left, -rcWnd.top);
  97. rcWnd.right++; rcWnd.bottom++;
  98. HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
  99. ::SetWindowRgn(*this, hRgn, TRUE);
  100. ::DeleteObject(hRgn);
  101. }
  102. bHandled = FALSE;
  103. return 0;
  104. }
  105. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  106. {
  107. LRESULT lRes = 0;
  108. BOOL bHandled = TRUE;
  109. switch (uMsg) {
  110. case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
  111. case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
  112. case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
  113. case WM_NCPAINT: lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
  114. case WM_NCHITTEST: lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
  115. case WM_SIZE: lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
  116. default:
  117. bHandled = FALSE;
  118. }
  119. if (bHandled) return lRes;
  120. if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;
  121. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  122. }
  123. LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
  124. {
  125. if (uMsg == WM_KEYDOWN)
  126. {
  127. if (wParam == VK_RETURN)
  128. {
  129. CEditUI* pEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
  130. if (pEdit->GetText().IsEmpty())
  131. {
  132. pEdit->SetFocus();
  133. }
  134. else
  135. {
  136. pEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("pwdedit")));
  137. if (pEdit->GetText().IsEmpty())
  138. {
  139. pEdit->SetFocus();
  140. }
  141. else
  142. {
  143. this->HandleLogin();
  144. }
  145. }
  146. return true;
  147. }
  148. else if (wParam == VK_ESCAPE)
  149. {
  150. PostQuitMessage(0);
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. void HandleLogin();
  157. public:
  158. CPaintManagerUI m_pm;
  159. };