Windows: clean up System Tray Icon message icon

The handle is not owned by the Shell, we have to clear it up ourselves.
The documentation is not clear about how long the handle needs to be
kept alive, so store the icon when we create it as a member of the
private, and clean it up when it need to be recreated, or when the
QSystemTrayIcon instance gets destroyed.

Fixes: QTBUG-96348
Fixes: QTBUG-62945
Pick-to: 6.6 6.5 6.2 5.15
Change-Id: I6f93f29a415cde2cfe4e1b296295783c15b4da4b
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
(cherry picked from commit afb74a86d8cd1ac6463fa804300480967101d7d7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2024-02-27 10:35:49 +01:00 committed by Qt Cherry-pick Bot
parent a3a2d3df20
commit 59ff87dc91
2 changed files with 10 additions and 1 deletions

View File

@ -220,11 +220,16 @@ void QWindowsSystemTrayIcon::showMessage(const QString &title, const QString &me
const auto size = icon.actualSize(QSize(256, 256));
QPixmap pm = icon.pixmap(size);
if (m_hMessageIcon) {
DestroyIcon(m_hMessageIcon);
m_hMessageIcon = nullptr;
}
if (pm.isNull()) {
tnd.dwInfoFlags = NIIF_INFO;
} else {
tnd.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON;
tnd.hBalloonIcon = qt_pixmapToWinHICON(pm);
m_hMessageIcon = qt_pixmapToWinHICON(pm);
tnd.hBalloonIcon = m_hMessageIcon;
}
tnd.hWnd = m_hwnd;
tnd.uTimeout = msecsIn <= 0 ? UINT(10000) : UINT(msecsIn); // 10s default
@ -282,7 +287,10 @@ void QWindowsSystemTrayIcon::ensureCleanup()
}
if (m_hIcon != nullptr)
DestroyIcon(m_hIcon);
if (m_hMessageIcon != nullptr)
DestroyIcon(m_hMessageIcon);
m_hIcon = nullptr;
m_hMessageIcon = nullptr;
m_menu = nullptr; // externally owned
m_toolTip.clear();
}

View File

@ -55,6 +55,7 @@ private:
QString m_toolTip;
HWND m_hwnd = nullptr;
HICON m_hIcon = nullptr;
HICON m_hMessageIcon = nullptr;
mutable QPointer<QWindowsPopupMenu> m_menu;
bool m_ignoreNextMouseRelease = false;
bool m_visible = false;