// 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());
|
}
|