#include "stdafx.h" #include "CUserEdit2Dlg.h" #include "CUserManager2.h" #include "resource.h" IMPLEMENT_DYNAMIC(CUserEdit2Dlg, CDialogEx) CUserEdit2Dlg::CUserEdit2Dlg(bool editMode, CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_DIALOG_USER_EDIT2, pParent) { m_bEditMode = editMode; } CUserEdit2Dlg::~CUserEdit2Dlg() { } void CUserEdit2Dlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT_USER_ACCOUNT, m_strUsername); DDX_Text(pDX, IDC_EDIT_USER_DISPLAY, m_strDisplayName); DDX_Text(pDX, IDC_EDIT_USER_PASSWORD, m_strPassword); DDX_CBString(pDX, IDC_COMBO_USER_ROLE, m_strRole); DDX_Check(pDX, IDC_CHECK_USER_ENABLED, m_bEnabled); } BEGIN_MESSAGE_MAP(CUserEdit2Dlg, CDialogEx) END_MESSAGE_MAP() BOOL CUserEdit2Dlg::OnInitDialog() { CDialogEx::OnInitDialog(); if (m_bEditMode) { if (auto pEdit = GetDlgItem(IDC_EDIT_USER_ACCOUNT)) { pEdit->EnableWindow(FALSE); } } UpdateData(FALSE); auto roles = CUserManager2::getInstance().getRoles(); CComboBox* pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_USER_ROLE); if (pCombo) { int selected = -1; for (const auto& role : roles) { CString text(role.name.c_str()); int idx = pCombo->AddString(text); if (selected == -1 && m_strRole.CompareNoCase(text) == 0) { selected = idx; } } if (selected >= 0) { pCombo->SetCurSel(selected); } else if (pCombo->GetCount() > 0) { pCombo->SetCurSel(0); CString text; pCombo->GetLBText(0, text); if (m_strRole.IsEmpty()) { m_strRole = text; } } } if (auto pPwd = GetDlgItem(IDC_EDIT_USER_PASSWORD)) { pPwd->EnableWindow(!m_bEditMode); if (m_bEditMode) { pPwd->SetWindowText(_T("")); } } return TRUE; } void CUserEdit2Dlg::OnOK() { UpdateData(TRUE); CString user = m_strUsername; user.Trim(); CString role = m_strRole; role.Trim(); CString password = m_strPassword; password.Trim(); if (m_bEditMode) { password.Empty(); } if (!m_bEditMode) { if (user.IsEmpty()) { AfxMessageBox(_T("请输入账号")); return; } if (password.IsEmpty()) { AfxMessageBox(_T("请输入密码")); return; } } if (role.IsEmpty()) { AfxMessageBox(_T("请选择角色")); return; } if (auto pCombo = (CComboBox*)GetDlgItem(IDC_COMBO_USER_ROLE)) { int sel = pCombo->GetCurSel(); if (sel != CB_ERR) { CString text; pCombo->GetLBText(sel, text); if (!text.IsEmpty()) { m_strRole = text; } } } m_strUsername = user; m_strPassword = password; CDialogEx::OnOK(); }