Search code examples
c++windowssystem-tray

Why would a system tray icon clear an earlier-displayed one?


We have an application that uses various system tray icons to communicate with users. Different icons indicate different internal states of the (otherwise windowless) application. We implemented our system tray stuff using code from this Code Guru project (the MFC version; this is not a new application by any means), and until recently, it has worked fine. However, recently we have tried to add another icon and have run into trouble.

Here's how it's supposed to work:

  1. We have one main icon (call it 'A') that indicates that the application is running.

  2. If a particular event occurs, we display icon 'B', over which the user can hover their mouse to get a tooltip with status regarding that event.

  3. If a (recently added, internal, threaded) procedure starts, we display icon 'C', and again, the user can hover over that to see a tooltip that indicates the progress of that activity.

What's actually happening: if icon 'B' is visible when we (try to) display icon 'C', then icon 'B' either disappears entirely, never to return, or it stays there and icon 'C' never shows up, but the tooltip for icon 'B' is changed to what icon 'C' should have.

I've simplified the scenario a bit; we actually have a few other icons, but they're rarely used. However, we have never had any trouble displaying multiple icons until we added icon 'C' in the last couple of weeks.

Any ideas? Happy to clarify anything I haven't explained well.


Solution

  • You need to use a different uID for each icon. The documentation says:

    The application-defined identifier of the taskbar icon. The Shell uses either (hWnd plus uID) or guidItem to identify which icon to operate on when Shell_NotifyIcon is invoked. You can have multiple icons associated with a single hWnd by assigning each a different uID. If guidItem is specified, uID is ignored.

    In other words you need to pick a value for uID. This is how the shell knows which icon you are referring to each time you call Shell_NotifyIcon(). Clearly you must use a different value for each icon. So, use 0 for icon A, 1 for icon B etc.