| | |
| | | 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) |
| | |
| | | return false; |
| | | } |
| | | |
| | | void CUserManagerDlg::AdjustControls(int nWidth, int nHeight) |
| | | 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) { |
| | | 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("MFCGridCtrl")) == 0) { |
| | | CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd; |
| | | pGridCtrl->SetDefCellHeight(newHeight / 20); |
| | | pGridCtrl->ExpandColumnsToFit(TRUE); |
| | | pGridCtrl->Invalidate(); |
| | | pWnd->MoveWindow(&newRect); |
| | | AdjustControlFont(pWnd, newRect.Width(), newRect.Height()); |
| | | } |
| | | |
| | | pWnd->MoveWindow(newX, newY, newWidth, newHeight); |
| | | AdjustControlFont(pWnd, newWidth, newHeight); |
| | | |
| | | // 获取下一个控件 |
| | | pWnd = pWnd->GetNextWindow(); |
| | | } |
| | | } |
| | |
| | | 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; |
| | | } |
| | | |
| | | // 跳过特殊控件(如 MFCGridCtrl) |
| | | if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) { |
| | | return; |
| | | } |
| | | |
| | | int fontSize = nHeight - 10; |
| | | CFont* pCurrentFont = pWnd->GetFont(); |
| | | LOGFONT logFont; |
| | | pCurrentFont->GetLogFont(&logFont); |
| | | logFont.lfHeight = -fontSize; |
| | | // 根据控件高度动态调整字体大小 |
| | | int fontSize = nHeight / 2; |
| | | if (fontSize < 8) fontSize = 8; |
| | | |
| | | CFont newFont; |
| | | newFont.CreateFontIndirect(&logFont); |
| | | // 检查字体是否已经存在 |
| | | auto it = m_mapFonts.find(fontSize); |
| | | if (it == m_mapFonts.end()) { |
| | | // 动态创建新字体 |
| | | CFont* newFont = new CFont(); |
| | | LOGFONT logFont = { 0 }; |
| | | _tcscpy_s(logFont.lfFaceName, _T("Segoe UI")); |
| | | logFont.lfHeight = -fontSize; |
| | | logFont.lfQuality = CLEARTYPE_QUALITY; // 启用 ClearType 抗锯齿 |
| | | newFont->CreateFontIndirect(&logFont); |
| | | |
| | | pWnd->SetFont(&newFont); |
| | | pWnd->Invalidate(); |
| | | // 存储到字体管理容器中 |
| | | m_mapFonts[fontSize] = newFont; |
| | | it = m_mapFonts.find(fontSize); |
| | | } |
| | | |
| | | pWnd->SetFont(it->second); |
| | | pWnd->Invalidate(); // 刷新控件显示 |
| | | } |
| | | |
| | | BEGIN_MESSAGE_MAP(CUserManagerDlg, CDialogEx) |
| | |
| | | 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() |
| | | |
| | | |
| | |
| | | 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(); |
| | | dlgWidth = screenRect.Width(); |
| | | } |
| | | if (dlgHeight > screenRect.Height()) { |
| | | dlgHeight = screenRect.Height(); |
| | |
| | | // 异常: 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) |
| | | { |
| | |
| | | *pResult = 0; |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnBnClickedButtonAdd() |
| | | { |
| | | // TODO: 在此添加控件通知处理程序代码 |
| | | AddRow(&m_gridUserManager); |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnBnClickedButtonInsert() |
| | | { |
| | |
| | | m_gridUserManager.UpdateWindow(); |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnBnClickedButtonDel() |
| | | { |
| | | // TODO: 在此添加控件通知处理程序代码 |
| | | DeleteSelectedRow(&m_gridUserManager); |
| | | } |
| | | |
| | | |
| | | void CUserManagerDlg::OnBnClickedOk() |
| | | { |
| | | // TODO: 在此添加控件通知处理程序代码 |