I have supplied you with some sample code, I will just explain what I did and why I did it so you can understand what is going on :-)
I swipped this code from my CD Player, so for those of you who might recognize this, that's where it came from.
first let's create the Icon Variable
void CreateTrayIcon(HWND hWnd, HINSTANCE hInst)
First we set the size of the TrayIcon variable:
nid.cbSize = sizeof(nid);
I initialized the main Icon here
TrayIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_TRAY), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
Now I set the main Icon into the Tray Icon, you will see soon how to change this with out resetting the Icon.
nid.hIcon = TrayIcon;
Here I gave the Icon something to 'report to' all the user inputs...
nid.hWnd = hWnd;
Mouse over tip text, you will see how to change this also.
strcpy(nid.szTip, "CD Player");
Now I set the Callback Message, I will explain this later in the code.
nid.uCallbackMessage = WM_TRAYICON;
Here I told the Tray Icon what I wanted it to do, I have it making the Icon and the Tip, aswell as having the Tray notice it has been assigned a Callback Message
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
Give the Tray Icon the main Windows Instance
nid.uID = (UINT)hInst;
This just created the Icon:
You might be thinking: 'Why did I add the Icon again?', but when I hide the icon I deleted it from the tray...
...as you can see here.
void SetTrayIconIcon(HICON icon)
To change the Icon, just make icon equal to the icon in which you wish it to be
nid.hIcon = icon;
void SetTrayIconCaption(char *msg)
Same here, just set msg to the message you want to be displayed on mouse over of the Tray Icon
To clean up the icon when the program is shutting down...
...first we delete the Icon from the tray...
...then we destroy the main Icon...
...and reset the Tray Icon variable back to nothing.
memset(&nid, 0, sizeof(nid));
now we can 'handle' all input from the Icon and show a menu when it's right or left clicked on:
this is where the Callback Message comes into play... whenever the icon is clicked on, whether it be left or right click, WM_TRAYICON is sent to the window procedure for processing
to catch any clicks you make a switch/case loop using the LPARAM parameter of the windows procedure
then you can have it check to see if it was a right button or a left button that was clicked.
here I just have it getting the cursor position and displaying a menu where ever the right mouse button was pressed
this just keeps in a respectable place on the screen...
if (pp.x < (long) (GetSystemMetrics(SM_CXSCREEN) - 100))
pp.x = (long) (GetSystemMetrics(SM_CXSCREEN) - 100);
if (pp.y < (long) (GetSystemMetrics(SM_CYSCREEN) - 100))
pp.y = (long) (GetSystemMetrics(SM_CYSCREEN) - 100);
TrackPopupMenu(hPopup, TPM_RIGHTBUTTON, pp.x, pp.y, 0, hWnd, NULL);
for the left button I have it showing the window if I had it minimized:
to process any menu commands, just process that the way you would with a normal window menu.
That pretty much covers it! if you hve any further questions about ANY of my tutorials E-Mail me, and I will answer your questions as best as I can.