problems with Shell_NotifyIcon

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi guys,

I'm an absolute novice when it comes to using Windows API calls, but thought
I'd have a go at writing a command line application that allows a user to
specify a piece of text which will be shown as a balloon hint in the system
tray. After some research, I found the Shell_NotifyIcon command and I've
had some limited success. I've run into the following problems:

(Running VS6, with the Feb 2003 platform SDK installed)

1. Once my icon has been added to the tray, it disappears whenever a user
moves the mouse over the top of the icon. This seems like strange behaviour
and I'd rather it remain regardless of mouse movements.

2. NOTIFYICONDATA->uTimeout seems to be completely ignored. According to
the msdn, the system enforces a min/max timeout of 10/30 seconds. It
doesn't seem to matter what I specify - the balloon stays up until the user
dismisses it. At the moment I've got a very dodgy Sleep() in place as a
work-around.

3. I'd like to get a callback from the tray when a user has dismissed the
balloon. I've looked at some example code, and it seems that I need to
create a window to receive notification messages. Can I do that from a
console application? If not, can I make a win32 app that has an invisible
window and still interacts with the command line?

Here's the extremely simplistic code that I have so far:

#define _WIN32_IE 0x0501
#include "shellapi.h"
#include "winuser.h"

int main (int argc, char* argv[])
{

NOTIFYICONDATA data;
ZeroMemory(&data,sizeof(data));
data.cbSize = sizeof(NOTIFYICONDATA);
data.uCallbackMessage = 0;

//Title
strncpy(data.szInfoTitle, "title", sizeof(data.szInfoTitle));

//Message
strncpy(data.szInfo, "message", sizeof(data.szInfo));

//Timeout
data.uTimeout = 15000;
data.uVersion = NOTIFYICON_VERSION;

data.dwInfoFlags = NIIF_INFO;

//Load the icon
HICON icon = (HICON)LoadImage( NULL,
MAKEINTRESOURCE(32514),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_SHARED);
data.hIcon = icon;

//Unique identifier
data.uID = 1;

//Use the icon and use balloon functionality
data.uFlags = NIF_ICON | NIF_INFO;

//Add the icon to the tray and show message
bool blnReturn = Shell_NotifyIcon(NIM_ADD, &data);

//Sleep for x milliseconds
Sleep(10000);

if (icon)
{
DestroyIcon(icon);
}

//Delete the tray icon
Shell_NotifyIcon(NIM_DELETE, &data);

return 0;
}

Any suggestions would be most appreciated.

Cheers,

Andrew


.



Relevant Pages

  • Re: problems with Shell_NotifyIcon
    ... You need to have a window to handle the notification messages from the ... the system removes your icon from the system tray. ... Once my icon has been added to the tray, ...
    (microsoft.public.win32.programmer.ui)
  • Re: Running Process in the Background
    ... We were able to meet the desired specification using the System tray. ... can't receive Registered Window Messages from another process. ... Another is to create a dialog-based process that has a tray icon. ... The window registered message would then ...
    (microsoft.public.vc.mfc)
  • Re: New to Windows Coding...
    ... When I right click on my icon in the tray I am bringing up a menu. ... first call SetForegroundWindow on the window that owns the popup. ... until I finally uncovered a problem report about it on MSDN. ...
    (microsoft.public.vc.mfc)
  • Re: WinXP and Activesync
    ... I have this same issue - the AS icon is not in the tray and if I ... I cannot even start activesync by clicking on the program. ... it's there, but, when you double click it no window appears? ...
    (microsoft.public.pocketpc.activesync)
  • Re: Some issues on Tk
    ... I use the "wm iconbitmap" command to change the icon of the main ... wm iconbitmap window ?bitmap? ... If bitmap is specified, then it names a bitmap in the standard ...
    (comp.lang.tcl)