i wanna make a program runs in the background and shows an icon in notification area of taskbar. I'm using win32. What api should i use? Do you know any good tutorials?
To make a program run in the background, you either add it as a service or make it "unavailable" to shutdown (for instance, hide the window for the program). To add an icon in the toolbar you use winapi. Call Shell_NotifyIcon and pass in a NOTIFYICONDATA structure
This should be defined somewhere
enum TrayIcon {
ID = 13, CALLBACKID = WM_APP+1
};
Also, in the below code the hWnd
is a HWND
, which is the window that you want to associate with the notification icon. This HWND's
wndProc
will receive the messages for the icon.
NIF_ICON
flag makes the hIcon
valid in the NOTIFICATIONICONDATA
structure. So if you don't want to have an icon, don't specify it.NIF_MESSAGE
flag makes the uCallbackMessage
valid. If you don't want to handle any messages, don't specify this flag.With this said, this is how you add, remove and handle the messages for the tray icon
// in HICON hIcon: this is the icon you want as the image in the tray
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = ID;
nid.uFlags = NIF_ICON | NIF_MESSAGE;
nid.hIcon = hIcon;
nid.uCallbackMessage = /*TrayIcon::*/CALLBACKID;
Shell_NotifyIcon(NIM_ADD, &nid);
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = /*TrayIcon::*/ID;
Shell_NotifyIcon(NIM_DELETE, &nid);
LRESULT wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch (msg){
// ...
case /*TrayIcon::*/CALLBACKID:
{
// here, you handle the messages for your tray icon
}
break;
// ...
}
}