A way to ensure that a system tray icon is removed

2020-07-06 03:42发布

Is there a way to guarantee that your system tray icon is removed?

To add the system tray icon you do:

Shell_NotifyIcon(NIM_ADD, &m_tnd);

To remove the system tray icon you do:

Shell_NotifyIcon(NIM_DELETE, &m_tnd);

What I want to know: what if you application crashes? The icon stays in your system tray until you mouse over. Is there a way to guarantee that the icon will be removed, even when the application crashes? I would prefer not to use structured exception handling for various reasons.

Another case that I want to handle is when the process is killed, but doesn't necessarily crash.

8条回答
爷、活的狠高调
2楼-- · 2020-07-06 04:27

You could have a separate, simpler (and thus presumably more robust) program which monitors your application. This program could actually launch your program and then monitor the process. Yeah, this is a very ugly solution.

查看更多
小情绪 Triste *
3楼-- · 2020-07-06 04:27

Hmm, you can always have an external monitor process call SendMessage with the WM_PAINT message to the system tray window (which you would have to do based on the class of the window). That should remove the icon which is no longer valid.

查看更多
登录 后发表回答