Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
264 lines (237 loc) · 8.79 KB
/
Copy pathmain.cpp
File metadata and controls
264 lines (237 loc) · 8.79 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
#pragma comment(lib, "d2d1.lib")
#pragma comment(lib, "dwrite.lib")
#pragma comment(lib, "dwmapi.lib")
#include <windows.h>
#include <windowsx.h>
#include <dwmapi.h>
#include <d2d1.h>
#include <dwrite.h>
#include <commctrl.h>
#include <algorithm>
#ifndef DWMWA_WINDOW_CORNER_PREFERENCE
#define DWMWA_WINDOW_CORNER_PREFERENCE 33
#endif
using SetWindowCompositionAttributeFn = BOOL(WINAPI*)(HWND, void*);
struct AccentPolicy {
int accent_state;
int accent_flags;
unsigned int gradient_color;
int animation_id;
};
struct WindowCompositionAttribData {
int attribute;
void* data;
size_t size_of_data;
};
static D2D1_COLOR_F ToColor(COLORREF value, float alpha = 1.0f) {
return D2D1::ColorF(
static_cast<float>(GetRValue(value)) / 255.0f,
static_cast<float>(GetGValue(value)) / 255.0f,
static_cast<float>(GetBValue(value)) / 255.0f,
alpha);
}
static void TryEnableAcrylic(HWND hwnd) {
const HMODULE user32 = GetModuleHandleW(L"user32.dll");
if (user32 == nullptr) return;
const auto set_attribute = reinterpret_cast<SetWindowCompositionAttributeFn>(
GetProcAddress(user32, "SetWindowCompositionAttribute"));
if (set_attribute == nullptr) return;
AccentPolicy accent{};
accent.accent_state = 4;
accent.accent_flags = 2;
accent.gradient_color = 0x96F7F7FA;
WindowCompositionAttribData data{};
data.attribute = 19;
data.data = &accent;
data.size_of_data = sizeof(accent);
set_attribute(hwnd, &data);
}
static void ApplySystemBackdrop(HWND hwnd) {
const MARGINS margins{ -1, -1, -1, -1 };
DwmExtendFrameIntoClientArea(hwnd, &margins);
const DWORD corner_preference = 2; // DWMWCP_ROUND
DwmSetWindowAttribute(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &corner_preference, sizeof(corner_preference));
const DWORD border_color = 0xFFFFFFFE;
DwmSetWindowAttribute(hwnd, 34, &border_color, sizeof(border_color));
for (const int attribute : {38, 1029}) {
const int transient_window = 3;
if (SUCCEEDED(DwmSetWindowAttribute(hwnd, attribute, &transient_window, sizeof(transient_window))))
return;
}
TryEnableAcrylic(hwnd);
}
// --- Window state ---
static ID2D1Factory* g_factory = nullptr;
static ID2D1HwndRenderTarget* g_rt = nullptr;
static UINT g_dpi = 96;
static HWND g_hwnd = nullptr;
static float Scale(float v) { return v * static_cast<float>(g_dpi) / 96.0f; }
static int ScaleInt(float v) { return static_cast<int>(Scale(v) + 0.5f); }
static void Invalidate() {
if (g_hwnd) InvalidateRect(g_hwnd, nullptr, FALSE);
}
static bool EnsureDeviceResources() {
if (g_rt) return true;
if (!g_factory) return false;
RECT client{};
GetClientRect(g_hwnd, &client);
const D2D1_SIZE_U size = D2D1::SizeU(
static_cast<UINT>((std::max)(1L, client.right - client.left)),
static_cast<UINT>((std::max)(1L, client.bottom - client.top)));
const HRESULT hr = g_factory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED)),
D2D1::HwndRenderTargetProperties(g_hwnd, size),
&g_rt);
if (FAILED(hr)) return false;
g_rt->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
g_rt->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE);
return true;
}
static void Paint() {
if (!EnsureDeviceResources()) return;
g_rt->BeginDraw();
g_rt->Clear(ToColor(RGB(245, 247, 251), 0.18f));
RECT rc;
GetClientRect(g_hwnd, &rc);
float cx = rc.right / 2.0f;
float cy = rc.bottom / 2.0f;
RECT card;
card.left = static_cast<LONG>(cx - Scale(160));
card.top = static_cast<LONG>(cy - Scale(90));
card.right = static_cast<LONG>(cx + Scale(160));
card.bottom = static_cast<LONG>(cy + Scale(90));
const float r = Scale(14.0f);
// 五层极轻阴影
float sh_ex[] = {9,6,4,2,0};
float sh_ey[] = {8,5,3,2,1.5f};
float sh_alpha[] = {0.012f,0.018f,0.026f,0.034f,0.042f};
for (int i = 0; i < 5; i++) {
RECT sr;
sr.left = card.left - ScaleInt(sh_ex[i]);
sr.top = card.top - ScaleInt(sh_ey[i] * 0.5f);
sr.right = card.right + ScaleInt(sh_ex[i]);
sr.bottom = card.bottom + ScaleInt(sh_ey[i]);
ID2D1SolidColorBrush* b = nullptr;
g_rt->CreateSolidColorBrush(ToColor(RGB(15,23,42), sh_alpha[i]), &b);
g_rt->FillRoundedRectangle(
D2D1::RoundedRect(D2D1::RectF((float)sr.left,(float)sr.top,(float)sr.right,(float)sr.bottom),
r + Scale(sh_ex[i]), r + Scale(sh_ex[i])), b);
b->Release();
}
ID2D1SolidColorBrush* fill = nullptr;
ID2D1SolidColorBrush* border = nullptr;
g_rt->CreateSolidColorBrush(ToColor(RGB(255,255,255), 0.58f), &fill);
g_rt->CreateSolidColorBrush(ToColor(RGB(255,255,255), 0.22f), &border);
const auto rr = D2D1::RoundedRect(D2D1::RectF((float)card.left,(float)card.top,(float)card.right,(float)card.bottom), r, r);
g_rt->FillRoundedRectangle(rr, fill);
g_rt->DrawRoundedRectangle(rr, border, Scale(1.0f));
fill->Release();
border->Release();
const HRESULT hr = g_rt->EndDraw();
if (hr == D2DERR_RECREATE_TARGET) {
g_rt->Release();
g_rt = nullptr;
}
}
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch (msg) {
case WM_NCCALCSIZE:
if (wparam == TRUE) return 0;
break;
case WM_CREATE:
return 0;
case WM_NCHITTEST: {
POINT pt{ GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam) };
ScreenToClient(hwnd, &pt);
if (pt.y >= 0 && pt.y < ScaleInt(54.0f)) return HTCAPTION;
return HTCLIENT;
}
case WM_ERASEBKGND:
return 1;
case WM_PAINT: {
PAINTSTRUCT ps{};
BeginPaint(hwnd, &ps);
Paint();
EndPaint(hwnd, &ps);
return 0;
}
case WM_SIZE:
if (g_rt) g_rt->Resize(D2D1::SizeU(LOWORD(lparam), HIWORD(lparam)));
Invalidate();
return 0;
case WM_DPICHANGED: {
g_dpi = HIWORD(wparam);
const RECT* r = reinterpret_cast<const RECT*>(lparam);
SetWindowPos(hwnd, nullptr, r->left, r->top,
r->right - r->left, r->bottom - r->top,
SWP_NOACTIVATE | SWP_NOZORDER);
if (g_rt) { g_rt->Release(); g_rt = nullptr; }
Invalidate();
return 0;
}
case WM_DWMCOMPOSITIONCHANGED:
ApplySystemBackdrop(hwnd);
Invalidate();
return 0;
case WM_DESTROY:
if (g_rt) { g_rt->Release(); g_rt = nullptr; }
if (g_factory) { g_factory->Release(); g_factory = nullptr; }
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, msg, wparam, lparam);
}
static void EnableDpiAwareness() {
const auto user32 = GetModuleHandleW(L"user32.dll");
if (user32 != nullptr) {
const auto fn = reinterpret_cast<BOOL(WINAPI*)(DPI_AWARENESS_CONTEXT)>(
GetProcAddress(user32, "SetProcessDpiAwarenessContext"));
if (fn != nullptr) { fn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); return; }
}
SetProcessDPIAware();
}
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show_cmd) {
EnableDpiAwareness();
INITCOMMONCONTROLSEX icc{};
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_STANDARD_CLASSES;
InitCommonControlsEx(&icc);
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_factory);
WNDCLASSEXW wc{};
wc.cbSize = sizeof(wc);
wc.lpfnWndProc = WndProc;
wc.hInstance = instance;
wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wc.hbrBackground = nullptr;
wc.lpszClassName = L"GlassWindow";
RegisterClassExW(&wc);
g_dpi = []() -> UINT {
const auto user32 = GetModuleHandleW(L"user32.dll");
if (user32) {
const auto fn = reinterpret_cast<UINT(WINAPI*)()>(GetProcAddress(user32, "GetDpiForSystem"));
if (fn) return (std::max)(96u, fn());
}
return 96u;
}();
const int W = ScaleInt(600), H = ScaleInt(400);
const int x = (GetSystemMetrics(SM_CXSCREEN) - W) / 2;
const int y = (GetSystemMetrics(SM_CYSCREEN) - H) / 2;
g_hwnd = CreateWindowExW(
WS_EX_APPWINDOW,
L"GlassWindow", L"Glass",
WS_POPUP | WS_SYSMENU,
x, y, W, H,
nullptr, nullptr, instance, nullptr);
SetWindowPos(g_hwnd, nullptr, x, y, W, H, SWP_NOZORDER | SWP_FRAMECHANGED);
ApplySystemBackdrop(g_hwnd);
ShowWindow(g_hwnd, show_cmd == SW_HIDE ? SW_SHOWNORMAL : show_cmd);
UpdateWindow(g_hwnd);
MSG msg{};
while (GetMessageW(&msg, nullptr, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return static_cast<int>(msg.wParam);
}