This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThemeHelper.hpp
188 lines (163 loc) · 4.96 KB
/
ThemeHelper.hpp
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
#pragma once
#include "framework.h"
#include "wil.h"
namespace AcrylicEverywhere::ThemeHelper
{
inline HRESULT RefreshTheme()
{
static const auto s_actualSetSystemVisualStyle{ reinterpret_cast<HRESULT(WINAPI*)(LPCWSTR, LPCWSTR, LPCWSTR, int)>(GetProcAddress(GetModuleHandleW(L"UxTheme.dll"), MAKEINTRESOURCEA(65))) };
if (!s_actualSetSystemVisualStyle) [[unlikely]]
{
return E_FAIL;
}
WCHAR themeFileName[MAX_PATH + 1];
WCHAR colorScheme[MAX_PATH + 1];
WCHAR canonicalName[MAX_PATH + 1];
GetCurrentThemeName(themeFileName, MAX_PATH, colorScheme, MAX_PATH, canonicalName, MAX_PATH);
return s_actualSetSystemVisualStyle(themeFileName, colorScheme, canonicalName, 0);
}
static HRESULT DrawTextWithGlow(
HDC hdc,
LPCWSTR pszText,
int cchText,
LPRECT prc,
UINT dwFlags,
UINT crText,
UINT crGlow,
UINT nGlowRadius,
UINT nGlowIntensity,
BOOL bPreMultiply,
DTT_CALLBACK_PROC actualDrawTextCallback,
LPARAM lParam
)
{
static const auto s_actualDrawTextWithGlow{ reinterpret_cast<HRESULT(WINAPI*)(HDC, LPCWSTR, int, LPRECT, UINT, UINT, UINT, UINT, UINT, BOOL, DTT_CALLBACK_PROC, LPARAM)>(GetProcAddress(GetModuleHandle(TEXT("UxTheme")), MAKEINTRESOURCEA(126))) };
if (s_actualDrawTextWithGlow) [[likely]]
{
return s_actualDrawTextWithGlow(hdc, pszText, cchText, prc, dwFlags, crText, crGlow, nGlowRadius, nGlowIntensity, bPreMultiply, actualDrawTextCallback, lParam);
}
return E_FAIL;
}
inline HRESULT DrawThemeContent(
HDC hdc,
const RECT& paintRect,
LPCRECT clipRect,
LPCRECT excludeRect,
DWORD additionalFlags,
std::function<void(HDC, HPAINTBUFFER, RGBQUAD*, int)> callback,
std::byte alpha = std::byte{ 0xFF },
bool useBlt = false,
bool update = true
) try
{
BOOL updateTarget{ FALSE };
HDC memoryDC{ nullptr };
HPAINTBUFFER bufferedPaint{ nullptr };
BLENDFUNCTION blendFunction{ AC_SRC_OVER, 0, std::to_integer<BYTE>(alpha), AC_SRC_ALPHA };
BP_PAINTPARAMS paintParams{ sizeof(BP_PAINTPARAMS), BPPF_ERASE | additionalFlags, excludeRect, !useBlt ? &blendFunction : nullptr };
THROW_HR_IF_NULL(E_INVALIDARG, callback);
SaveDC(hdc);
auto restore = wil::scope_exit([hdc]{ RestoreDC(hdc, -1); });
{
auto cleanUp = wil::scope_exit([&]
{
if (bufferedPaint != nullptr)
{
EndBufferedPaint(bufferedPaint, updateTarget);
bufferedPaint = nullptr;
}
});
if (clipRect)
{
IntersectClipRect(hdc, clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
}
if (excludeRect)
{
ExcludeClipRect(hdc, excludeRect->left, excludeRect->top, excludeRect->right, excludeRect->bottom);
}
bufferedPaint = BeginBufferedPaint(hdc, &paintRect, BPBF_TOPDOWNDIB, &paintParams, &memoryDC);
THROW_HR_IF_NULL(E_FAIL, bufferedPaint);
{
auto selectedFont{ wil::SelectObject(memoryDC, GetCurrentObject(hdc, OBJ_FONT)) };
auto selectedBrush{ wil::SelectObject(memoryDC, GetCurrentObject(hdc, OBJ_BRUSH)) };
auto selectedPen{ wil::SelectObject(memoryDC, GetCurrentObject(hdc, OBJ_PEN)) };
int cxRow{ 0 };
RGBQUAD* buffer{ nullptr };
THROW_IF_FAILED(GetBufferedPaintBits(bufferedPaint, &buffer, &cxRow));
callback(memoryDC, bufferedPaint, buffer, cxRow);
}
updateTarget = TRUE;
}
return S_OK;
}
CATCH_RETURN()
static int DrawTextWithAlpha(
HDC hdc,
LPCWSTR lpchText,
int cchText,
LPRECT lprc,
UINT format,
decltype(&DrawTextW) drawTextProc
) try
{
struct DrawTextContext
{
int m_result{ 0 };
decltype(&DrawTextW) m_drawTextProc{ nullptr };
} context{ 0, drawTextProc };
auto drawTextCallback = [](HDC hdc, LPWSTR pszText, int cchText, LPRECT prc, UINT dwFlags, LPARAM lParam) -> int
{
return reinterpret_cast<DrawTextContext*>(lParam)->m_result = reinterpret_cast<DrawTextContext*>(lParam)->m_drawTextProc(hdc, pszText, cchText, prc, dwFlags);
};
auto callback = [&](HDC memoryDC, HPAINTBUFFER, RGBQUAD*, int)
{
THROW_IF_FAILED(
ThemeHelper::DrawTextWithGlow(
memoryDC,
lpchText,
cchText,
lprc,
format,
GetTextColor(hdc),
0,
0,
0,
TRUE,
drawTextCallback,
(LPARAM)&context
)
);
};
auto callbackAsFallback = [&](HDC memoryDC, HPAINTBUFFER, RGBQUAD*, int)
{
DTTOPTS options =
{
sizeof(DTTOPTS),
DTT_TEXTCOLOR | DTT_COMPOSITED | DTT_CALLBACK,
GetTextColor(hdc),
0,
0,
0,
{},
0,
0,
0,
0,
FALSE,
0,
drawTextCallback,
(LPARAM)&context
};
wil::unique_htheme hTheme{ OpenThemeData(nullptr, TEXT("Composited::Window")) };
THROW_HR_IF_NULL(E_FAIL, hTheme);
THROW_IF_FAILED(DrawThemeTextEx(hTheme.get(), memoryDC, 0, 0, lpchText, cchText, format, lprc, &options));
};
THROW_HR_IF_NULL(E_INVALIDARG, hdc);
if (FAILED(ThemeHelper::DrawThemeContent(hdc, *lprc, nullptr, nullptr, 0, callback))) [[unlikely]]
{
THROW_IF_FAILED(ThemeHelper::DrawThemeContent(hdc, *lprc, nullptr, nullptr, 0, callbackAsFallback));
}
return context.m_result;
}
catch(...) { return 0; }
}