-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_win11_backdrop.cpp
More file actions
82 lines (70 loc) · 2.65 KB
/
03_win11_backdrop.cpp
File metadata and controls
82 lines (70 loc) · 2.65 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
/*
Win11-specific API to affect the non-client style.
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT: {
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
}
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) DestroyWindow(hwnd);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"BasicWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(255, 0, 0));
RegisterClass(&wc);
// https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles
constexpr DWORD dwStyle = WS_OVERLAPPEDWINDOW;
// https://learn.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles
constexpr DWORD dwExStyle = 0;
RECT rect = {0, 0, 300, 300};
AdjustWindowRect(&rect, dwStyle, FALSE);
HWND hwnd = CreateWindowEx(
dwExStyle, CLASS_NAME, L"Basic Window",
dwStyle,
100, 100, rect.right - rect.left, rect.bottom - rect.top,
nullptr, nullptr, hInstance, nullptr
);
if (!hwnd) {
return 1;
}
// Optional (helps on Win11): immersive dark mode attribute.
// 20 = DWMWA_USE_IMMERSIVE_DARK_MODE (older), 19 on some builds.
// Not required for backdrop; safe to ignore if it fails.
constexpr DWORD DWMWA_USE_IMMERSIVE_DARK_MODE_20 = 20;
constexpr BOOL darkmodeSet = TRUE;
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE_20, &darkmodeSet, sizeof(darkmodeSet));
// Values:
// DWMSBT_AUTO (Auto heuristic)
// DWMSBT_NONE (Disable)
// DWMSBT_MAINWINDOW (System theme Mica)
// DWMSBT_TRANSIENTWINDOW (Acrylic-like)
// DWMSBT_TABBEDWINDOW (System theme Mica Alt)
constexpr DWM_SYSTEMBACKDROP_TYPE backdropType = DWMSBT_AUTO;
DwmSetWindowAttribute(hwnd, DWMWA_SYSTEMBACKDROP_TYPE, &backdropType, sizeof(backdropType));
ShowWindow(hwnd, nCmdShow);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}