From ef34a7c699565eb7d4140060c9a95c4a397f588a Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Thu, 1 Apr 2021 09:52:38 +0800 Subject: [PATCH] Minor fixes 1. Fix MainWindow example issue 2. Add a comment about the inverted argument 3. Fix some memory leaks Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/widget/widget.cpp | 2 +- qtacryliceffecthelper_win32.cpp | 5 ++++- qtacrylicmainwindow.cpp | 1 + qtacrylicwidget.cpp | 1 + utilities.cpp | 2 ++ utilities_win32.cpp | 16 ++++++++++++---- 6 files changed, 21 insertions(+), 6 deletions(-) diff --git a/examples/widget/widget.cpp b/examples/widget/widget.cpp index b748bd7..021bf2c 100644 --- a/examples/widget/widget.cpp +++ b/examples/widget/widget.cpp @@ -34,6 +34,7 @@ Widget::Widget(QWidget *parent) : QtAcrylicWidget(parent) { createWinId(); + setAcrylicEnabled(true); setupUi(); startTimer(500); } @@ -54,7 +55,6 @@ void Widget::showEvent(QShowEvent *event) static bool inited = false; if (!inited) { FramelessWindowsManager::addWindow(windowHandle()); - setAcrylicEnabled(true); inited = true; } } diff --git a/qtacryliceffecthelper_win32.cpp b/qtacryliceffecthelper_win32.cpp index de3f40d..8e093c4 100644 --- a/qtacryliceffecthelper_win32.cpp +++ b/qtacryliceffecthelper_win32.cpp @@ -106,10 +106,13 @@ bool QtAcrylicWinEventFilter::nativeEventFilter(const QByteArray &eventType, voi shouldUpdate = true; } } break; + case WM_DPICHANGED: { + shouldClearWallpaper = true; + shouldUpdate = true; + } break; case WM_THEMECHANGED: case WM_DWMCOMPOSITIONCHANGED: case WM_DWMCOLORIZATIONCOLORCHANGED: - case WM_DPICHANGED: shouldUpdate = true; break; default : diff --git a/qtacrylicmainwindow.cpp b/qtacrylicmainwindow.cpp index 6386fd2..c08a3ec 100644 --- a/qtacrylicmainwindow.cpp +++ b/qtacrylicmainwindow.cpp @@ -165,6 +165,7 @@ void QtAcrylicMainWindow::showEvent(QShowEvent *event) m_acrylicHelper.install(windowHandle()); m_acrylicHelper.updateAcrylicBrush(tintColor()); connect(&m_acrylicHelper, &QtAcrylicEffectHelper::needsRepaint, this, qOverload<>(&QtAcrylicMainWindow::update)); + Utilities::setBlurEffectEnabled(windowHandle(), m_acrylicEnabled); m_inited = true; } } diff --git a/qtacrylicwidget.cpp b/qtacrylicwidget.cpp index d9d962c..b896ee3 100644 --- a/qtacrylicwidget.cpp +++ b/qtacrylicwidget.cpp @@ -164,6 +164,7 @@ void QtAcrylicWidget::showEvent(QShowEvent *event) m_acrylicHelper.install(windowHandle()); m_acrylicHelper.updateAcrylicBrush(tintColor()); connect(&m_acrylicHelper, &QtAcrylicEffectHelper::needsRepaint, this, qOverload<>(&QtAcrylicWidget::update)); + Utilities::setBlurEffectEnabled(windowHandle(), m_acrylicEnabled); m_inited = true; } } diff --git a/utilities.cpp b/utilities.cpp index 5b40388..cae733c 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -165,6 +165,8 @@ static inline void expblur(QImage &img, const qreal radius, const bool improvedQ qt_blurrow(img, row, alpha); } } + // TODO: QImage(int width, int height, QImage::Format format) + // Why the argument order is inverted here? QImage temp(img.height(), img.width(), img.format()); temp.setDevicePixelRatio(img.devicePixelRatio()); if (transposed >= 0) { diff --git a/utilities_win32.cpp b/utilities_win32.cpp index 12e9ae9..a8a9cee 100644 --- a/utilities_win32.cpp +++ b/utilities_win32.cpp @@ -514,8 +514,12 @@ QImage Utilities::getDesktopWallpaperImage(const int screen) if (SUCCEEDED(pDesktopWallpaper->GetMonitorDevicePathAt(monitorIndex, &monitorId)) && monitorId) { LPWSTR wallpaperPath = nullptr; if (SUCCEEDED(pDesktopWallpaper->GetWallpaper(monitorId, &wallpaperPath)) && wallpaperPath) { - return QImage(QString::fromWCharArray(wallpaperPath)); + CoTaskMemFree(monitorId); + const QString _path = QString::fromWCharArray(wallpaperPath); + CoTaskMemFree(wallpaperPath); + return QImage(_path); } else { + CoTaskMemFree(monitorId); qWarning() << "IDesktopWallpaper::GetWallpaper() failed."; } } else { @@ -543,10 +547,12 @@ QImage Utilities::getDesktopWallpaperImage(const int screen) }); #endif if (SUCCEEDED(CoCreateInstance(CLSID_ActiveDesktop, nullptr, CLSCTX_INPROC_SERVER, IID_IActiveDesktop, reinterpret_cast(&pActiveDesktop))) && pActiveDesktop) { - WCHAR wallpaperPath[MAX_PATH] = {}; + const auto wallpaperPath = new WCHAR[MAX_PATH]; // TODO: AD_GETWP_BMP, AD_GETWP_IMAGE, AD_GETWP_LAST_APPLIED. What's the difference? if (SUCCEEDED(pActiveDesktop->GetWallpaper(wallpaperPath, MAX_PATH, AD_GETWP_LAST_APPLIED))) { - return QImage(QString::fromWCharArray(wallpaperPath)); + const QString _path = QString::fromWCharArray(wallpaperPath); + delete [] wallpaperPath; + return QImage(_path); } else { qWarning() << "IActiveDesktop::GetWallpaper() failed."; } @@ -559,7 +565,9 @@ QImage Utilities::getDesktopWallpaperImage(const int screen) qDebug() << "Shell API failed. Using SystemParametersInfoW instead."; LPWSTR wallpaperPath = nullptr; if (SystemParametersInfoW(SPI_GETDESKWALLPAPER, MAX_PATH, wallpaperPath, 0) != FALSE) { - return QImage(QString::fromWCharArray(wallpaperPath)); + const QString _path = QString::fromWCharArray(wallpaperPath); + CoTaskMemFree(wallpaperPath); + return QImage(_path); } qWarning() << "SystemParametersInfoW failed. Reading from the registry instead."; const QSettings settings(g_desktopRegistryKey, QSettings::NativeFormat);