LAPTOP-T815PCOQ\25526
2024-11-15 6eacf60891a6ac09f96985af042df3c4ed3da000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// UserManagerDlg.cpp: 实现文件
//
 
#include "stdafx.h"
#include "BondEq.h"
#include "afxdialogex.h"
#include "UserManagerDlg.h"
#include "UserManager.h"
 
 
// CUserManagerDlg 对话框
 
IMPLEMENT_DYNAMIC(CUserManagerDlg, CDialogEx)
 
CUserManagerDlg::CUserManagerDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_DIALOG_USER_MANAGER, pParent)
{
 
}
 
CUserManagerDlg::~CUserManagerDlg()
{
}
 
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 = 7;
 
    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);
 
    CFont* pFont = m_gridUserManager.GetFont();
    if (pFont)
    {
        LOGFONT lf;
        pFont->GetLogFont(&lf);
        lf.lfItalic = 0;
        lf.lfHeight = 14;
        lf.lfWeight = FW_BOLD;
        _tcscpy_s(lf.lfFaceName, _T("Arial"));
 
        m_gridUserManager.GetDefaultCell(FALSE, TRUE)->SetFont(&lf);
        m_gridUserManager.GetDefaultCell(TRUE, FALSE)->SetFont(&lf);
        m_gridUserManager.GetDefaultCell(FALSE, FALSE)->SetFont(&lf);
        m_gridUserManager.GetDefaultCell(TRUE, TRUE)->SetFont(&lf);
    }
 
    // Col
    m_gridUserManager.SetColumnWidth(nColIdx, 90);
    m_gridUserManager.SetItemText(nRowIdx, nColIdx++, _T("No."));
    m_gridUserManager.SetColumnWidth(nColIdx, 100);
    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.SetFixedRowSelection(FALSE);
    m_gridUserManager.SetFixedColumnSelection(FALSE);
    m_gridUserManager.EnableSelection(TRUE);
    m_gridUserManager.SetEditable(TRUE);
    m_gridUserManager.SetRowResize(FALSE);
    m_gridUserManager.SetColumnResize(FALSE);
    m_gridUserManager.ExpandColumnsToFit(TRUE);
 
    FillUserManager();
}
 
void CUserManagerDlg::FillUserManager()
{
    UserManager& userManager = UserManager::getInstance();
    if (!userManager.isLoggedIn()) {
        AfxMessageBox("未登录");
        return;
    }
 
    if (userManager.getCurrentUserRole() != UserRole::SuperAdmin) {
        AfxMessageBox("非管理员用户");
        return;
    }
 
    std::vector<std::vector<std::string>> usersData = userManager.getUsers();
    if (usersData.size() > 0) {
        m_gridUserManager.SetRowCount(usersData.size() + 1);
        for (size_t 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 (size_t j = 0; j < usersData[i].size(); j++) {
                m_gridUserManager.SetItemText(nRowIdx, nColIdx++, usersData[i][j].c_str());
            }
        }
    }
}
 
void CUserManagerDlg::AdjustControls(int nWidth, int nHeight)
{
    CWnd* pWnd = GetWindow(GW_CHILD);
    while (pWnd) {
        UINT nCtrlID = pWnd->GetDlgCtrlID();
 
        CRect ctrlRect;
        pWnd->GetWindowRect(&ctrlRect);
        ScreenToClient(&ctrlRect);
 
        // 计算控件的新位置和大小,按比例调整
        int newX = (int)(ctrlRect.left * (nWidth / (float)m_nInitialWidth));
        int newY = (int)(ctrlRect.top * (nHeight / (float)m_nInitialHeight));
        int newWidth = (int)(ctrlRect.Width() * (nWidth / (float)m_nInitialWidth));
        int newHeight = (int)(ctrlRect.Height() * (nHeight / (float)m_nInitialHeight));
 
        TCHAR szClassName[256];
        GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
 
 
        if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
            CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd;
            //int nRowCount = pGridCtrl->GetRowCount();
            //int newRowHeight = (int)(newHeight / (float)nRowCount);
 
            //for (int i = 0; i < nRowCount; ++i) {
            //    pGridCtrl->SetRowHeight(i, newRowHeight);
            //}
 
            pGridCtrl->ExpandColumnsToFit(TRUE);
        }
 
        pWnd->MoveWindow(newX, newY, newWidth, newHeight);
        AdjustControlFont(pWnd, newWidth, newHeight);
 
        // 获取下一个控件
        pWnd = pWnd->GetNextWindow();
    }
}
 
void CUserManagerDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight)
{
    TCHAR szClassName[256];
    GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
 
    if (_tcsicmp(szClassName, _T("Static")) == 0) {
        CStatic* pStatic = (CStatic*)pWnd;
        pStatic->ModifyStyle(0, SS_CENTER | SS_CENTERIMAGE);
        return;
    }
 
    if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
        return;
    }
 
    int fontSize = nHeight - 10;
    CFont* pCurrentFont = pWnd->GetFont();
    LOGFONT logFont;
    pCurrentFont->GetLogFont(&logFont);
    logFont.lfHeight = -fontSize;
 
    CFont newFont;
    newFont.CreateFontIndirect(&logFont);
 
    pWnd->SetFont(&newFont);
    pWnd->Invalidate();
}
 
BEGIN_MESSAGE_MAP(CUserManagerDlg, CDialogEx)
    ON_WM_SIZE()
END_MESSAGE_MAP()
 
 
// CUserManagerDlg 消息处理程序
 
 
BOOL CUserManagerDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    // TODO:  在此添加额外的初始化
    CRect rect;
    GetClientRect(&rect);
    m_nInitialWidth = rect.Width();
    m_nInitialHeight = rect.Height();
 
    rect.right *= 3;
    rect.bottom *= 3;
    // 调整对话框大小
    MoveWindow(rect);
 
    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: 在此处添加消息处理程序代码
    CRect rect;
    GetClientRect(&rect);
 
    // 遍历对话框中的所有控件
    AdjustControls(rect.Width(), rect.Height());
}