LAPTOP-T815PCOQ\25526
2024-11-21 d84ddeea533a8e5f122cc4a2fed06e93c2aacf3d
SourceCode/Bond/BondEq/View/UserManagerDlg.cpp
@@ -20,7 +20,8 @@
CUserManagerDlg::CUserManagerDlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_DIALOG_USER_MANAGER, pParent)
{
   m_nInitialWidth = 0;
   m_nInitialHeight = 0;
}
CUserManagerDlg::~CUserManagerDlg()
@@ -349,25 +350,27 @@
   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 = nullptr;
   // 如果字体管理容器中有默认大小(如 12)的字体,直接使用
   auto it = m_mapFonts.find(12);
   if (it != m_mapFonts.end()) {
      defaultFont = it->second;
   }
   else {
      // 创建默认字体
      defaultFont = new CFont();
      LOGFONT logFont = { 0 };
      _tcscpy_s(logFont.lfFaceName, _T("Segoe UI"));
      logFont.lfHeight = -12;
      logFont.lfQuality = CLEARTYPE_QUALITY;
      defaultFont->CreateFontIndirect(&logFont);
      m_mapFonts[12] = defaultFont; // 存储到字体管理容器
   }
   CFont* defaultFont = GetOrCreateFont(12);
   // 遍历所有控件,应用默认字体
   CWnd* pWnd = GetWindow(GW_CHILD);
@@ -385,41 +388,40 @@
   }
}
void CUserManagerDlg::AdjustControls(int nWidth, int nHeight)
void CUserManagerDlg::AdjustControls(float dScaleX, float dScaleY)
{
   CWnd* pWnd = GetWindow(GW_CHILD);
   while (pWnd) {
      UINT nCtrlID = pWnd->GetDlgCtrlID();
      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));
      CRect ctrlRect;
      pWnd->GetWindowRect(&ctrlRect);
      ScreenToClient(&ctrlRect);
         TCHAR szClassName[256];
         GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
      // 计算控件的新位置和大小,按比例调整
      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));
         if (_tcsicmp(szClassName, _T("ComboBox")) == 0) {
            CComboBox* pComboBox = (CComboBox*)pWnd;
            pComboBox->SetItemHeight(-1, newRect.Height());  // -1 表示所有项的高度
         }
      TCHAR szClassName[256];
      GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
         if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
            CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd;
            pGridCtrl->SetDefCellHeight(newRect.Height() / 21);
            pGridCtrl->ExpandColumnsToFit(TRUE);
            pGridCtrl->ExpandLastColumn();
            pGridCtrl->Invalidate();
            pGridCtrl->UpdateWindow();
         }
      if (_tcsicmp(szClassName, _T("ComboBox")) == 0) {
         CComboBox* pComboBox = (CComboBox*)pWnd;
         pComboBox->SetItemHeight(-1, nHeight);  // -1 表示所有项的高度
         pWnd->MoveWindow(&newRect);
         AdjustControlFont(pWnd, newRect.Width(), newRect.Height());
      }
      if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) {
         CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd;
         pGridCtrl->SetDefCellHeight(newHeight / 21);
         pGridCtrl->ExpandColumnsToFit(TRUE);
         pGridCtrl->Invalidate();
      }
      pWnd->MoveWindow(newX, newY, newWidth, newHeight);
      AdjustControlFont(pWnd, newWidth, newHeight);
      // 获取下一个控件
      pWnd = pWnd->GetNextWindow();
   }
}
@@ -465,6 +467,7 @@
   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()
@@ -480,12 +483,37 @@
   SetWindowText(_T("用户管理"));
   SystemParametersInfo(SPI_GETWORKAREA, 0, &screenRect, 0);
   // 设置默认字体
   SetDefaultFont();
   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;
@@ -509,17 +537,33 @@
   // 异常: OCX 属性页应返回 FALSE
}
void CUserManagerDlg::OnSize(UINT nType, int cx, int cy)
{
   CDialogEx::OnSize(nType, cx, cy);
   // TODO: 在此处添加消息处理程序代码
   CRect rect;
   GetClientRect(&rect);
   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(rect.Width(), rect.Height());
   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)
{
@@ -550,11 +594,13 @@
   *pResult = 0;
}
void CUserManagerDlg::OnBnClickedButtonAdd()
{
   // TODO: 在此添加控件通知处理程序代码
   AddRow(&m_gridUserManager);
}
void CUserManagerDlg::OnBnClickedButtonInsert()
{
@@ -619,12 +665,14 @@
   m_gridUserManager.UpdateWindow();
}
void CUserManagerDlg::OnBnClickedButtonDel()
{
   // TODO: 在此添加控件通知处理程序代码
   DeleteSelectedRow(&m_gridUserManager);
}
void CUserManagerDlg::OnBnClickedOk()
{
   // TODO: 在此添加控件通知处理程序代码