From 0fbabfd048daf0d62b8eb3a2f801cc946b2f6fe7 Mon Sep 17 00:00:00 2001 From: lx-s Date: Wed, 8 Jul 2026 10:57:36 +0200 Subject: [PATCH] Set tray icon version correctly. --- WinMute/TrayIcon.cpp | 259 ++++++++++++++++++++++--------------------- 1 file changed, 131 insertions(+), 128 deletions(-) diff --git a/WinMute/TrayIcon.cpp b/WinMute/TrayIcon.cpp index ae65209..4bca8d4 100644 --- a/WinMute/TrayIcon.cpp +++ b/WinMute/TrayIcon.cpp @@ -37,177 +37,180 @@ TrayIcon::TrayIcon() { } -TrayIcon::TrayIcon( - HWND hWnd, UINT trayID, HICON hIcon, - const std::wstring& tooltip, bool show, - int callbackId) - : callbackId_(callbackId) +TrayIcon::TrayIcon(HWND hWnd, UINT trayID, HICON hIcon, + const std::wstring& tooltip, bool show, int callbackId) + : callbackId_(callbackId) { - Init(hWnd, trayID, hIcon, tooltip, show); + Init(hWnd, trayID, hIcon, tooltip, show); } -void TrayIcon::Init( - HWND hWnd, UINT trayID, HICON hIcon, - const std::wstring& tooltip, bool show, - int callbackId) +void TrayIcon::Init(HWND hWnd, UINT trayID, HICON hIcon, + const std::wstring& tooltip, bool show, int callbackId) { - callbackId_ = callbackId; - if (!hIcon) { - hIcon_ = LoadIcon(0, IDI_APPLICATION); - } else { - hIcon_ = hIcon; - } - - tooltip_ = tooltip; - trayID_ = trayID; - hWnd_ = hWnd; - - if (show) { - Show(); - } - initialized_ = true; - if (popupQueue_.size() > 0) { - for (const auto& p : popupQueue_) { - ShowPopup(p.title, p.text); - } - popupQueue_.clear(); - } + callbackId_ = callbackId; + if (!hIcon) { + hIcon_ = LoadIcon(0, IDI_APPLICATION); + } else { + hIcon_ = hIcon; + } + + tooltip_ = tooltip; + trayID_ = trayID; + hWnd_ = hWnd; + + if (show) { + Show(); + } + initialized_ = true; + if (popupQueue_.size() > 0) { + for (const auto& p : popupQueue_) { + ShowPopup(p.title, p.text); + } + popupQueue_.clear(); + } } TrayIcon::~TrayIcon() { - Hide(); - DestroyTrayIcon(); + Hide(); + DestroyTrayIcon(); } void TrayIcon::Hide() { - if (iconVisible_) { - if (RemoveNotifyIcon()) { - iconVisible_ = false; - } - } + if (iconVisible_) { + if (RemoveNotifyIcon()) { + iconVisible_ = false; + } + } } void TrayIcon::Show() { - if (!iconVisible_) { - if (AddNotifyIcon()) { - iconVisible_ = true; - } - } + if (!iconVisible_) { + if (AddNotifyIcon()) { + iconVisible_ = true; + } + } } void TrayIcon::ChangeIcon(HICON hNewIcon) { - if (!hNewIcon) { - return; - } - bool oldIconVisible = iconVisible_; - - DestroyTrayIcon(); - hIcon_ = hNewIcon; - - if (oldIconVisible) { - NOTIFYICONDATA tnid{0}; - tnid.cbSize = sizeof(NOTIFYICONDATA); - tnid.uVersion = NOTIFYICON_VERSION_4; - tnid.hWnd = hWnd_; - tnid.uID = trayID_; - tnid.hIcon = hIcon_; - tnid.uFlags = NIF_ICON; - - if (!Shell_NotifyIcon(NIM_MODIFY, &tnid)) - return; - - iconVisible_ = true; - } - - return; + if (!hNewIcon) { + return; + } + bool oldIconVisible = iconVisible_; + + DestroyTrayIcon(); + hIcon_ = hNewIcon; + + if (oldIconVisible) { + NOTIFYICONDATA tnid{0}; + tnid.cbSize = sizeof(NOTIFYICONDATA); + tnid.uVersion = NOTIFYICON_VERSION_4; + tnid.hWnd = hWnd_; + tnid.uID = trayID_; + tnid.hIcon = hIcon_; + tnid.uFlags = NIF_ICON; + + if (!Shell_NotifyIcon(NIM_MODIFY, &tnid)) + return; + + iconVisible_ = true; + } + + return; } void TrayIcon::ChangeText(const std::wstring& tooltip) { - tooltip_ = tooltip; + tooltip_ = tooltip; - if (iconVisible_) - ChangeText(); + if (iconVisible_) + ChangeText(); } -void TrayIcon::ShowPopup( - const std::wstring& title, - const std::wstring& text) const +void TrayIcon::ShowPopup(const std::wstring& title, + const std::wstring& text) const { - if (!initialized_) { - TrayIconPopup popup; - popup.title = title; - popup.text = text; - popupQueue_.push_back(popup); - return; - } - NOTIFYICONDATAW tnid{0}; - tnid.cbSize = sizeof(tnid); - tnid.uVersion = NOTIFYICON_VERSION_4; - tnid.hWnd = hWnd_; - tnid.uID = trayID_; - tnid.uFlags = NIF_INFO | NIF_SHOWTIP; - tnid.dwInfoFlags = NIIF_INFO | NIIF_NOSOUND | NIIF_LARGE_ICON | NIIF_RESPECT_QUIET_TIME; - tnid.uTimeout = 10 * 1000; - StringCchCopy(tnid.szInfoTitle, ARRAY_SIZE(tnid.szInfoTitle), title.c_str()); - StringCchCopy(tnid.szInfo, ARRAY_SIZE(tnid.szInfo), text.c_str()); - - Shell_NotifyIcon(NIM_MODIFY, &tnid); + if (!initialized_) { + TrayIconPopup popup; + popup.title = title; + popup.text = text; + popupQueue_.push_back(popup); + return; + } + NOTIFYICONDATAW tnid{0}; + tnid.cbSize = sizeof(tnid); + tnid.uVersion = NOTIFYICON_VERSION_4; + tnid.hWnd = hWnd_; + tnid.uID = trayID_; + tnid.uFlags = NIF_INFO | NIF_SHOWTIP; + tnid.dwInfoFlags = + NIIF_INFO | NIIF_NOSOUND | NIIF_LARGE_ICON | NIIF_RESPECT_QUIET_TIME; + tnid.uTimeout = 10 * 1000; + StringCchCopy(tnid.szInfoTitle, ARRAY_SIZE(tnid.szInfoTitle), + title.c_str()); + StringCchCopy(tnid.szInfo, ARRAY_SIZE(tnid.szInfo), text.c_str()); + + Shell_NotifyIcon(NIM_MODIFY, &tnid); } void TrayIcon::DestroyTrayIcon() { - if (hIcon_) { - DestroyIcon(hIcon_); - hIcon_ = 0; - } + if (hIcon_) { + DestroyIcon(hIcon_); + hIcon_ = 0; + } } bool TrayIcon::AddNotifyIcon() { - NOTIFYICONDATAW tnid{ 0 }; - tnid.cbSize = sizeof(tnid); - tnid.uVersion = NOTIFYICON_VERSION_4; - tnid.hWnd = hWnd_; - tnid.uID = trayID_; - tnid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_SHOWTIP; - tnid.hIcon = hIcon_; - tnid.uCallbackMessage = callbackId_; - - if (SUCCEEDED(StringCchCopy(tnid.szTip, ARRAY_SIZE(tnid.szTip), tooltip_.c_str()))) { - return Shell_NotifyIconW(NIM_ADD, &tnid) != 0; - } - return false; + NOTIFYICONDATAW tnid{0}; + tnid.cbSize = sizeof(tnid); + tnid.uVersion = NOTIFYICON_VERSION_4; + tnid.hWnd = hWnd_; + tnid.uID = trayID_; + tnid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_SHOWTIP; + tnid.hIcon = hIcon_; + tnid.uCallbackMessage = callbackId_; + + if (SUCCEEDED(StringCchCopy(tnid.szTip, ARRAY_SIZE(tnid.szTip), + tooltip_.c_str()))) + { + DWORD result = Shell_NotifyIconW(NIM_ADD, &tnid); + if (result == 0) { + result = Shell_NotifyIconW(NIM_SETVERSION, &tnid); + } + return result != 0; + } + return false; } bool TrayIcon::RemoveNotifyIcon() { - NOTIFYICONDATAW tnid{0}; - tnid.cbSize = sizeof(tnid); - tnid.uVersion = NOTIFYICON_VERSION_4; - tnid.hWnd = hWnd_; - tnid.uID = trayID_; + NOTIFYICONDATAW tnid{0}; + tnid.cbSize = sizeof(tnid); + tnid.uVersion = NOTIFYICON_VERSION_4; + tnid.hWnd = hWnd_; + tnid.uID = trayID_; - return Shell_NotifyIcon(NIM_DELETE, &tnid) != 0; + return Shell_NotifyIcon(NIM_DELETE, &tnid) != 0; } bool TrayIcon::ChangeText() { - NOTIFYICONDATAW tnid{0}; - tnid.cbSize = sizeof(tnid); - tnid.uVersion = NOTIFYICON_VERSION_4; - tnid.hWnd = hWnd_; - tnid.uID = trayID_; - tnid.uFlags = NIF_TIP; - - if (SUCCEEDED(StringCchCopy(tnid.szTip, ARRAY_SIZE(tnid.szTip), tooltip_.c_str()))) { - return Shell_NotifyIcon(NIM_MODIFY, &tnid) != 0; - } - return false; + NOTIFYICONDATAW tnid{0}; + tnid.cbSize = sizeof(tnid); + tnid.uVersion = NOTIFYICON_VERSION_4; + tnid.hWnd = hWnd_; + tnid.uID = trayID_; + tnid.uFlags = NIF_TIP; + + if (SUCCEEDED(StringCchCopy(tnid.szTip, ARRAY_SIZE(tnid.szTip), + tooltip_.c_str()))) + { + return Shell_NotifyIcon(NIM_MODIFY, &tnid) != 0; + } + return false; } - - -- 2.43.0