Merge pull request #549 from jeffrey0326/main

整理dwmapi相关代码,修复Win11设置效果出错的bug
This commit is contained in:
zhuzichu 2024-08-22 00:44:45 +08:00 committed by GitHub
commit 3e28c42e1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -84,14 +84,57 @@ static inline QByteArray qtNativeEventType() {
return result; return result;
} }
static inline bool isCompositionEnabled() { static inline bool initializeFunctionPointers() {
HMODULE module = ::LoadLibraryW(L"dwmapi.dll"); HMODULE module = LoadLibraryW(L"dwmapi.dll");
if (module) { if (module) {
BOOL composition_enabled = false; if (!pDwmSetWindowAttribute) {
pDwmIsCompositionEnabled = reinterpret_cast<DwmIsCompositionEnabledFunc>(::GetProcAddress(module, "DwmIsCompositionEnabled")); pDwmSetWindowAttribute = reinterpret_cast<DwmSetWindowAttributeFunc>(
if (pDwmIsCompositionEnabled) { GetProcAddress(module, "DwmSetWindowAttribute"));
pDwmIsCompositionEnabled(&composition_enabled); if (!pDwmSetWindowAttribute) {
return false;
} }
}
if (!pDwmExtendFrameIntoClientArea) {
pDwmExtendFrameIntoClientArea = reinterpret_cast<DwmExtendFrameIntoClientAreaFunc>(
GetProcAddress(module, "DwmExtendFrameIntoClientArea"));
if (!pDwmExtendFrameIntoClientArea) {
return false;
}
}
if (!pDwmIsCompositionEnabled) {
pDwmIsCompositionEnabled = reinterpret_cast<DwmIsCompositionEnabledFunc>(
::GetProcAddress(module, "DwmIsCompositionEnabled"));
if (!pDwmIsCompositionEnabled) {
return false;
}
}
if (!pDwmEnableBlurBehindWindow) {
pDwmEnableBlurBehindWindow =
reinterpret_cast<DwmEnableBlurBehindWindowFunc>(
GetProcAddress(module, "DwmEnableBlurBehindWindow"));
if (!pDwmEnableBlurBehindWindow) {
return false;
}
}
if (!pSetWindowCompositionAttribute) {
HMODULE user32 = LoadLibraryW(L"user32.dll");
if (!user32) {
return false;
}
pSetWindowCompositionAttribute = reinterpret_cast<SetWindowCompositionAttributeFunc>(
GetProcAddress(user32, "SetWindowCompositionAttribute"));
if (!pSetWindowCompositionAttribute) {
return false;
}
}
}
return true;
}
static inline bool isCompositionEnabled() {
if(initializeFunctionPointers()){
BOOL composition_enabled = false;
pDwmIsCompositionEnabled(&composition_enabled);
return composition_enabled; return composition_enabled;
} }
return false; return false;
@ -99,41 +142,22 @@ static inline bool isCompositionEnabled() {
static inline void setShadow(HWND hwnd) { static inline void setShadow(HWND hwnd) {
const MARGINS shadow = {1, 0, 0, 0}; const MARGINS shadow = {1, 0, 0, 0};
HMODULE module = LoadLibraryW(L"dwmapi.dll"); if (initializeFunctionPointers()) {
if (module) {
pDwmExtendFrameIntoClientArea = reinterpret_cast<DwmExtendFrameIntoClientAreaFunc>(GetProcAddress(module, "DwmExtendFrameIntoClientArea"));
if (pDwmExtendFrameIntoClientArea) {
pDwmExtendFrameIntoClientArea(hwnd, &shadow); pDwmExtendFrameIntoClientArea(hwnd, &shadow);
} }
}
} }
static inline bool setWindowDarkMode(HWND hwnd, const BOOL enable) { static inline bool setWindowDarkMode(HWND hwnd, const BOOL enable) {
if (!pDwmSetWindowAttribute) { if (!initializeFunctionPointers()) {
HMODULE module = LoadLibraryW(L"dwmapi.dll");
if (module) {
pDwmSetWindowAttribute = reinterpret_cast<DwmSetWindowAttributeFunc>(
GetProcAddress(module, "DwmSetWindowAttribute"));
}
if (!pDwmSetWindowAttribute) {
return false; return false;
} }
}
return bool(pDwmSetWindowAttribute(hwnd, 20, &enable, sizeof(BOOL))); return bool(pDwmSetWindowAttribute(hwnd, 20, &enable, sizeof(BOOL)));
} }
static inline bool setWindowEffect(HWND hwnd, const QString &key, const bool &enable) { static inline bool setWindowEffect(HWND hwnd, const QString &key, const bool &enable) {
static constexpr const MARGINS extendedMargins = {-1, -1, -1, -1}; static constexpr const MARGINS extendedMargins = {-1, -1, -1, -1};
HMODULE module = LoadLibraryW(L"dwmapi.dll");
if (module) {
pDwmExtendFrameIntoClientArea = reinterpret_cast<DwmExtendFrameIntoClientAreaFunc>(
GetProcAddress(module, "DwmExtendFrameIntoClientArea"));
if (!pDwmExtendFrameIntoClientArea) {
return false;
}
}
if (key == QStringLiteral("mica")) { if (key == QStringLiteral("mica")) {
if (!isWin11OrGreater()) { if (!isWin11OrGreater() || !initializeFunctionPointers()) {
return false; return false;
} }
if (enable) { if (enable) {
@ -160,7 +184,7 @@ static inline bool setWindowEffect(HWND hwnd, const QString &key, const bool &en
} }
if (key == QStringLiteral("mica-alt")) { if (key == QStringLiteral("mica-alt")) {
if (!isWin1122H2OrGreater()) { if (!isWin1122H2OrGreater() || !initializeFunctionPointers()) {
return false; return false;
} }
if (enable) { if (enable) {
@ -177,12 +201,11 @@ static inline bool setWindowEffect(HWND hwnd, const QString &key, const bool &en
} }
if (key == QStringLiteral("acrylic")) { if (key == QStringLiteral("acrylic")) {
if (!isWin11OrGreater()) { if (!isWin11OrGreater() || !initializeFunctionPointers()) {
return false; return false;
} }
if (enable) { if (enable) {
MARGINS margins{-1, -1, -1, -1}; pDwmExtendFrameIntoClientArea(hwnd, &extendedMargins);
pDwmExtendFrameIntoClientArea(hwnd, &margins);
DWORD system_backdrop_type = _DWMSBT_TRANSIENTWINDOW; DWORD system_backdrop_type = _DWMSBT_TRANSIENTWINDOW;
pDwmSetWindowAttribute(hwnd, 38, &system_backdrop_type, sizeof(DWORD)); pDwmSetWindowAttribute(hwnd, 38, &system_backdrop_type, sizeof(DWORD));
} else { } else {
@ -195,19 +218,11 @@ static inline bool setWindowEffect(HWND hwnd, const QString &key, const bool &en
} }
if (key == QStringLiteral("dwm-blur")) { if (key == QStringLiteral("dwm-blur")) {
if (isWin7Only() && !isCompositionEnabled()) { if ((isWin7Only() && !isCompositionEnabled()) || !initializeFunctionPointers()) {
return false; return false;
} }
BOOL isDark = FluTheme::getInstance()->dark(); BOOL isDark = FluTheme::getInstance()->dark();
setWindowDarkMode(hwnd, isDark && enable); setWindowDarkMode(hwnd, isDark && enable);
if (isWin8OrGreater() && !pSetWindowCompositionAttribute) {
HMODULE module = LoadLibraryW(L"user32.dll");
pSetWindowCompositionAttribute = reinterpret_cast<SetWindowCompositionAttributeFunc>(
GetProcAddress(module, "SetWindowCompositionAttribute"));
if (!pSetWindowCompositionAttribute) {
return false;
}
}
if (enable) { if (enable) {
if (isWin8OrGreater()) { if (isWin8OrGreater()) {
ACCENT_POLICY policy{}; ACCENT_POLICY policy{};
@ -222,15 +237,6 @@ static inline bool setWindowEffect(HWND hwnd, const QString &key, const bool &en
DWM_BLURBEHIND bb{}; DWM_BLURBEHIND bb{};
bb.fEnable = TRUE; bb.fEnable = TRUE;
bb.dwFlags = DWM_BB_ENABLE; bb.dwFlags = DWM_BB_ENABLE;
if (!pDwmEnableBlurBehindWindow) {
HMODULE module = LoadLibraryW(L"user32.dll");
pDwmEnableBlurBehindWindow =
reinterpret_cast<DwmEnableBlurBehindWindowFunc>(
GetProcAddress(module, "DwmEnableBlurBehindWindowFunc"));
if (!pDwmEnableBlurBehindWindow) {
return false;
}
}
pDwmEnableBlurBehindWindow(hwnd, &bb); pDwmEnableBlurBehindWindow(hwnd, &bb);
} }
} else { } else {
@ -247,21 +253,11 @@ static inline bool setWindowEffect(HWND hwnd, const QString &key, const bool &en
DWM_BLURBEHIND bb{}; DWM_BLURBEHIND bb{};
bb.fEnable = FALSE; bb.fEnable = FALSE;
bb.dwFlags = DWM_BB_ENABLE; bb.dwFlags = DWM_BB_ENABLE;
if (!pDwmEnableBlurBehindWindow) {
HMODULE module = LoadLibraryW(L"user32.dll");
pDwmEnableBlurBehindWindow =
reinterpret_cast<DwmEnableBlurBehindWindowFunc>(
GetProcAddress(module, "DwmEnableBlurBehindWindowFunc"));
if (!pDwmEnableBlurBehindWindow) {
return false;
}
}
pDwmEnableBlurBehindWindow(hwnd, &bb); pDwmEnableBlurBehindWindow(hwnd, &bb);
} }
} }
return true; return true;
} }
return false; return false;
} }