馋了Java Swing的Flatlaf Darkmode样式的标题栏好久了x

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
typedef HRESULT (WINAPI* DwmSetWindowAttribute)(HWND, DWORD, LPCVOID, DWORD);
typedef HRESULT (WINAPI* SetWindowTheme)(HWND, const wchar_t*, const wchar_t*);

static DwmSetWindowAttribute pDwmSetWindowAttribute = 0;
static SetWindowTheme pSetWindowTheme = 0;

void setDarkTitle() {
HMODULE dwm = LoadLibraryA("dwmapi.dll");
HMODULE uxtheme = LoadLibraryA("uxtheme.dll");
if (dwm && uxtheme) {
pDwmSetWindowAttribute = reinterpret_cast<DwmSetWindowAttribute>(GetProcAddress(dwm, "DwmSetWindowAttribute"));
pSetWindowTheme = reinterpret_cast<SetWindowTheme>(GetProcAddress(uxtheme, "SetWindowTheme"));
if (pDwmSetWindowAttribute && pSetWindowTheme) {
HWND w = GetConsoleWindow();

pSetWindowTheme(w, L"DarkMode_Explorer", NULL);

BOOL dark = 1;
if (S_OK != pDwmSetWindowAttribute(w, 20, &dark, sizeof dark)) {
pDwmSetWindowAttribute(w, 19, &dark, sizeof dark);
}

// force dwm redraw title
RECT rect = { NULL };
if (GetWindowRect(w, &rect)) {
SetWindowPos(w, NULL, rect.left, rect.top, rect.right - rect.left - 1, rect.bottom - rect.top,
SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
SetWindowPos(w, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
}

}
}
}