#include "../pch/pch.h" #include "CLoginWnd.h" void CLoginWnd::Init() { std::map users = CSetting::getUsers(); CComboUI* pCom = static_cast(m_pm.FindControl(_T("accountcombo"))); for (std::map::iterator it = users.begin(); it != users.end(); it++) { std::string username = it->first; CListLabelElementUI* elem = new CListLabelElementUI(); elem->SetText(CLewaimaiString::UTF8ToUnicode(username).c_str()); pCom->Add(elem); } CCheckBoxUI* pAuto = static_cast(m_pm.FindControl(_T("login_auto_login"))); CCheckBoxUI* pRemember = static_cast(m_pm.FindControl(_T("login_remember_password"))); if (CSetting::GetParam("setting_is_remember_password") == "1") { pRemember->Selected(true, false); } else { pRemember->Selected(false, false); } if (CSetting::GetParam("setting_is_auto_login") == "1") { //自动登录开启了,记住密码一定要开启 pAuto->Selected(true, false); pRemember->Selected(true, false); } else { pAuto->Selected(false, false); } std::string last_login_username = CSetting::GetParam("last_login_username"); std::string password = CSetting::GetUser(last_login_username); CEditUI* pAccountEdit = static_cast(m_pm.FindControl(_T("accountedit"))); CEditUI* pPasswordEdit = static_cast(m_pm.FindControl(_T("pwdedit"))); pAccountEdit->SetText(CLewaimaiString::UTF8ToUnicode(last_login_username).c_str()); pPasswordEdit->SetText(CLewaimaiString::UTF8ToUnicode(password).c_str()); if (CSetting::GetParam("setting_is_auto_login") == "1") { HandleLogin(); } } void CLoginWnd::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; } else if (msg.pSender->GetName() == _T("login_auto_login")) { CCheckBoxUI* pAuto = static_cast(m_pm.FindControl(_T("login_auto_login"))); CCheckBoxUI* pRemember = static_cast(m_pm.FindControl(_T("login_remember_password"))); if (!pAuto->IsSelected()) { pRemember->Selected(true, false); } } else if (msg.pSender->GetName() == _T("login_remember_password")) { CCheckBoxUI* pAuto = static_cast(m_pm.FindControl(_T("login_auto_login"))); CCheckBoxUI* pRemember = static_cast(m_pm.FindControl(_T("login_remember_password"))); if (pRemember->IsSelected()) { pAuto->Selected(false, false); } } } else if (msg.sType == _T("itemselect")) { CDuiString name = msg.pSender->GetName(); if (name == _T("accountcombo")) { CComboUI* pCom = static_cast(m_pm.FindControl(_T("accountcombo"))); std::wstring name = pCom->GetItemAt(pCom->GetCurSel())->GetText(); std::string password = CSetting::GetUser(CLewaimaiString::UnicodeToUTF8(name)); std::wstring wspassword = CLewaimaiString::UTF8ToUnicode(password); CEditUI* pEdit = static_cast(m_pm.FindControl(_T("accountedit"))); pEdit->SetText(name.c_str()); CEditUI* pPassword = static_cast(m_pm.FindControl(_T("pwdedit"))); pPassword->SetText(wspassword.c_str()); } } } LRESULT CLoginWnd::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; CControlUI* pRoot = builder.Create(_T("login.xml"), (UINT)0, NULL, &m_pm); ASSERT(pRoot && "Failed to parse XML"); // 把这些控件绘制到本窗口上 m_pm.AttachDialog(pRoot); // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数 m_pm.AddNotifier(this); Init(); return 0; } LRESULT CLoginWnd::OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if(::IsIconic(*this)) { bHandled = FALSE; } return (wParam == 0) ? TRUE : FALSE; } LRESULT CLoginWnd::OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { return 0; } LRESULT CLoginWnd::OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { //在这里设置焦点才有用 CEditUI* pAccountEdit = static_cast(m_pm.FindControl(_T("accountedit"))); if (pAccountEdit) { pAccountEdit->SetFocus(); } return 0; } LRESULT CLoginWnd::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 CLoginWnd::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 CLoginWnd::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 CLoginWnd::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 CLoginWnd::HandleLogin() { //判断账号密码是否正确 std::wstring account, password; CEditUI* pAccountEdit = static_cast(m_pm.FindControl(_T("accountedit"))); if(pAccountEdit) { account = pAccountEdit->GetText().GetData(); } CEditUI* pPasswordEdit = static_cast(m_pm.FindControl(_T("pwdedit"))); if(pPasswordEdit) { password = pPasswordEdit->GetText().GetData(); } LOG_INFO("account:" << account.c_str() << ", password:" << password.c_str()); string s_account = CLewaimaiString::UnicodeToUTF8(account); string s_password = CLewaimaiString::UnicodeToUTF8(password); CLewaimaiString::trim(s_account); CLewaimaiString::trim(s_password); CLabelUI* pLoginResultLabel = static_cast(m_pm.FindControl(_T("loginresult"))); if (s_account.compare("") == 0) { pLoginResultLabel->SetText(std::wstring(_T("用户名不能为空")).c_str()); pLoginResultLabel->SetVisible(true); return; } if (s_password.compare("") == 0) { pLoginResultLabel->SetText(std::wstring(_T("密码不能为空")).c_str()); pLoginResultLabel->SetVisible(true); return; } CZhipuziHttpClient::Init(s_account, s_password); std::string errmsg; bool res = CZhipuziHttpClient::Login(errmsg); if(res) { CCheckBoxUI* pAuto = static_cast(m_pm.FindControl(_T("login_auto_login"))); CCheckBoxUI* pRemember = static_cast(m_pm.FindControl(_T("login_remember_password"))); if (pAuto->IsSelected()) { CSetting::SetParam("setting_is_auto_login", "1", false); CSetting::SetParam("setting_is_remember_password", "1", false); //相当于开启自动登录,默认就是开启了记住密码了 CSetting::SetUser(s_account, s_password, true); } else { CSetting::SetParam("setting_is_auto_login", "0", false); if (pRemember->IsSelected()) { CSetting::SetParam("setting_is_remember_password", "1", false); } else { CSetting::SetParam("setting_is_remember_password", "0", false); } if (pRemember->IsSelected()) { CSetting::SetUser(s_account, s_password, true); } else { CSetting::SetUser(s_account, "", true); } } //在这里设置完参数后,统一保存到数据库 CSetting::SetParam("last_login_username", s_account, true); CMainWnd* pFrame = new CMainWnd(); if(pFrame == NULL) { return; } pFrame->SetIcon(IDI_ICON_DUILIB); pFrame->Create(NULL, _T("智铺子收银软件"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 1024, 768); pFrame->CenterWindow(); ::ShowWindow(*pFrame, SW_SHOWMAXIMIZED); Close(); } else { //登录失败了 CLabelUI* pLoginResultLabel = static_cast(m_pm.FindControl(_T("loginresult"))); pLoginResultLabel->SetText(std::wstring(_T("登录失败:") + CLewaimaiString::UTF8ToUnicode(errmsg)).c_str()); pLoginResultLabel->SetVisible(true); } }