| ¶Ô±ÈÐÂÎļþ |
| | |
| | | // UserManagerDlg.cpp: å®ç°æä»¶ |
| | | // |
| | | |
| | | #include "stdafx.h" |
| | | #include "BondEq.h" |
| | | #include "afxdialogex.h" |
| | | #include "UserManagerDlg.h" |
| | | #include "InputDialog.h" |
| | | #include "NewCellTypes/GridCellCombo.h" |
| | | #include "NewCellTypes/GridCellNumeric.h" |
| | | |
| | | #include <set> |
| | | |
| | | const COLORREF CURR_USER_BK_COLOR = RGB(0, 255, 0); |
| | | |
| | | // CUserManagerDlg å¯¹è¯æ¡ |
| | | |
| | | IMPLEMENT_DYNAMIC(CUserManagerDlg, CDialogEx) |
| | | |
| | | CUserManagerDlg::CUserManagerDlg(CWnd* pParent /*=nullptr*/) |
| | | : CDialogEx(IDD_DIALOG_USER_MANAGER, pParent) |
| | | { |
| | | m_nInitialWidth = 0; |
| | | m_nInitialHeight = 0; |
| | | } |
| | | |
| | | CUserManagerDlg::~CUserManagerDlg() |
| | | { |
| | | for (auto& pair : m_mapFonts) { |
| | | if (pair.second) { |
| | | pair.second->DeleteObject(); |
| | | delete pair.second; |
| | | } |
| | | } |
| | | m_mapFonts.clear(); |
| | | } |
| | | |
| | | void CUserManagerDlg::DoDataExchange(CDataExchange* pDX) |
| | | { |
| | | DDX_Control(pDX, IDC_CUSTOM_USER, m_gridUserManager); |
| | | CDialogEx::DoDataExchange(pDX); |
| | | } |
| | | |
| | | void CUserManagerDlg::InitUserManager() |
| | | { |
| | | if (m_gridUserManager.GetSafeHwnd() == NULL) |
| | | return; |
| | | |
| | | int nRows = 1; |
| | | int nCols = 8; |
| | | |
| | | int nFixRows = 1; |
| | | int nFixCols = 0; |
| | | int nRowIdx = 0; |
| | | int nColIdx = 0; |
| | | |
| | | m_gridUserManager.DeleteAllItems(); |
| | | m_gridUserManager.SetVirtualMode(FALSE); |
| | | m_gridUserManager.GetDefaultCell(TRUE, FALSE)->SetBackClr(g_nGridFixCellColor); // 设置åºå®è¡èæ¯è² |
| | | m_gridUserManager.GetDefaultCell(FALSE, TRUE)->SetBackClr(g_nGridFixCellColor); // 设置åºå®åèæ¯è² |
| | | m_gridUserManager.GetDefaultCell(FALSE, FALSE)->SetBackClr(g_nGridCellColor); // 设置åå
æ ¼èæ¯è² |
| | | m_gridUserManager.SetFixedTextColor(g_nGridFixFontColor); // 设置åºå®è¡ååä½é¢è² |
| | | |
| | | m_gridUserManager.SetRowCount(nRows); |
| | | m_gridUserManager.SetColumnCount(nCols); |
| | | m_gridUserManager.SetFixedRowCount(nFixRows); |
| | | m_gridUserManager.SetFixedColumnCount(nFixCols); |
| | | |
| | | // Col |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 20); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("No.")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("ç¨æ·å")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("å¯ç ")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("æé")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("ä¼è¯è¶
æ¶ï¼åéï¼")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("ä¼è¯è¿æï¼å°æ¶ï¼")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 70); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("æå䏿¬¡ç»å½æ¶é´")); |
| | | m_gridUserManager.SetColumnWidth(nColIdx, 100); |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("è§è²æè¿°")); |
| | | |
| | | m_gridUserManager.SetFixedRowSelection(FALSE); |
| | | m_gridUserManager.SetFixedColumnSelection(FALSE); |
| | | m_gridUserManager.SetEditable(TRUE); |
| | | m_gridUserManager.SetRowResize(FALSE); |
| | | m_gridUserManager.SetColumnResize(TRUE); |
| | | m_gridUserManager.ExpandColumnsToFit(TRUE); |
| | | m_gridUserManager.SetListMode(TRUE); // å¯ç¨åè¡¨æ¨¡å¼ |
| | | m_gridUserManager.EnableSelection(TRUE); // å¯ç¨éæ© |
| | | m_gridUserManager.SetSingleRowSelection(TRUE); // èªå¨æ´è¡é«äº®ï¼éå¶ä¸ºåè¡éæ©ï¼ |
| | | m_gridUserManager.ExpandLastColumn(); // æåä¸åå¡«å
ç½æ ¼ |
| | | |
| | | m_mapRoleDescriptions.clear(); |
| | | m_mapRoleDescriptions.emplace(_T("管çå"), _T("ç®¡çææç¨æ·ï¼åé
æé")); |
| | | m_mapRoleDescriptions.emplace(_T("å·¥ç¨å¸"), _T("ç»´æ¤ç³»ç»ï¼è§£å³ææ¯é®é¢")); |
| | | m_mapRoleDescriptions.emplace(_T("æä½å"), _T("æ§è¡æ¥å¸¸æä½ä»»å¡")); |
| | | |
| | | FillUserManager(); |
| | | } |
| | | |
| | | void CUserManagerDlg::FillUserManager() |
| | | { |
| | | UserManager& userManager = UserManager::getInstance(); |
| | | if (!userManager.isLoggedIn()) { |
| | | AfxMessageBox(_T("æªç»å½")); |
| | | return; |
| | | } |
| | | |
| | | if (userManager.getCurrentUserRole() != UserRole::SuperAdmin) { |
| | | AfxMessageBox(_T("é管çåç¨æ·")); |
| | | return; |
| | | } |
| | | |
| | | int nCurrNameRow = -1; |
| | | std::vector<std::vector<std::string>> usersData = userManager.getUsers(); |
| | | if (!usersData.empty()) { |
| | | m_gridUserManager.SetRowCount(usersData.size() + 1); |
| | | |
| | | for (int i = 0; i < usersData.size(); i++) { |
| | | int nRowIdx = i + 1; |
| | | int nColIdx = 0; |
| | | |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, std::to_string(i + 1).c_str()); |
| | | for (int j = 0; j < usersData[i].size(); j++) { |
| | | if (usersData[i][1].empty()) { |
| | | continue; |
| | | } |
| | | m_gridUserManager.SetItemText(nRowIdx, nColIdx++, usersData[i][j].c_str()); |
| | | } |
| | | |
| | | // å½åç»å½ç¨æ·é«äº® |
| | | if (nCurrNameRow == -1 && usersData[i][0] == userManager.getCurrentUser()) { |
| | | nCurrNameRow = nRowIdx; |
| | | } |
| | | } |
| | | } |
| | | |
| | | CStringArray permissions; |
| | | permissions.Add(_T("管çå")); |
| | | permissions.Add(_T("å·¥ç¨å¸")); |
| | | permissions.Add(_T("æä½å")); |
| | | |
| | | int nCols = m_gridUserManager.GetColumnCount(); |
| | | for (int i = 1; i < m_gridUserManager.GetRowCount(); ++i) { |
| | | m_gridUserManager.SetItemState(i, 0, GVIS_READONLY); // 第ä¸ååªè¯» |
| | | m_gridUserManager.SetItemState(i, nCols - 2, GVIS_READONLY); // åæ°ç¬¬äºååªè¯» |
| | | m_gridUserManager.SetItemState(i, nCols - 1, GVIS_READONLY); // æåä¸ååªè¯» |
| | | |
| | | // 第åå设置æéåä¸ºä¸ææ¡ |
| | | if (m_gridUserManager.SetCellType(i, 3, RUNTIME_CLASS(CGridCellCombo))) { |
| | | CGridCellCombo* pCell = static_cast<CGridCellCombo*>(m_gridUserManager.GetCell(i, 3)); |
| | | pCell->SetOptions(permissions); |
| | | pCell->SetStyle(CBS_DROPDOWNLIST); |
| | | |
| | | CString cstrRole = m_gridUserManager.GetItemText(i, 3); |
| | | int nRole = _ttoi(cstrRole); |
| | | if (nRole < 0 || nRole > 2) { |
| | | CString cstrMessage; |
| | | cstrMessage.Format(_T("ç¨æ· [%s]ï¼æéå¼å¸¸ï¼å°è®¾ç½®ææä½åï¼"), m_gridUserManager.GetItemText(i, 1)); |
| | | AfxMessageBox(cstrMessage); |
| | | nRole = 2; |
| | | } |
| | | |
| | | m_gridUserManager.SetItemText(i, 3, permissions.GetAt(nRole)); |
| | | |
| | | auto it = m_mapRoleDescriptions.find(permissions.GetAt(nRole)); |
| | | if (it != m_mapRoleDescriptions.end()) { |
| | | m_gridUserManager.SetItemText(i, 7, it->second); |
| | | } |
| | | } |
| | | |
| | | // 第äºãå
åï¼ä¼è¯è¶
æ¶ï¼è®¾ç½®ä¸ºNumeric |
| | | //m_gridUserManager.SetCellType(i, 4, RUNTIME_CLASS(CGridCellNumeric)); |
| | | //m_gridUserManager.SetCellType(i, 5, RUNTIME_CLASS(CGridCellNumeric)); |
| | | m_gridUserManager.SetItemState(i, 4, GVIS_READONLY); // 第äºååªè¯» |
| | | m_gridUserManager.SetItemState(i, 5, GVIS_READONLY); // 第å
ååªè¯» |
| | | |
| | | m_gridUserManager.ExpandColumnsToFit(FALSE); |
| | | m_gridUserManager.ExpandLastColumn(); |
| | | } |
| | | |
| | | for (int i = 0; i < nCols; i++) { |
| | | m_gridUserManager.SetItemBkColour(nCurrNameRow, i, CURR_USER_BK_COLOR); |
| | | } |
| | | m_gridUserManager.SetItemState(nCurrNameRow, 3, GVIS_READONLY); |
| | | |
| | | m_gridUserManager.Invalidate(); |
| | | m_gridUserManager.UpdateWindow(); |
| | | } |
| | | |
| | | void CUserManagerDlg::AddRow(CGridCtrl* pGridCtrl) |
| | | { |
| | | if (!pGridCtrl) return; |
| | | |
| | | if (pGridCtrl->GetRowCount() <= 0 || pGridCtrl->GetColumnCount() <= 0) { |
| | | AfxMessageBox(_T("ç½æ ¼æ§ä»¶æªæ£ç¡®åå§åï¼è¯·æ£æ¥åå§åé»è¾ï¼")); |
| | | return; |
| | | } |
| | | |
| | | CInputDialog inputDialog(_T("æ·»å ç¨æ·"), _T("请è¾å
¥ç¨æ·åï¼")); |
| | | if (inputDialog.DoModal() != IDOK) { |
| | | return; |
| | | } |
| | | |
| | | CString inputText = inputDialog.GetInputText(); |
| | | if (inputText.IsEmpty()) { |
| | | AfxMessageBox(_T("ç¨æ·åä¸è½ä¸ºç©ºï¼")); |
| | | return; |
| | | } |
| | | |
| | | if (IsUsernameDuplicate(inputText)) { |
| | | AfxMessageBox(_T("ç¨æ·åéå¤ï¼")); |
| | | return; |
| | | } |
| | | |
| | | int nRowCount = pGridCtrl->GetRowCount(); |
| | | pGridCtrl->SetRowCount(nRowCount + 1); |
| | | int newRowIndex = nRowCount; |
| | | |
| | | CString strText; |
| | | strText.Format(_T("%d"), newRowIndex); |
| | | pGridCtrl->SetItemText(newRowIndex, 0, strText); |
| | | pGridCtrl->SetItemText(newRowIndex, 1, inputText); |
| | | pGridCtrl->SetItemText(newRowIndex, 2, _T("123456")); |
| | | pGridCtrl->SetItemText(newRowIndex, 3, _T("æä½å")); |
| | | pGridCtrl->SetItemText(newRowIndex, 4, _T("30")); |
| | | pGridCtrl->SetItemText(newRowIndex, 5, _T("72")); |
| | | pGridCtrl->SetItemText(newRowIndex, 6, _T("2024-01-01 00:00:00")); |
| | | |
| | | auto it = m_mapRoleDescriptions.find(_T("æä½å")); |
| | | if (it != m_mapRoleDescriptions.end()) { |
| | | pGridCtrl->SetItemText(newRowIndex, 7, it->second); |
| | | } |
| | | |
| | | int nCols = pGridCtrl->GetColumnCount(); |
| | | pGridCtrl->SetItemState(newRowIndex, 0, GVIS_READONLY); // 第ä¸ååªè¯» |
| | | pGridCtrl->SetItemState(newRowIndex, nCols - 2, GVIS_READONLY); // åæ°ç¬¬äºååªè¯» |
| | | pGridCtrl->SetItemState(newRowIndex, nCols - 1, GVIS_READONLY); // æåä¸ååªè¯» |
| | | |
| | | // 第ååè®¾ç½®ï¼æéåï¼ä¸ºä¸ææ¡ |
| | | CStringArray permissions; |
| | | permissions.Add(_T("管çå")); |
| | | permissions.Add(_T("å·¥ç¨å¸")); |
| | | permissions.Add(_T("æä½å")); |
| | | |
| | | if (pGridCtrl->SetCellType(newRowIndex, 3, RUNTIME_CLASS(CGridCellCombo))) { |
| | | CGridCellCombo* pCell = static_cast<CGridCellCombo*>(pGridCtrl->GetCell(newRowIndex, 3)); |
| | | pCell->SetOptions(permissions); |
| | | pCell->SetStyle(CBS_DROPDOWNLIST); |
| | | |
| | | pGridCtrl->SetItemText(newRowIndex, 3, permissions.GetAt(2)); |
| | | } |
| | | |
| | | // 第äºãå
åï¼ä¼è¯è¶
æ¶ï¼è®¾ç½®ä¸ºNumeric |
| | | //pGridCtrl->SetCellType(newRowIndex, 4, RUNTIME_CLASS(CGridCellNumeric)); |
| | | //pGridCtrl->SetCellType(newRowIndex, 5, RUNTIME_CLASS(CGridCellNumeric)); |
| | | pGridCtrl->SetItemState(newRowIndex, 4, GVIS_READONLY); // 第äºååªè¯» |
| | | pGridCtrl->SetItemState(newRowIndex, 5, GVIS_READONLY); // 第å
ååªè¯» |
| | | |
| | | pGridCtrl->ExpandColumnsToFit(FALSE); |
| | | pGridCtrl->ExpandLastColumn(); |
| | | pGridCtrl->Invalidate(); |
| | | pGridCtrl->UpdateWindow(); |
| | | |
| | | CString cstrMessage; |
| | | cstrMessage.Format(_T("颿·»å ç¨æ· [%s]ï¼"), inputText); |
| | | std::string strMessage = CT2A(cstrMessage); |
| | | SystemLogManager::getInstance().log(SystemLogManager::LogType::Operation, strMessage); |
| | | } |
| | | |
| | | void CUserManagerDlg::DeleteSelectedRow(CGridCtrl* pGridCtrl) |
| | | { |
| | | if (!pGridCtrl) return; |
| | | CCellRange selectedRange = pGridCtrl->GetSelectedCellRange(); |
| | | if (selectedRange.IsValid()) { |
| | | CString currentUser = UserManager::getInstance().getCurrentUser().c_str(); |
| | | std::vector<int> rowsToDelete; |
| | | |
| | | for (int row = selectedRange.GetMinRow(); row <= selectedRange.GetMaxRow(); ++row) { |
| | | BOOL isRowSelected = FALSE; |
| | | for (int col = selectedRange.GetMinCol(); col <= selectedRange.GetMaxCol(); ++col) { |
| | | if (pGridCtrl->IsCellSelected(row, col)) { |
| | | isRowSelected = TRUE; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (!isRowSelected) { |
| | | continue; |
| | | } |
| | | |
| | | CString selectedUser = pGridCtrl->GetItemText(row, 1); |
| | | if (selectedUser == currentUser) { |
| | | CString message; |
| | | message.Format(_T("ç¨æ· [%s] æ¯å½åç»å½ç¨æ·ï¼ä¸è½å é¤ï¼"), currentUser); |
| | | AfxMessageBox(message); |
| | | return; |
| | | } |
| | | |
| | | rowsToDelete.push_back(row); |
| | | } |
| | | |
| | | if (rowsToDelete.empty()) { |
| | | AfxMessageBox(_T("请å
éæ©è¦å é¤çè¡ï¼")); |
| | | return; |
| | | } |
| | | |
| | | CString message; |
| | | if (rowsToDelete.size() == 1) { |
| | | CString selectedUser = pGridCtrl->GetItemText(rowsToDelete[0], 1); |
| | | message.Format(_T("ç¡®å®è¦å é¤éä¸ç¨æ· [%s] åï¼"), selectedUser); |
| | | |
| | | CString cstrMessage; |
| | | cstrMessage.Format(_T("é¢å é¤ç¨æ· [%s]ï¼"), selectedUser); |
| | | std::string strMessage = CT2A(cstrMessage); |
| | | SystemLogManager::getInstance().log(SystemLogManager::LogType::Operation, strMessage); |
| | | } |
| | | else { |
| | | message.Format(_T("ç¡®å®è¦å é¤éä¸ç %d ä¸ªç¨æ·åï¼"), rowsToDelete.size()); |
| | | } |
| | | |
| | | if (AfxMessageBox(message, MB_YESNO | MB_ICONQUESTION) == IDYES) { |
| | | for (auto it = rowsToDelete.rbegin(); it != rowsToDelete.rend(); ++it) { |
| | | pGridCtrl->DeleteRow(*it); |
| | | } |
| | | |
| | | pGridCtrl->ExpandColumnsToFit(FALSE); |
| | | pGridCtrl->ExpandLastColumn(); |
| | | pGridCtrl->Invalidate(); |
| | | pGridCtrl->UpdateWindow(); |
| | | } |
| | | } |
| | | else { |
| | | AfxMessageBox(_T("请å
éæ©è¦å é¤çç¨æ·ï¼")); |
| | | } |
| | | } |
| | | |
| | | bool CUserManagerDlg::IsUsernameDuplicate(const CString& username) |
| | | { |
| | | for (int row = 1; row < m_gridUserManager.GetRowCount(); ++row) { |
| | | if (m_gridUserManager.GetItemText(row, 1) == username) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | CFont* CUserManagerDlg::GetOrCreateFont(int nFontSize) |
| | | { |
| | | auto it = m_mapFonts.find(nFontSize); |
| | | if (it != m_mapFonts.end()) { |
| | | return it->second; |
| | | } |
| | | |
| | | CFont* font = new CFont(); |
| | | LOGFONT logFont = { 0 }; |
| | | _tcscpy_s(logFont.lfFaceName, _T("Segoe UI")); |
| | | logFont.lfHeight = -nFontSize; |
| | | logFont.lfQuality = CLEARTYPE_QUALITY; |
| | | font->CreateFontIndirect(&logFont); |
| | | m_mapFonts[nFontSize] = font; |
| | | |
| | | return font; |
| | | } |
| | | |
| | | void CUserManagerDlg::SetDefaultFont() |
| | | { |
| | | CFont* defaultFont = GetOrCreateFont(12); |
| | | |
| | | // éåæææ§ä»¶ï¼åºç¨é»è®¤åä½ |
| | | CWnd* pWnd = GetWindow(GW_CHILD); |
| | | while (pWnd) { |
| | | // è·³è¿ç¹æ®æ§ä»¶ï¼å¦ MFCGridCtrlï¼ |
| | | TCHAR szClassName[256]; |
| | | GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName)); |
| | | if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) { |
| | | pWnd = pWnd->GetNextWindow(); |
| | | continue; |
| | | } |
| | | |
| | | pWnd->SetFont(defaultFont, TRUE); |
| | | pWnd = pWnd->GetNextWindow(); |
| | | } |
| | | } |
| | | |
| | | void CUserManagerDlg::AdjustControls(float dScaleX, float dScaleY) |
| | | { |
| | | CWnd* pWnd = GetWindow(GW_CHILD); |
| | | while (pWnd) { |
| | | int nCtrlID = pWnd->GetDlgCtrlID(); |
| | | if (nCtrlID != -1 && m_mapCtrlLayouts.find(nCtrlID) != m_mapCtrlLayouts.end()) |
| | | { |
| | | CRect originalRect = m_mapCtrlLayouts[nCtrlID]; |
| | | CRect newRect( |
| | | static_cast<int>(originalRect.left * dScaleX), |
| | | static_cast<int>(originalRect.top * dScaleY), |
| | | static_cast<int>(originalRect.right * dScaleX), |
| | | static_cast<int>(originalRect.bottom * dScaleY)); |
| | | |
| | | TCHAR szClassName[256]; |
| | | GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName)); |
| | | |
| | | if (_tcsicmp(szClassName, _T("ComboBox")) == 0) { |
| | | CComboBox* pComboBox = (CComboBox*)pWnd; |
| | | pComboBox->SetItemHeight(-1, newRect.Height()); // -1 表示ææé¡¹çé«åº¦ |
| | | } |
| | | |
| | | if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) { |
| | | CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd; |
| | | pGridCtrl->SetDefCellHeight(newRect.Height() / 21); |
| | | pGridCtrl->ExpandColumnsToFit(TRUE); |
| | | pGridCtrl->ExpandLastColumn(); |
| | | pGridCtrl->Invalidate(); |
| | | pGridCtrl->UpdateWindow(); |
| | | } |
| | | |
| | | pWnd->MoveWindow(&newRect); |
| | | AdjustControlFont(pWnd, newRect.Width(), newRect.Height()); |
| | | } |
| | | pWnd = pWnd->GetNextWindow(); |
| | | } |
| | | } |
| | | |
| | | void CUserManagerDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight) |
| | | { |
| | | TCHAR szClassName[256]; |
| | | GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName)); |
| | | |
| | | // è·³è¿ç¹æ®æ§ä»¶ï¼å¦ MFCGridCtrlï¼ |
| | | if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) { |
| | | return; |
| | | } |
| | | |
| | | // æ ¹æ®æ§ä»¶é«åº¦å¨æè°æ´åä½å¤§å° |
| | | int fontSize = nHeight / 2; |
| | | if (fontSize < 8) fontSize = 8; |
| | | |
| | | // è·åæå建åä½ |
| | | CFont* pFont = GetOrCreateFont(fontSize); |
| | | |
| | | pWnd->SetFont(pFont); |
| | | pWnd->Invalidate(); // å·æ°æ§ä»¶æ¾ç¤º |
| | | } |
| | | |
| | | BEGIN_MESSAGE_MAP(CUserManagerDlg, CDialogEx) |
| | | ON_WM_SIZE() |
| | | ON_NOTIFY(GVN_COMBOSELCHANGE, IDC_CUSTOM_USER, &CUserManagerDlg::OnGridComboSelChange) |
| | | ON_BN_CLICKED(IDC_BUTTON_ADD, &CUserManagerDlg::OnBnClickedButtonAdd) |
| | | ON_BN_CLICKED(IDC_BUTTON_DEL, &CUserManagerDlg::OnBnClickedButtonDel) |
| | | ON_BN_CLICKED(IDOK, &CUserManagerDlg::OnBnClickedOk) |
| | | ON_BN_CLICKED(IDC_BUTTON_INSERT, &CUserManagerDlg::OnBnClickedButtonInsert) |
| | | ON_WM_GETMINMAXINFO() |
| | | END_MESSAGE_MAP() |
| | | |
| | | |
| | | // CUserManagerDlg æ¶æ¯å¤çç¨åº |
| | | |
| | | |
| | | BOOL CUserManagerDlg::OnInitDialog() |
| | | { |
| | | CDialogEx::OnInitDialog(); |
| | | |
| | | // TODO: 卿¤æ·»å é¢å¤çåå§å |
| | | CRect screenRect, dlgRect, clientRect; |
| | | SetWindowText(_T("ç¨æ·ç®¡ç")); |
| | | SystemParametersInfo(SPI_GETWORKAREA, 0, &screenRect, 0); |
| | | |
| | | GetClientRect(&clientRect); |
| | | m_nInitialWidth = clientRect.Width(); |
| | | m_nInitialHeight = clientRect.Height(); |
| | | |
| | | // åå§åé»è®¤åä½ |
| | | CFont* pDefaultFont = GetOrCreateFont(12); |
| | | |
| | | // éåææåæ§ä»¶ï¼è®°å½åå§ä½ç½®å¹¶è®¾ç½®é»è®¤åä½ |
| | | CWnd* pWnd = GetWindow(GW_CHILD); |
| | | while (pWnd) { |
| | | int nCtrlID = pWnd->GetDlgCtrlID(); |
| | | if (nCtrlID != -1) { |
| | | // è®°å½æ§ä»¶åå§å¸å± |
| | | CRect ctrlRect; |
| | | pWnd->GetWindowRect(&ctrlRect); |
| | | ScreenToClient(&ctrlRect); |
| | | m_mapCtrlLayouts[nCtrlID] = ctrlRect; |
| | | |
| | | // è·³è¿ç¹æ®æ§ä»¶ï¼å¦ MFCGridCtrlï¼ |
| | | TCHAR szClassName[256]; |
| | | GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName)); |
| | | if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) { |
| | | pWnd = pWnd->GetNextWindow(); |
| | | continue; |
| | | } |
| | | |
| | | // 设置é»è®¤åä½ |
| | | pWnd->SetFont(pDefaultFont); |
| | | } |
| | | pWnd = pWnd->GetNextWindow(); |
| | | } |
| | | |
| | | GetWindowRect(&dlgRect); |
| | | int dlgWidth = dlgRect.Width() * 3; |
| | | int dlgHeight = dlgRect.Height() * 3; |
| | | |
| | | if (dlgWidth > screenRect.Width()) { |
| | | dlgWidth = screenRect.Width(); |
| | | } |
| | | if (dlgHeight > screenRect.Height()) { |
| | | dlgHeight = screenRect.Height(); |
| | | } |
| | | |
| | | int centerX = screenRect.left + (screenRect.Width() - dlgWidth) / 2; |
| | | int centerY = screenRect.top + (screenRect.Height() - dlgHeight) / 2; |
| | | MoveWindow(centerX, centerY, dlgWidth, dlgHeight); |
| | | |
| | | // åå§åç¨æ·ç®¡çè¡¨æ ¼ |
| | | InitUserManager(); |
| | | |
| | | return TRUE; // return TRUE unless you set the focus to a control |
| | | // å¼å¸¸: OCX 屿§é¡µåºè¿å FALSE |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnSize(UINT nType, int cx, int cy) |
| | | { |
| | | CDialogEx::OnSize(nType, cx, cy); |
| | | |
| | | // TODO: 卿¤å¤æ·»å æ¶æ¯å¤çç¨åºä»£ç |
| | | if (nType == SIZE_MINIMIZED || m_mapCtrlLayouts.empty()) { |
| | | return; |
| | | } |
| | | |
| | | float dScaleX = static_cast<float>(cx) / m_nInitialWidth; |
| | | float dScaleY = static_cast<float>(cy) / m_nInitialHeight; |
| | | |
| | | // éåå¯¹è¯æ¡ä¸çæææ§ä»¶ |
| | | AdjustControls(dScaleX, dScaleY); |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI) |
| | | { |
| | | // TODO: 卿¤æ·»å æ¶æ¯å¤çç¨åºä»£ç å/æè°ç¨é»è®¤å¼ |
| | | lpMMI->ptMinTrackSize.x = 400; // æå°å®½åº¦ |
| | | lpMMI->ptMinTrackSize.y = 300; // æå°é«åº¦ |
| | | |
| | | CDialogEx::OnGetMinMaxInfo(lpMMI); |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnGridComboSelChange(NMHDR* pNMHDR, LRESULT* pResult) |
| | | { |
| | | NM_GRIDVIEW* pItem = (NM_GRIDVIEW*)pNMHDR; |
| | | |
| | | int nRow = pItem->iRow; |
| | | int nCol = pItem->iColumn; |
| | | |
| | | // 第4å为æéå, 第8å为è§è²æè¿°å, 仿 å°ä¸æ¥æ¾å¯¹åºæè¿° |
| | | if (nCol == 3) { |
| | | if (m_gridUserManager.GetItemBkColour(nRow, nCol) == CURR_USER_BK_COLOR) { |
| | | AfxMessageBox(_T("å½åç»å½ç¨æ·æéä¸è½ä¿®æ¹ï¼")); |
| | | } |
| | | else { |
| | | CString selectedRole = m_gridUserManager.GetItemText(nRow, nCol); |
| | | auto it = m_mapRoleDescriptions.find(selectedRole); |
| | | if (it != m_mapRoleDescriptions.end()) { |
| | | m_gridUserManager.SetItemText(nRow, 7, it->second); |
| | | m_gridUserManager.RedrawCell(nRow, 7); |
| | | } |
| | | else { |
| | | m_gridUserManager.SetItemText(nRow, 7, _T("")); |
| | | m_gridUserManager.RedrawCell(nRow, 7); |
| | | } |
| | | } |
| | | } |
| | | |
| | | *pResult = 0; |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnBnClickedButtonAdd() |
| | | { |
| | | // TODO: 卿¤æ·»å æ§ä»¶éç¥å¤çç¨åºä»£ç |
| | | AddRow(&m_gridUserManager); |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnBnClickedButtonInsert() |
| | | { |
| | | // TODO: 卿¤æ·»å æ§ä»¶éç¥å¤çç¨åºä»£ç |
| | | if (!m_gridUserManager.GetSafeHwnd()) return; |
| | | |
| | | CCellRange selectedRange = m_gridUserManager.GetSelectedCellRange(); |
| | | if (!selectedRange.IsValid()) { |
| | | AfxMessageBox(_T("请å
éæ©è¦æå
¥çä½ç½®ï¼")); |
| | | return; |
| | | } |
| | | |
| | | int minRow = selectedRange.GetMinRow(); |
| | | int maxRow = selectedRange.GetMaxRow(); |
| | | if (minRow < 1) { |
| | | AfxMessageBox(_T("è¯·éæ©ææçè¡ï¼")); |
| | | return; |
| | | } |
| | | |
| | | for (int row = maxRow; row >= minRow; --row) { |
| | | std::vector<CString> rowData; |
| | | for (int col = 0; col < m_gridUserManager.GetColumnCount(); ++col) { |
| | | rowData.push_back(m_gridUserManager.GetItemText(row, col)); |
| | | } |
| | | |
| | | m_gridUserManager.InsertRow(_T("æ°ç¨æ·"), row); |
| | | CString newUsername = rowData[1]; |
| | | int suffix = 1; |
| | | while (IsUsernameDuplicate(newUsername)) { |
| | | newUsername.Format(_T("%s_%d"), rowData[1], suffix++); |
| | | } |
| | | rowData[1] = newUsername; |
| | | |
| | | for (int col = 0; col < m_gridUserManager.GetColumnCount(); ++col) { |
| | | m_gridUserManager.SetItemText(row, col, rowData[col]); |
| | | } |
| | | |
| | | CStringArray permissions; |
| | | permissions.Add(_T("管çå")); |
| | | permissions.Add(_T("å·¥ç¨å¸")); |
| | | permissions.Add(_T("æä½å")); |
| | | |
| | | if (m_gridUserManager.SetCellType(row, 3, RUNTIME_CLASS(CGridCellCombo))) { |
| | | CGridCellCombo* pCell = static_cast<CGridCellCombo*>(m_gridUserManager.GetCell(row, 3)); |
| | | pCell->SetOptions(permissions); |
| | | pCell->SetStyle(CBS_DROPDOWNLIST); |
| | | |
| | | m_gridUserManager.SetItemText(row, 3, rowData[3]); |
| | | } |
| | | |
| | | m_gridUserManager.SetCellType(row, 4, RUNTIME_CLASS(CGridCellNumeric)); |
| | | m_gridUserManager.SetCellType(row, 5, RUNTIME_CLASS(CGridCellNumeric)); |
| | | } |
| | | |
| | | for (int row = 1; row < m_gridUserManager.GetRowCount(); ++row) { |
| | | CString strIndex; |
| | | strIndex.Format(_T("%d"), row); |
| | | m_gridUserManager.SetItemText(row, 0, strIndex); |
| | | } |
| | | |
| | | m_gridUserManager.Invalidate(); |
| | | m_gridUserManager.UpdateWindow(); |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnBnClickedButtonDel() |
| | | { |
| | | // TODO: 卿¤æ·»å æ§ä»¶éç¥å¤çç¨åºä»£ç |
| | | DeleteSelectedRow(&m_gridUserManager); |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnBnClickedOk() |
| | | { |
| | | // TODO: 卿¤æ·»å æ§ä»¶éç¥å¤çç¨åºä»£ç |
| | | std::vector<std::vector<std::string>> vecData; |
| | | std::set<std::string> usernameSet; // ç¨äºåå¨ç¨æ·åï¼æ£æ¥æ¯å¦éå¤ |
| | | |
| | | int nCurrUserRow = -1; |
| | | UserManager& userManager = UserManager::getInstance(); |
| | | for (int i = 1; i < m_gridUserManager.GetRowCount(); ++i) { |
| | | std::vector<std::string> rowData; |
| | | CString cellText = m_gridUserManager.GetItemText(i, 1); |
| | | std::string username = CT2A(cellText.GetString()); |
| | | |
| | | cellText = m_gridUserManager.GetItemText(i, 2); |
| | | std::string userpass = CT2A(cellText.GetString()); |
| | | |
| | | if (username.empty() || userpass.empty()) { |
| | | AfxMessageBox(_T("ç¨æ·ååå¯ç ä¸è½ä¸ºç©ºï¼")); |
| | | return; |
| | | } |
| | | |
| | | if (usernameSet.find(username) != usernameSet.end()) { |
| | | CString message; |
| | | message.Format(_T("ç¨æ·å [%s] éå¤ï¼è¯·ä¿®æ¹åä¿åï¼"), cellText); |
| | | AfxMessageBox(message, MB_ICONEXCLAMATION); |
| | | return; |
| | | } |
| | | usernameSet.insert(username); |
| | | |
| | | if (nCurrUserRow == -1 && m_gridUserManager.GetItemBkColour(i, 0) == CURR_USER_BK_COLOR){ |
| | | nCurrUserRow = i; |
| | | if (username.compare(userManager.getCurrentUser()) != 0) { |
| | | userManager.setCurrentUser(username); |
| | | } |
| | | |
| | | if (userpass.compare(userManager.getCurrentPass()) != 0) { |
| | | userManager.setCurrentPass(userpass); |
| | | } |
| | | |
| | | userManager.clearSession(); |
| | | userManager.saveSession(); |
| | | } |
| | | |
| | | for (int j = 1; j < m_gridUserManager.GetColumnCount() - 1; ++j) { |
| | | CString cellText = m_gridUserManager.GetItemText(i, j); |
| | | std::string cellString = CT2A(cellText.GetString()); |
| | | |
| | | // 第4忝æéï¼è½¬æ¢ä¸ºæ°åå符串 |
| | | if (j == 3) { |
| | | if (cellText == _T("管çå")) |
| | | cellString = "0"; |
| | | else if (cellText == _T("å·¥ç¨å¸")) |
| | | cellString = "1"; |
| | | else if (cellText == _T("æä½å")) |
| | | cellString = "2"; |
| | | else |
| | | cellString = "2"; |
| | | } |
| | | |
| | | rowData.push_back(cellString); |
| | | } |
| | | |
| | | vecData.push_back(rowData); |
| | | } |
| | | |
| | | userManager.setUsers(vecData); |
| | | CDialogEx::OnOK(); |
| | | } |