#include "stdafx.h" #include "CBaseDlg.h" #include "GridCtrl.h" IMPLEMENT_DYNAMIC(CBaseDlg, CDialogEx) CBaseDlg::CBaseDlg(UINT id, CWnd* pPage) : CDialogEx(id, pPage) { m_nInitialWidth = 0; m_nInitialHeight = 0; } CBaseDlg::~CBaseDlg() { for (auto& pair : m_mapFonts) { if (pair.second) { pair.second->DeleteObject(); delete pair.second; } } m_mapFonts.clear(); } CFont* CBaseDlg::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 CBaseDlg::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 CBaseDlg::AdjustControls(float dScaleX, float dScaleY) { CWnd* pWnd = GetWindow(GW_CHILD); while (pWnd) { int nCtrlID = pWnd->GetDlgCtrlID(); if (nCtrlID != -1 && m_mapCtrlLayouts.find(nCtrlID) != m_mapCtrlLayouts.end()) { CRect originalRect = m_mapCtrlLayouts[nCtrlID]; CRect newRect( static_cast(originalRect.left * dScaleX), static_cast(originalRect.top * dScaleY), static_cast(originalRect.right * dScaleX), static_cast(originalRect.bottom * dScaleY)); TCHAR szClassName[256]; GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName)); if (_tcsicmp(szClassName, _T("ComboBox")) == 0) { CComboBox* pComboBox = (CComboBox*)pWnd; pComboBox->SetItemHeight(-1, newRect.Height()); // -1 ±íʾËùÓÐÏîµÄ¸ß¶È } if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) { CGridCtrl* pGridCtrl = (CGridCtrl*)pWnd; pGridCtrl->SetDefCellHeight(newRect.Height() / 21); pGridCtrl->ExpandColumnsToFit(TRUE); pGridCtrl->ExpandLastColumn(); pGridCtrl->Invalidate(); pGridCtrl->UpdateWindow(); } pWnd->MoveWindow(&newRect); AdjustControlFont(pWnd, newRect.Width(), newRect.Height()); } pWnd = pWnd->GetNextWindow(); } } void CBaseDlg::AdjustControlFont(CWnd* pWnd, int nWidth, int nHeight) { TCHAR szClassName[256]; GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName)); // Ìø¹ýÌØÊâ¿Ø¼þ£¨Èç MFCGridCtrl£© if (_tcsicmp(szClassName, _T("MFCGridCtrl")) == 0) { return; } // ¸ù¾Ý¿Ø¼þ¸ß¶È¶¯Ì¬µ÷Õû×ÖÌå´óС int fontSize = nHeight / 2; if (fontSize < 8) fontSize = 8; if (fontSize > 32) fontSize = 32; // »ñÈ¡»ò´´½¨×ÖÌå CFont* pFont = GetOrCreateFont(fontSize); pWnd->SetFont(pFont); pWnd->Invalidate(); // ˢпؼþÏÔʾ } BEGIN_MESSAGE_MAP(CBaseDlg, CDialogEx) ON_WM_SIZE() ON_WM_GETMINMAXINFO() END_MESSAGE_MAP() BOOL CBaseDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // TODO: ÔÚ´ËÌí¼Ó¶îÍâµÄ³õʼ»¯ CRect screenRect, dlgRect, clientRect; SystemParametersInfo(SPI_GETWORKAREA, 0, &screenRect, 0); 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() * 2; int dlgHeight = dlgRect.Height() * 2; if (dlgWidth > screenRect.Width()) { dlgWidth = screenRect.Width(); } if (dlgHeight > screenRect.Height()) { dlgHeight = screenRect.Height(); } int centerX = screenRect.left + (screenRect.Width() - dlgWidth) / 2; int centerY = screenRect.top + (screenRect.Height() - dlgHeight) / 2; MoveWindow(centerX, centerY, dlgWidth, dlgHeight); return TRUE; // return TRUE unless you set the focus to a control // Òì³£: OCX ÊôÐÔÒ³Ó¦·µ»Ø FALSE } void CBaseDlg::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); // TODO: ÔÚ´Ë´¦Ìí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂë if (nType == SIZE_MINIMIZED || m_mapCtrlLayouts.empty()) { return; } float dScaleX = static_cast(cx) / m_nInitialWidth; float dScaleY = static_cast(cy) / m_nInitialHeight; // ±éÀú¶Ô»°¿òÖеÄËùÓпؼþ AdjustControls(dScaleX, dScaleY); } void CBaseDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI) { // TODO: ÔÚ´ËÌí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂëºÍ/»òµ÷ÓÃĬÈÏÖµ lpMMI->ptMinTrackSize.x = 400; // ×îС¿í¶È lpMMI->ptMinTrackSize.y = 300; // ×îС¸ß¶È CDialogEx::OnGetMinMaxInfo(lpMMI); }