整理代码,修复win11设置效果崩溃的错误

This commit is contained in:
jeffrey0326 2024-08-21 22:50:43 +08:00
parent 893000e40f
commit 50b89e7eb2

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,50 +142,24 @@ 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(module && !pDwmSetWindowAttribute){
pDwmSetWindowAttribute = reinterpret_cast<DwmSetWindowAttributeFunc>(
GetProcAddress(module, "DwmSetWindowAttribute"));
if (!pDwmSetWindowAttribute) {
return false;
}
}
if (enable) { if (enable) {
pDwmExtendFrameIntoClientArea(hwnd, &extendedMargins); pDwmExtendFrameIntoClientArea(hwnd, &extendedMargins);
if (isWin1122H2OrGreater()) { if (isWin1122H2OrGreater()) {
@ -167,16 +184,9 @@ 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(module && !pDwmSetWindowAttribute){
pDwmSetWindowAttribute = reinterpret_cast<DwmSetWindowAttributeFunc>(
GetProcAddress(module, "DwmSetWindowAttribute"));
if (!pDwmSetWindowAttribute) {
return false;
}
}
if (enable) { if (enable) {
pDwmExtendFrameIntoClientArea(hwnd, &extendedMargins); pDwmExtendFrameIntoClientArea(hwnd, &extendedMargins);
const DWORD backdropType = _DWMSBT_TABBEDWINDOW; const DWORD backdropType = _DWMSBT_TABBEDWINDOW;
@ -191,19 +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(module && !pDwmSetWindowAttribute){
pDwmSetWindowAttribute = reinterpret_cast<DwmSetWindowAttributeFunc>(
GetProcAddress(module, "DwmSetWindowAttribute"));
if (!pDwmSetWindowAttribute) {
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 {
@ -216,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{};
@ -243,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 {
@ -268,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;
} }