Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Icon added to notification tray disappears on mouse over

Tags:

windows

winapi

I want my application to have an icon in the notification area in Windows 7. I used Shell_NotifyIcon to add the icon. The icon appears, but when I bring the mouse pointer over the icon, the icon disappears. The application is running the whole time. The icon isn't hidden, it just disappears.

Shell_NotifyIcon returns a non-zero value, which means it succeeds.

Here's the relevant code:

static const int ID_TRAYICON = 300;
static const int MSG_TRAYICON = WM_USER + 1;
NOTIFYICONDATA nid;
void InitTrayIconData()
{
    memset(&nid, 0, sizeof(NOTIFYICONDATA));

    nid.cbSize = sizeof(NOTIFYICONDATA);
    nid.hWnd = hwnd;
    nid.uID = ID_TRAYICON;
    nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
    nid.uCallbackMessage = MSG_TRAYICON;
    nid.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
    //nid.uVersion = NOTIFYICON_VERSION_4;
    lstrcpy(nid.szTip, TEXT("Data Aggregator in-dev version"));
}

Then while processing the WM_CREATE message:

InitTrayIconData();
Shell_NotifyIcon(NIM_ADD, &nid);

And while processing WM_DESTROY:

Shell_NotifyIcon(NIM_DELETE, &nid);

I've also noticed that for some reason the MSG_TRAYICON message is never called.

like image 924
nerdap Avatar asked Jul 17 '11 06:07

nerdap


People also ask

How do I unhide system tray icons?

To change how icons and notifications appearPress and hold or right-click any empty space on the taskbar and select Taskbar settings. Under Taskbar corner icons: Select On for any icons you want to see on the taskbar. Select Off for any icons you don't want to see on the taskbar.

Where are system tray icons stored?

Most icons from Windows operating systems are stored inside DLL files.

What is the tray icon in Windows 10?

The system tray (or "systray") is a section of the taskbars in the Microsoft Windows operating system (OS) user interface that provides easy access icons to the user's most commonly used apps and displays the clock. A system tray is also available in other OSes such as Linux, Mac OS, Android and iOS.


2 Answers

I figured it out. When I called InitTrayIconData() in WM_CREATE, the global hwnd hadn't been assigned the value returned from CreateWindowEx yet (the WM_CREATE message wasn't sent after the CreateWindowEx call, but during it, which I didn't know). So the line,

nid.hWnd = hwnd;

just equated nid.hWnd to nullptr (which is what I had initialized hwnd to).

I fixed the problem by passing the hwnd argument in WndProc to the InitTrayIconData(), so it would use that hwnd instead of the global hwnd.

like image 114
nerdap Avatar answered Oct 31 '22 19:10

nerdap


This happens when the system can't communicate with the application that owns the notification icon.

Normally this is because the process has terminated abnormally. In your case you state that the process is running the whole time. Thus I can only conclude that the window handle associated with the notification icon has been destroyed, or is not responding to messages correctly. That diagnosis also tallies with your observation that you do not receive MSG_TRAYICON.

like image 31
David Heffernan Avatar answered Oct 31 '22 19:10

David Heffernan