// LoginDlg.cpp: 实现文件 // #include "stdafx.h" #include "BondEq.h" #include "afxdialogex.h" #include "LoginDlg.h" #include "UserManager.h" #include "ChangePasswordDlg.h" // CLoginDlg 对话框 IMPLEMENT_DYNAMIC(CLoginDlg, CDialogEx) CLoginDlg::CLoginDlg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_DIALOG_LOGIN, pParent) { } CLoginDlg::~CLoginDlg() { } void CLoginDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_EDIT_USERNAME, m_editUsername); DDX_Control(pDX, IDC_EDIT_PASSWORD, m_editPassword); DDX_Control(pDX, IDC_COMBO_ROLE, m_comboRole); DDX_Control(pDX, IDC_CHECK_REMEMBER_PASSWORD, m_checkRememberPassword); } BEGIN_MESSAGE_MAP(CLoginDlg, CDialogEx) ON_BN_CLICKED(IDC_BUTTON_LOGIN, &CLoginDlg::OnBnClickedLogin) ON_STN_CLICKED(IDC_STATIC_CHANGE_PASSWORD, &CLoginDlg::OnBnClickedChangePassword) END_MESSAGE_MAP() // CLoginDlg 消息处理程序 BOOL CLoginDlg::OnInitDialog() { CDialog::OnInitDialog(); // 设置窗口标题和初始值 SetWindowText(_T("登录")); m_comboRole.AddString(_T("管理员")); m_comboRole.AddString(_T("工程师")); m_comboRole.AddString(_T("操作员")); m_comboRole.SetCurSel(0); // 添加SS_NOTIFY样式 CStatic* pStatic = (CStatic*)GetDlgItem(IDC_STATIC_CHANGE_PASSWORD); if (pStatic != nullptr) { pStatic->ModifyStyle(0, SS_NOTIFY); } UserManager& userManager = UserManager::getInstance(); if (userManager.isLoggedIn()) { int nRole = (int)userManager.getCurrentUserRole(); if (nRole <= m_comboRole.GetCount()) { m_comboRole.SetCurSel(nRole); } if (userManager.isRememberMe()) { m_checkRememberPassword.SetCheck(BST_CHECKED); } userManager.getCurrentUserRole(); m_editUsername.SetWindowText(userManager.getCurrentUser().c_str()); m_editPassword.SetWindowText(userManager.getCurrentPass().c_str()); } return TRUE; } void CLoginDlg::OnBnClickedLogin() { CString username, password, role; m_editUsername.GetWindowText(username); m_editPassword.GetWindowText(password); m_comboRole.GetLBText(m_comboRole.GetCurSel(), role); if (username.IsEmpty() || password.IsEmpty()) { AfxMessageBox(_T("请输入用户名和密码。")); return; } #ifdef UNICODE std::string strUsername = CStringA(username); std::string strPassword = CStringA(password); #else std::string strUsername = username; std::string strPassword = password; #endif UserManager& userManager = UserManager::getInstance(); if (!userManager.login(strUsername, strPassword, (m_checkRememberPassword.GetCheck() == BST_CHECKED))) { AfxMessageBox(_T("登录失败。")); return; } EndDialog(IDOK); } void CLoginDlg::OnBnClickedChangePassword() { CChangePasswordDlg changePasswordDlg; changePasswordDlg.DoModal(); if (changePasswordDlg.DoModal() == IDOK) { m_editPassword.SetWindowText(""); } }