Reputation: 37606
I'm currently working on a Win32 application and I want to have an icon in the system task bar. I'm using Shell_NotifyIcon
to display the icon, and it works well but often the icon disapears without explanation...
In fact, the bug appears the first time I run the code in Visual Studio 2013 after having built the code OR when I run the application from the .exe
in another directory (I don't have any error like missing dll
and the application is running, but the icon is not there anymore... ).
The code I use to create the icon:
// At the beginning of the file
static NOTIFYICONDATA m_nid ;
// After
m_nid.cbSize = sizeof (m_nid);
m_nid.hWnd = hwnd;
m_nid.uVersion = NOTIFYICON_VERSION_4 ;
static const GUID myGUID =
{0x5CA81ADA, 0xA481, 0x4BA8, {0x8B, 0x70, 0x80, 0x3f, 0x48, 0x67, 0xA1, 0x68}};
m_nid.guidItem = myGUID;
m_nid.uFlags = NIF_ICON | NIF_TIP | NIF_GUID | NIF_MESSAGE ;
m_nid.uCallbackMessage = WM_SYSTEM_TRAY;
StringCchCopy (m_nid.szTip, ARRAYSIZE (m_nid.szTip), NotificationManager::APPLICATION_NAME.c_str ());
m_nid.hIcon = (HICON) LoadImage (NULL, L"icon_v1.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
ShowWindow (m_nid.hWnd, SW_HIDE);
Shell_NotifyIcon (NIM_ADD, &m_nid);
Shell_NotifyIcon (NIM_SETVERSION, &m_nid);
Edit: I think it's a problem with the GUID
because in Release
mode the icon was not showing, but since I changed the GUID
it's working... But it still not works when I run the program outside Visual Studio...
Upvotes: 4
Views: 4185
Reputation: 109
from the notification documentation on msdn I see this:
> Each icon in the notification area can be identified in two ways:
>
> The GUID with which the icon is declared in the registry. This is the preferred method on Windows 7 and later.
> The handle of a window associated with the notification area icon, plus an application-defined icon identifier. This method is used on
> Windows Vista and earlier.
here is the link also: http://msdn.microsoft.com/en-us/library/windows/desktop/ee330740%28v=vs.85%29.aspx
Upvotes: 0
Reputation: 595329
There are several possibilities for why a system tray icon disappears.
The HWND associated with the icon was destroyed.
In modern Windows versions, the icon has been hidden from view by user settings outside of your app's control.
Windows Explorer crashed and restarted, but you did not handle the TaskbarCreated
message to re-add your icon.
Edit: Another possibility is that you are simply passing bad input to Shell_NotifyIcon()
and it is failing with an error that you are ignoring.
Upvotes: 6
Reputation: 247899
It is most likely intended behavior. Windows 7 (and later) is very aggressive about not always displaying notification icons, because they are, and were, heavily abused. Their intend were, as the name implies, to provide notifications, and nothing else.
The API doesn't guarantee that your icon will be visible. It only allows you to register that you would like to make a notification. It is up to the OS to decide whether that notification should be displayed, and for how long.
If you go into Notification Area Icons in the Control Panel, you can see the settings that are applied to each icon. Most likely, yours is set to either "Hide icon and notifications", or "only show notifications".
And no, there is nothing you can do about that, other than asking the user to allow your icon. Once again, the API is for delivering notifications, and if the user doesn't want to see your icon or your notification, the OS respects that.
Upvotes: 2