#pragma once #include "pch/pch.h" #include "CGameFrameWnd.h" #include "rapidjson/document.h" #include "rapidjson/prettywriter.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" using namespace rapidjson; class CLoginFrameWnd : public CWindowWnd, public INotifyUI, public IMessageFilterUI { public: CLoginFrameWnd() { } LPCTSTR GetWindowClassName() const { return _T("UILoginFrame"); }; UINT GetClassStyle() const { return UI_CLASSSTYLE_DIALOG; }; void OnFinalMessage(HWND /*hWnd*/) { m_pm.RemovePreMessageFilter(this); delete this; }; void Init() { CEditUI* pAccountEdit = static_cast(m_pm.FindControl(_T("accountedit"))); if (pAccountEdit) pAccountEdit->SetText(_T("")); pAccountEdit->SetFocus(); } void Notify(TNotifyUI& msg) { if (msg.sType == _T("click")) { if (msg.pSender->GetName() == _T("closebtn")) { PostQuitMessage(0); return; } else if (msg.pSender->GetName() == _T("loginBtn")) { HandleLogin(); return; } } } LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { LONG styleValue = ::GetWindowLong(*this, GWL_STYLE); styleValue &= ~WS_CAPTION; ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN); // 把自己的窗口句柄与窗口绘制管理器挂接在一起 m_pm.Init(m_hWnd); m_pm.AddPreMessageFilter(this); CDialogBuilder builder; CDialogBuilderCallbackEx cb; CControlUI* pRoot = builder.Create(_T("login.xml"), (UINT)0, &cb, &m_pm); ASSERT(pRoot && "Failed to parse XML"); // 把这些控件绘制到本窗口上 m_pm.AttachDialog(pRoot); // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数 m_pm.AddNotifier(this); Init(); return 0; } LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if (::IsIconic(*this)) bHandled = FALSE; return (wParam == 0) ? TRUE : FALSE; } LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { return 0; } LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { return 0; } LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam); ::ScreenToClient(*this, &pt); RECT rcClient; ::GetClientRect(*this, &rcClient); RECT rcCaption = m_pm.GetCaptionRect(); if (pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \ && pt.y >= rcCaption.top && pt.y < rcCaption.bottom) { CControlUI* pControl = static_cast(m_pm.FindControl(pt)); if (pControl && _tcscmp(pControl->GetClass(), DUI_CTR_BUTTON) != 0) return HTCAPTION; } return HTCLIENT; } LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { SIZE szRoundCorner = m_pm.GetRoundCorner(); if (!::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0)) { CDuiRect rcWnd; ::GetWindowRect(*this, &rcWnd); rcWnd.Offset(-rcWnd.left, -rcWnd.top); rcWnd.right++; rcWnd.bottom++; HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy); ::SetWindowRgn(*this, hRgn, TRUE); ::DeleteObject(hRgn); } bHandled = FALSE; return 0; } LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) { LRESULT lRes = 0; BOOL bHandled = TRUE; switch (uMsg) { case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break; case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break; case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break; case WM_NCPAINT: lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break; case WM_NCHITTEST: lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break; case WM_SIZE: lRes = OnSize(uMsg, wParam, lParam, bHandled); break; default: bHandled = FALSE; } if (bHandled) return lRes; if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes; return CWindowWnd::HandleMessage(uMsg, wParam, lParam); } LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled) { if (uMsg == WM_KEYDOWN) { if (wParam == VK_RETURN) { CEditUI* pEdit = static_cast(m_pm.FindControl(_T("accountedit"))); if (pEdit->GetText().IsEmpty()) { pEdit->SetFocus(); } else { pEdit = static_cast(m_pm.FindControl(_T("pwdedit"))); if (pEdit->GetText().IsEmpty()) { pEdit->SetFocus(); } else { this->HandleLogin(); } } return true; } else if (wParam == VK_ESCAPE) { PostQuitMessage(0); return true; } } return false; } void HandleLogin(); public: CPaintManagerUI m_pm; };