MFC - Adding / Removing an Application icon to the task bar
Create a NOTIFYICONDATA object.
Initialise it's members, for example :
NOTIFYICONDATA *
poIconData = new NOTIFYICONDATA;
poIconData->cbSize = sizeof(NOTIFYICONDATA);
poIconData->hWnd = (HWND) this ;
poIconData->uCallbackMessage = ID_TASKBAR_CALLBACK_MESSAGE;
poIconData->uFlags = NIF_MESSAGE | NIF_TIP | NIF_ICON;
poIconData->hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
sprintf(poIconData->szTip,"Tip Text");
poIconData->uID = 0;
Add the icon using :
Shell_NotifyIcon(NIM_ADD, poIconData);
To remove the icon use
Shell_NotifyIcon(NIM_DELETE, poIconData);
Notes :
uId is relative to the number of icons "this" application has on the icon bar. If you have more than one then you should have a unique uID for each.
uCallbackMessage is sent back to "this" application and does not need to be handled. The message is sent every time the the mouse moves accross the icon.
With the NIF_MESSAGE
option set, if the mouse moves accross the icon and and the hWnd
cannot be found then the icon is removed from the icon bar
automatically.
More Info :
If you are adding an icon to the task bar it is possible ( probable ) that you are creating an App that doesn't have a UI until something happens or someone selects something on the task bar icon. Your application is a worker - it does stuff but doesn't clutter the desk top with windows.
In order to respond to mouse messages from the task bar icon, you need a window. If you want to make the window invisible then you need to do something a bit special ( not very ). If you edit the ShowWindow call that is made at the end of the InitInstance function then you make you MFC application window invisible. This is only possible with Doc / View applications. If you choose to use a single Doc interface then you will get a flicker of the window before it becomes invisible ( this is when the FileNew is carried out by default ). To get rid of the flicker create a multi Doc interface.
For worker type Apps I would choose to use the Ap Wizard to create a multi Doc application and then set the ShowWindow operand to SW_HIDE. Then I can process the mouse commands in my Main Frame and show dialogs when I need to interact with the user. The multi Doc window is never shown.