From 645990581658350a071f7262115cebb698c830b8 Mon Sep 17 00:00:00 2001 From: Anthony Lee Stark Date: Fri, 23 May 2025 21:50:43 +0700 Subject: [PATCH] Fix Notepad++ try icon lost after Windows Taskbar crashes & being recreate Re-add the tray icon when the taskbar is re-created. Fix #16588, close #16589 --- PowerEditor/src/NppBigSwitch.cpp | 11 +++++++++++ .../WinControls/TrayIcon/trayIconControler.cpp | 16 +++++++++++++++- .../src/WinControls/TrayIcon/trayIconControler.h | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/PowerEditor/src/NppBigSwitch.cpp b/PowerEditor/src/NppBigSwitch.cpp index 75902690b..d177ea322 100644 --- a/PowerEditor/src/NppBigSwitch.cpp +++ b/PowerEditor/src/NppBigSwitch.cpp @@ -41,6 +41,7 @@ using namespace std; #endif std::atomic g_bNppExitFlag{ false }; +const UINT WM_TASKBARCREATED = ::RegisterWindowMessage(L"TaskbarCreated"); struct SortTaskListPred final @@ -4365,6 +4366,16 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa return TRUE; } + else if (message == WM_TASKBARCREATED) + { + if (!_pTrayIco) + return TRUE; + + // re-add tray icon + _pTrayIco->reAddTrayIcon(); + return TRUE; + } + return ::DefWindowProc(hwnd, message, wParam, lParam); } } diff --git a/PowerEditor/src/WinControls/TrayIcon/trayIconControler.cpp b/PowerEditor/src/WinControls/TrayIcon/trayIconControler.cpp index ea69e9db3..1e5559143 100644 --- a/PowerEditor/src/WinControls/TrayIcon/trayIconControler.cpp +++ b/PowerEditor/src/WinControls/TrayIcon/trayIconControler.cpp @@ -27,7 +27,6 @@ trayIconControler::trayIconControler(HWND hwnd, UINT uID, UINT uCBMsg, HICON hic _nid.hIcon = hicon; wcscpy_s(_nid.szTip, tip); - ::RegisterWindowMessage(L"TaskbarCreated"); _isIconShown = false; } @@ -44,3 +43,18 @@ int trayIconControler::doTrayIcon(DWORD op) return 0; } + +int trayIconControler::reAddTrayIcon() +{ + if (!_isIconShown) + return -1; + + // we only re-add the tray icon when it has been lost and no longer exists, + // but we still delete it first to ensure it's not duplicated + // due to an incorrect call or something + ::Shell_NotifyIcon(NIM_DELETE, &_nid); + + // reset state and re-add tray icon + _isIconShown = false; + return doTrayIcon(ADD); +} diff --git a/PowerEditor/src/WinControls/TrayIcon/trayIconControler.h b/PowerEditor/src/WinControls/TrayIcon/trayIconControler.h index 636574bf9..f70d0df66 100644 --- a/PowerEditor/src/WinControls/TrayIcon/trayIconControler.h +++ b/PowerEditor/src/WinControls/TrayIcon/trayIconControler.h @@ -32,6 +32,7 @@ public: trayIconControler(HWND hwnd, UINT uID, UINT uCBMsg, HICON hicon, const wchar_t *tip); int doTrayIcon(DWORD op); bool isInTray() const {return _isIconShown;}; + int reAddTrayIcon(); private: NOTIFYICONDATA _nid;