Repository navigation
Expand file tree
/
Copy pathframework_base.cpp
More file actions
409 lines (331 loc) · 12 KB
/
Copy pathframework_base.cpp
File metadata and controls
409 lines (331 loc) · 12 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "framework_base.hpp"
#include <iomanip>
#include <sstream>
namespace Framework {
Point::Point() : x(0), y(0) {}
Point::Point(int x, int y) : x(x), y(y) {}
Point Point::move(int dx, int dy) const { return Point(x + dx, y + dy); }
#ifdef OS_WINDOWS
Point::Point(const POINT& pt) : x(pt.x), y(pt.y) {}
Point::operator POINT() const { return POINT{ x, y }; }
#endif
Size::Size() : w(0), h(0) {}
Size::Size(int w, int h) : w(w), h(h) {}
Size Size::expand(int dw, int dh) const { return Size(w + dw, h + dh); }
Size Size::expand(int d) const { return Size(w + d, h + d); }
Size Size::shrink(int dw, int dh) const { return Size(w - dw, h - dh); }
Size Size::shrink(int d) const { return Size(w - d, h - d); }
#ifdef OS_WINDOWS
Size::Size(const SIZE& sz) : w(sz.cx), h(sz.cy) {}
Size::operator SIZE() const { return SIZE{ w, h }; }
#endif
Rect::Rect() : x(0), y(0), w(0), h(0) {}
Rect::Rect(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
Rect::Rect(Point pos, Size size) : x(pos.x), y(pos.y), w(size.w), h(size.h) {}
Rect Rect::expand(int dw, int dh) const { return Rect(x, y, w + dw, h + dh); }
Rect Rect::expand(int d) const { return Rect(x - d, y - d, w + 2 * d, h + 2 * d); }
Rect Rect::shrink(int dw, int dh) const { return Rect(x + dw, y + dh, w - 2 * dw, h - 2 * dh); }
Rect Rect::shrink(int d) const { return Rect(x + d, y + d, w - 2 * d, h - 2 * d); }
bool Rect::contains(Point pt) const { return pt.x >= x && pt.x < x + w && pt.y >= y && pt.y < y + h; }
#ifdef OS_WINDOWS
Rect::Rect(const RECT& rc) : x(rc.left), y(rc.top), w(rc.right - rc.left), h(rc.bottom - rc.top) {}
Rect::operator RECT() const { return RECT{ x, y, x + w, y + h }; }
#endif
Point Rect::position() const { return Point(x, y); }
Size Rect::size() const { return Size(w, h); }
Font::Font(uint32_t dpi, int baseHeight)
: m_hFont(NULL), ownResource(false)
{
recreateFont(dpi, baseHeight);
}
Font::Font(HFONT hFont, bool ownResource)
: m_hFont(hFont), ownResource(ownResource)
{}
Font::~Font()
{
if (ownResource && m_hFont != NULL) {
DeleteObject(m_hFont);
}
}
Font::Font(Font &&other) noexcept
: m_hFont(other.m_hFont), ownResource(other.ownResource)
{
other.m_hFont = NULL;
other.ownResource = false;
}
Font& Font::operator=(Font &&other) noexcept
{
if (this != &other) {
if (ownResource && m_hFont != NULL) {
DeleteObject(m_hFont);
}
m_hFont = other.m_hFont;
ownResource = other.ownResource;
other.m_hFont = NULL;
other.ownResource = false;
}
return *this;
}
HFONT Font::hFont() const
{
return m_hFont;
}
void Font::recreateFont(uint32_t dpi, int baseHeight)
{
// 先释放当前拥有的字体
if (ownResource && m_hFont != NULL) {
DeleteObject(m_hFont);
}
// 实测结论:负的 lfHeight 会被 GDI 按 DC 的 DPI 再缩放,与 MulDiv 叠加成双重缩放(19*1.5*1.5);
// 正的 lfHeight 不会。所以用正的 MulDiv,跨 DPI 重建时才能正确跟随。
int physicalHeight = ::MulDiv(baseHeight, dpi, 96);
LOGFONTA lf = {0};
lf.lfHeight = physicalHeight;
lf.lfWeight = FW_NORMAL;
lf.lfQuality = CLEARTYPE_QUALITY;
lf.lfCharSet = DEFAULT_CHARSET;
strncpy_s(lf.lfFaceName, "Segoe UI", LF_FACESIZE - 1);
m_hFont = CreateFontIndirectA(&lf);
ownResource = true;
}
// the global font resource
// We do need a default constructor for ChildWindow,
// since we need to create an child window array.
// Luckily, this class does not own any resources (not even the HWND),
// so no special handling is needed for move/copy constructors or assignment operators.
ChildWindow::ChildWindow()
: m_geometry(0, 0, 0, 0), m_dpi(96), m_cID(0), m_hwnd(NULL)
{}
ChildWindow::ChildWindow(const std::string& className, const std::string& windowName, DWORD exStyle, DWORD style,
Geometry geometry, HWND parent, uintptr_t controlID, LPVOID lpParam)
: m_geometry(geometry), m_cID(controlID), m_dpi(96)
{
// We do not handle the case where dpi is not 96 here.
// It will be applied later by the main window after object creation.
m_hwnd = CreateWindowExA(exStyle,
className.c_str(), windowName.c_str(), WS_CHILD | WS_VISIBLE | style,
Rect_unwrap_plain(m_geometry), parent,
(HMENU)controlID, GetModuleHandle(NULL), lpParam
);
if (m_hwnd == NULL) {
throw std::runtime_error("Failed to create child window.");
}
}
void ChildWindow::move(Geometry newGeometry)
{
m_geometry = newGeometry;
refreshPosition();
}
void ChildWindow::applyScale(UINT dpi)
{
m_dpi = dpi;
refreshPosition();
}
void ChildWindow::applyFont(const Font &font)
{
sendMessage(WM_SETFONT, WPARAM(font.hFont()), TRUE);
}
HWND ChildWindow::hwnd() const
{
return m_hwnd;
}
LRESULT ChildWindow::sendMessage(UINT Msg, WPARAM wParam, LPARAM lParam) const
{
return SendMessageA(m_hwnd, Msg, wParam, lParam);
}
bool ChildWindow::setText(const std::string &text)
{
return SetWindowTextA(m_hwnd, text.c_str());
}
std::string ChildWindow::getText()
{
int len = GetWindowTextLengthA(m_hwnd);
if (len <= 0) return std::string();
std::string result(len + 1, '\0'); // len 个字符 + 结尾 null
GetWindowTextA(m_hwnd, &result[0], len + 1);
result.resize(len); // 去掉结尾 null
return result;
}
bool ChildWindow::setEnabled(bool enabled)
{
return EnableWindow(m_hwnd, enabled);
}
void ChildWindow::setReadOnly(bool readonly)
{
SendMessage(m_hwnd, EM_SETREADONLY, readonly, 0);
}
void ChildWindow::setChecked(bool checked)
{
SendMessageA(m_hwnd, BM_SETCHECK, checked ? BST_CHECKED : BST_UNCHECKED, 0);
}
bool ChildWindow::isChecked()
{
return SendMessageA(m_hwnd, BM_GETCHECK, 0, 0) == BST_CHECKED;
}
void ChildWindow::statusSetPartText(int partIndex, const std::string &text)
{
SendMessageA(m_hwnd, SB_SETTEXT, partIndex, (LPARAM)text.c_str());
}
ChildWindow ChildWindow::createStatic(const std::string &text, Geometry geometry, HWND parent, uintptr_t controlID, DWORD extraStyle)
{
return ChildWindow("Static", text.c_str(), 0, extraStyle, geometry, parent, controlID);
}
ChildWindow ChildWindow::createEdit(const std::string &text, Geometry geometry, HWND parent, uintptr_t controlID, bool readOnly, DWORD extraStyle)
{
DWORD style = WS_BORDER | WS_TABSTOP | ES_LEFT;
if (readOnly) style |= ES_READONLY;
style |= extraStyle;
return ChildWindow("Edit", text.c_str(), 0, style, geometry, parent, controlID);
}
ChildWindow ChildWindow::createButton(const std::string &text, Geometry geometry, HWND parent, uintptr_t controlID)
{
return ChildWindow("Button", text.c_str(), 0, WS_TABSTOP | BS_FLAT, geometry, parent, controlID);
}
ChildWindow ChildWindow::createCheckBox(const std::string &text, Geometry geometry, HWND parent, uintptr_t controlID)
{
return ChildWindow("Button", text.c_str(), 0, WS_TABSTOP| BS_AUTOCHECKBOX, geometry, parent, controlID);
}
ChildWindow ChildWindow::createStatusBar(HWND parent, uintptr_t controlID, std::vector<int> partWidths, bool sizeGrip)
{
DWORD style = 0;
if (sizeGrip) style |= SBARS_SIZEGRIP;
ChildWindow bar("msctls_statusbar32", "", 0, style, { 0, 0, 0, 0 }, parent, controlID);
bar.spec.noResize = true; // Status bar should not be resized by the framework, it will be managed by the parent window.
// input part widths is the widths, but the SendMessage SB_SETPARTS expects an array of right edges.
// we need to convert the widths to right edges.
bar.partWidths = partWidths;
bar.refreshStatusBarParts();
return bar;
}
ChildWindow ChildWindow::fromHWND(HWND hwnd, Geometry geometry, uintptr_t controlID)
{
ChildWindow cw;
cw.m_hwnd = hwnd;
cw.m_geometry = geometry;
cw.m_cID = controlID;
return cw;
}
void ChildWindow::refreshPosition()
{
// 状态栏也要按当前 DPI 刷新分栏,所以这部分放在 noResize 检查之前。
// (noResize 的状态栏不参与 MoveWindow,但分栏宽度仍需跟随 DPI。)
if (!partWidths.empty()) {
refreshStatusBarParts();
}
if (spec.noResize) return;
Rect scaled = m_geometry.scale(m_dpi);
MoveWindow(m_hwnd, scaled.x, scaled.y, scaled.w, scaled.h, TRUE);
}
void ChildWindow::refreshStatusBarParts()
{
if (partWidths.empty())
throw std::runtime_error("ChildWindow::refreshStatusBarParts: partWidths is not set.");
// 分栏宽度按当前 DPI 缩放(MulDiv 最近取整)。
// 负数宽度(-1)表示自动延伸到状态栏右缘,SB_SETPARTS 的右边界直接传 -1,不参与缩放;
// 通常 -1 应作为最后一个分栏。
std::vector<int> scaledPartWidths = partWidths;
for (int& width : scaledPartWidths) {
if (width < 0) continue;
width = ::MulDiv(width, m_dpi, 96);
}
int numParts = static_cast<int>(partWidths.size());
std::vector<int> rightEdges(numParts);
int currentEdge = 0;
for (int i = 0; i < numParts; ++i) {
if (scaledPartWidths[i] < 0) {
rightEdges[i] = -1; // 自动延伸(通常为最后一个分栏)
} else {
currentEdge += scaledPartWidths[i];
rightEdges[i] = currentEdge;
}
}
sendMessage(SB_SETPARTS, numParts, (LPARAM)rightEdges.data());
}
void ChildWindowStorage::addWindow(uintptr_t controlID, ChildWindow window)
{
if (controlID == 0) {
m_noID_windows.push_back(window);
return;
}
m_windows[controlID] = window;
}
void ChildWindowStorage::removeWindow(uintptr_t controlID)
{
if (controlID == 0) throw std::invalid_argument("Cannot remove window with controlID 0 from storage.");
m_windows.erase(controlID);
}
ChildWindow& ChildWindowStorage::getWindow(uintptr_t controlID)
{
if (controlID == 0) throw std::invalid_argument("Cannot get window with controlID 0 from storage.");
return m_windows.at(controlID); // 键不存在时抛出 std::out_of_range,避免静默创建空控件
}
void ChildWindowStorage::addStatic(const std::string &text, Geometry geometry, HWND parent, uintptr_t controlID, DWORD extraStyle)
{
addWindow(controlID, ChildWindow::createStatic(text, geometry, parent, controlID, extraStyle));
}
void ChildWindowStorage::addEdit(const std::string& text, Geometry geometry, HWND parent, uintptr_t controlID, bool readOnly, DWORD extraStyle)
{
addWindow(controlID, ChildWindow::createEdit(text, geometry, parent, controlID, readOnly, extraStyle));
}
void ChildWindowStorage::addButton(const std::string& text, Geometry geometry, HWND parent, uintptr_t controlID)
{
addWindow(controlID, ChildWindow::createButton(text, geometry, parent, controlID));
}
void ChildWindowStorage::addCheckBox(const std::string& text, Geometry geometry, HWND parent, uintptr_t controlID)
{
addWindow(controlID, ChildWindow::createCheckBox(text, geometry, parent, controlID));
}
void ChildWindowStorage::addStatusBar(HWND parent, uintptr_t controlID, std::vector<int> partWidths, bool sizeGrip)
{
addWindow(controlID, ChildWindow::createStatusBar(parent, controlID, partWidths, sizeGrip));
}
void ChildWindowStorage::applyScale(UINT dpi)
{
for(auto& [cid, w] : m_windows) w.applyScale(dpi);
for(auto& w : m_noID_windows) w.applyScale(dpi);
}
void ChildWindowStorage::applyFont(const Font &font)
{
for(auto& [cid, w] : m_windows) w.applyFont(font);
for(auto& w : m_noID_windows) w.applyFont(font);
}
void batchApplyFont(ChildWindow *windows, size_t count, Font& hFont)
{
for (size_t i = 0; i < count; ++i) {
windows[i].applyFont(hFont);
}
}
void batchApplyScale(ChildWindow *windows, size_t count, UINT dpi)
{
// Apply the DPI to all child windows in the array
for (size_t i = 0; i < count; ++i) {
windows[i].applyScale(dpi);
}
}
std::string hwndToHex(HWND hwnd, bool uppercase, bool special)
{
std::ostringstream oss;
oss << "0x"
<< std::hex;
if (uppercase)
oss << std::uppercase; // 让 A-F 大写(不加则输出小写 a-f)
oss << std::setfill('0')
<< std::setw(8)
<< reinterpret_cast<uintptr_t>(hwnd);
return oss.str();
}
double getScalingFactor(HWND hwnd)
{
return (double)GetDpiForWindow(hwnd) / 96.0;
}
UINT getDpi(HWND hwnd)
{
return GetDpiForWindow(hwnd);
}
Rect getWindowRect(HWND hwnd)
{
RECT rect;
GetWindowRect(hwnd, &rect);
return Rect(rect);
}
} // namespace Framework