LAPTOP-T815PCOQ\25526
2024-12-18 b8a705515cd307bd46e04eeccc0bf0e158133402
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
#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<int>(originalRect.left * dScaleX),
                static_cast<int>(originalRect.top * dScaleY),
                static_cast<int>(originalRect.right * dScaleX),
                static_cast<int>(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<float>(cx) / m_nInitialWidth;
    float dScaleY = static_cast<float>(cy) / m_nInitialHeight;
 
    // ±éÀú¶Ô»°¿òÖеÄËùÓпؼþ
    AdjustControls(dScaleX, dScaleY);
}
 
void CBaseDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
    // TODO: ÔÚ´ËÌí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂëºÍ/»òµ÷ÓÃĬÈÏÖµ
    lpMMI->ptMinTrackSize.x = 400; // ×îС¿í¶È
    lpMMI->ptMinTrackSize.y = 300; // ×îС¸ß¶È
 
    CDialogEx::OnGetMinMaxInfo(lpMMI);
}