-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGDIWindow.cpp
More file actions
89 lines (79 loc) · 1.74 KB
/
GDIWindow.cpp
File metadata and controls
89 lines (79 loc) · 1.74 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
#include "GDIWindow.h"
#include <Windows.h>
#include <winrt/base.h>
void GDIWindow::recreateFont()
{
m_font.reset(CreateFontW(
m_fontSize * GetDpi() / 96.0,
0,
0,
0,
FW_REGULAR,
false,
false,
false,
DEFAULT_CHARSET,
OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
VARIABLE_PITCH,
m_fontFamily.data()
));
}
GDIWindow::GDIWindow(winrt::hstring const& text) :
m_backgroundBrush{reinterpret_cast<HBRUSH>(GetStockObject(WHITE_BRUSH))},
m_text{text},
BaseWindow{ L"GDI window" }
{
recreateFont();
}
void GDIWindow::OnPaint(HWND hwnd)
{
PAINTSTRUCT ps;
auto hdc = BeginPaint(hwnd, &ps);
auto self = getSelfFromHwnd(hwnd);
auto oldFont = reinterpret_cast<HFONT>(SelectObject(hdc, self->m_font.get()));
SetTextColor(hdc, RGB(0, 0, 0));
FillRect(hdc, &ps.rcPaint, self->m_backgroundBrush);
DrawText(hdc, self->m_text.data(), -1, &ps.rcPaint, DT_LEFT | DT_WORDBREAK);
SelectObject(hdc, oldFont);
EndPaint(hwnd, &ps);
}
void GDIWindow::OnDpiChanged(HWND hwnd, WORD dpiX, WORD dpiY, RECT* suggestPosition)
{
auto self = getSelfFromHwnd(hwnd);
self->m_font.reset(
CreateFontW(
self->m_fontSize * dpiX / 96.0,
0,
0,
0,
FW_REGULAR,
false,
false,
false,
DEFAULT_CHARSET,
OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY,
VARIABLE_PITCH,
self->m_fontFamily.data()
));
}
void GDIWindow::SetText(winrt::hstring const& text)
{
m_text = text.data();
InvalidateRect(m_hwnd.get(), nullptr, true);
}
void GDIWindow::SetFontSize(int fontSize)
{
m_fontSize = fontSize * 1.5;
recreateFont();
InvalidateRect(m_hwnd.get(), nullptr, true);
}
void GDIWindow::SetFontFamily(winrt::hstring const& fontFamily)
{
m_fontFamily = fontFamily;
recreateFont();
InvalidateRect(m_hwnd.get(), nullptr, true);
}