开发者

Icon added to notification tray disappears on mouse over

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("Da开发者_如何学Pythonta 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜