From 226bc52ece48f6057e22c726442decc09af3539b Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Sat, 17 Oct 2020 17:29:58 +0800 Subject: [PATCH] Fix the acrylic effect. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/Win32Demo/widget.cpp | 7 +------ winnativeeventfilter.cpp | 16 +++++++++++++++- winnativeeventfilter.h | 5 ++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/examples/Win32Demo/widget.cpp b/examples/Win32Demo/widget.cpp index 8ab5768..4ae45a1 100644 --- a/examples/Win32Demo/widget.cpp +++ b/examples/Win32Demo/widget.cpp @@ -96,12 +96,7 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) updateWindow(this); }); connect(ui->blurEffectCB, &QCheckBox::stateChanged, this, [this](int state) { - const bool enable = state == Qt::Checked; - QPalette palette = {}; - // Choose the gradient color you like. - palette.setColor(QPalette::Window, QColor(255, 255, 255, 220)); - setPalette(enable ? palette : QPalette()); - WinNativeEventFilter::setBlurEffectEnabled(getRawHandle(this), enable); + WinNativeEventFilter::setBlurEffectEnabled(getRawHandle(this), state == Qt::Checked); updateWindow(this); }); connect(ui->resizableCB, &QCheckBox::stateChanged, this, [this](int state) { diff --git a/winnativeeventfilter.cpp b/winnativeeventfilter.cpp index 7ebcb03..e785bae 100644 --- a/winnativeeventfilter.cpp +++ b/winnativeeventfilter.cpp @@ -2357,14 +2357,28 @@ bool WinNativeEventFilter::displaySystemMenu(void *handle, return false; } -bool WinNativeEventFilter::setBlurEffectEnabled(void *handle, const bool enabled) +bool WinNativeEventFilter::setBlurEffectEnabled(void *handle, + const bool enabled, + const QColor &gradientColor) { Q_ASSERT(handle); const auto hwnd = reinterpret_cast(handle); if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, hwnd)) { +#ifdef QT_WIDGETS_LIB + // Is it possible to set a palette to a QWindow? + QWidget *widget = QWidget::find(reinterpret_cast(hwnd)); + if (widget && widget->isTopLevel()) { + // Qt will paint a solid white background to the window, + // it will cover the blurred effect, so we need to + // make the background become totally transparent. Achieve + // this by setting a palette to the window. + widget->setPalette(enabled ? QPalette(QColor(0, 0, 0, 0)) : QPalette()); + } +#endif if (isWin8OrGreater() && coreData()->m_lpSetWindowCompositionAttribute) { ACCENT_POLICY accentPolicy; SecureZeroMemory(&accentPolicy, sizeof(accentPolicy)); + accentPolicy.GradientColor = gradientColor.rgba(); WINDOWCOMPOSITIONATTRIBDATA wcaData; SecureZeroMemory(&wcaData, sizeof(wcaData)); wcaData.Attrib = WCA_ACCENT_POLICY; diff --git a/winnativeeventfilter.h b/winnativeeventfilter.h index 98680b8..96c4e84 100644 --- a/winnativeeventfilter.h +++ b/winnativeeventfilter.h @@ -26,6 +26,7 @@ #include "framelesshelper_global.h" #include +#include #include #include @@ -141,7 +142,9 @@ public: // Enable or disable the blur effect for a specific window. // On Win10 it's the Acrylic effect. - static bool setBlurEffectEnabled(void *handle /* HWND */, const bool enabled = true); + static bool setBlurEffectEnabled(void *handle /* HWND */, + const bool enabled = true, + const QColor &gradientColor = QColor(255, 255, 255, 0)); // Thin wrapper of DwmExtendFrameIntoClientArea(). static void updateFrameMargins(void *handle /* HWND */);