From b43897d4f69cf85a425f48df9270a289bc0a0125 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期二, 19 十一月 2024 18:04:20 +0800
Subject: [PATCH] 1. 添加用户管理的菜单栏 2.更新菜单栏的状态
---
SourceCode/Bond/BondEq/UserManagerDlg.cpp | 153 ++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 128 insertions(+), 25 deletions(-)
diff --git a/SourceCode/Bond/BondEq/UserManagerDlg.cpp b/SourceCode/Bond/BondEq/UserManagerDlg.cpp
index c01bc32..24df33f 100644
--- a/SourceCode/Bond/BondEq/UserManagerDlg.cpp
+++ b/SourceCode/Bond/BondEq/UserManagerDlg.cpp
@@ -5,9 +5,12 @@
#include "BondEq.h"
#include "afxdialogex.h"
#include "UserManagerDlg.h"
+#include "InputDialog.h"
#include "UserManager.h"
#include "NewCellTypes/GridCellCombo.h"
#include "NewCellTypes/GridCellNumeric.h"
+
+#include <set>
// CUserManagerDlg 瀵硅瘽妗�
@@ -36,7 +39,7 @@
return;
int nRows = 1;
- int nCols = 7;
+ int nCols = 8;
int nFixRows = 1;
int nFixCols = 0;
@@ -45,10 +48,10 @@
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.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);
@@ -56,9 +59,9 @@
m_gridUserManager.SetFixedColumnCount(nFixCols);
// Col
- m_gridUserManager.SetColumnWidth(nColIdx, 90);
+ m_gridUserManager.SetColumnWidth(nColIdx, 20);
m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("No."));
- m_gridUserManager.SetColumnWidth(nColIdx, 100);
+ m_gridUserManager.SetColumnWidth(nColIdx, 70);
m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("鐢ㄦ埛鍚�"));
m_gridUserManager.SetColumnWidth(nColIdx, 70);
m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("瀵嗙爜"));
@@ -70,14 +73,24 @@
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.EnableSelection(TRUE);
m_gridUserManager.SetEditable(TRUE);
m_gridUserManager.SetRowResize(FALSE);
- m_gridUserManager.SetColumnResize(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();
}
@@ -95,6 +108,7 @@
return;
}
+ int nCurrNameRow = -1;
std::vector<std::vector<std::string>> usersData = userManager.getUsers();
if (!usersData.empty()) {
m_gridUserManager.SetRowCount(usersData.size() + 1);
@@ -110,10 +124,12 @@
}
m_gridUserManager.SetItemText(nRowIdx, nColIdx++, usersData[i][j].c_str());
}
- }
- m_gridUserManager.Invalidate();
- m_gridUserManager.UpdateWindow();
+ // 褰撳墠鐧诲綍鐢ㄦ埛楂樹寒
+ if (nCurrNameRow == -1 && usersData[i][0] == userManager.getCurrentUser()) {
+ nCurrNameRow = nRowIdx;
+ }
+ }
}
CStringArray permissions;
@@ -124,6 +140,7 @@
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); // 鏈�鍚庝竴鍒楀彧璇�
// 绗洓鍒楄缃潈闄愬垪涓轰笅鎷夋
@@ -142,12 +159,29 @@
}
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.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++) {
+ bool ret = m_gridUserManager.SetItemBkColour(nCurrNameRow, i, RGB(0, 255, 0));
+ }
+
+ m_gridUserManager.Invalidate();
+ m_gridUserManager.UpdateWindow();
}
void CUserManagerDlg::AddRow(CGridCtrl* pGridCtrl)
@@ -159,6 +193,22 @@
return;
}
+ CInputDialog inputDialog(_T("娣诲姞鐢ㄦ埛"), _T("璇疯緭鍏ョ敤鎴峰悕锛�"));
+ if (inputDialog.DoModal() != IDOK) {
+ return;
+ }
+
+ CString inputText = inputDialog.GetInputText();
+ if (inputText.IsEmpty()) {
+ AfxMessageBox(_T("鐢ㄦ埛鍚嶄笉鑳戒负绌猴紒"), MB_OK | MB_ICONEXCLAMATION);
+ return;
+ }
+
+ if (IsUsernameDuplicate(inputText)) {
+ AfxMessageBox(_T("鐢ㄦ埛鍚嶉噸澶嶏紒"), MB_OK | MB_ICONEXCLAMATION);
+ return;
+ }
+
int nRowCount = pGridCtrl->GetRowCount();
pGridCtrl->SetRowCount(nRowCount + 1);
int newRowIndex = nRowCount;
@@ -166,17 +216,22 @@
CString strText;
strText.Format(_T("%d"), newRowIndex);
pGridCtrl->SetItemText(newRowIndex, 0, strText);
- strText.Format(_T("User%d"), newRowIndex);
- pGridCtrl->SetItemText(newRowIndex, 1, 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"));
- int nCols = m_gridUserManager.GetColumnCount();
- m_gridUserManager.SetItemState(newRowIndex, 0, GVIS_READONLY); // 绗竴鍒�
- m_gridUserManager.SetItemState(newRowIndex, nCols - 1, GVIS_READONLY); // 鏈�鍚庝竴鍒�
+ 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;
@@ -184,18 +239,22 @@
permissions.Add(_T("宸ョ▼甯�"));
permissions.Add(_T("鎿嶄綔鍛�"));
- if (m_gridUserManager.SetCellType(newRowIndex, 3, RUNTIME_CLASS(CGridCellCombo))) {
- CGridCellCombo* pCell = static_cast<CGridCellCombo*>(m_gridUserManager.GetCell(newRowIndex, 3));
+ if (pGridCtrl->SetCellType(newRowIndex, 3, RUNTIME_CLASS(CGridCellCombo))) {
+ CGridCellCombo* pCell = static_cast<CGridCellCombo*>(pGridCtrl->GetCell(newRowIndex, 3));
pCell->SetOptions(permissions);
pCell->SetStyle(CBS_DROPDOWNLIST);
- m_gridUserManager.SetItemText(newRowIndex, 3, permissions.GetAt(2));
+ pGridCtrl->SetItemText(newRowIndex, 3, permissions.GetAt(2));
}
// 绗簲銆佸叚鍒楋紙浼氳瘽瓒呮椂锛夎缃负Numeric
- m_gridUserManager.SetCellType(newRowIndex, 4, RUNTIME_CLASS(CGridCellNumeric));
- m_gridUserManager.SetCellType(newRowIndex, 5, RUNTIME_CLASS(CGridCellNumeric));
+ //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();
}
@@ -251,6 +310,8 @@
pGridCtrl->DeleteRow(*it);
}
+ pGridCtrl->ExpandColumnsToFit(FALSE);
+ pGridCtrl->ExpandLastColumn();
pGridCtrl->Invalidate();
pGridCtrl->UpdateWindow();
}
@@ -335,6 +396,7 @@
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)
@@ -390,6 +452,31 @@
// 閬嶅巻瀵硅瘽妗嗕腑鐨勬墍鏈夋帶浠�
AdjustControls(rect.Width(), rect.Height());
+}
+
+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) {
+ 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()
@@ -471,11 +558,27 @@
{
// TODO: 鍦ㄦ娣诲姞鎺т欢閫氱煡澶勭悊绋嬪簭浠g爜
std::vector<std::vector<std::string>> vecData;
+ std::set<std::string> usernameSet; // 鐢ㄤ簬瀛樺偍鐢ㄦ埛鍚嶏紝妫�鏌ユ槸鍚﹂噸澶�
+
for (int i = 1; i < m_gridUserManager.GetRowCount(); ++i) {
std::vector<std::string> rowData;
- for (int j = 1; j < m_gridUserManager.GetColumnCount(); ++j) {
+ CString cellText = m_gridUserManager.GetItemText(i, 1);
+ std::string username = CT2A(cellText.GetString());
+
+ if (usernameSet.find(username) != usernameSet.end()) {
+ CString message;
+ message.Format(_T("鐢ㄦ埛鍚� [%s] 閲嶅锛岃淇敼鍚庝繚瀛橈紒"), cellText);
+ AfxMessageBox(message, MB_ICONEXCLAMATION);
+ return;
+ }
+
+ usernameSet.insert(username);
+
+ 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";
--
Gitblit v1.9.3