Create a Tray Icon

Submitted on: 1/2/2015 11:56:00 AM
By: Andrew Nielsen (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: Microsoft Visual C++
Views: 670
     Teach users to create a Tray Icon and change the Tip Text and Icon when ever they want with out re-creating the tray icon.

				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
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
Give the Tray Icon the main Windows Instance
nid.uID = (UINT)hInst;
This just created the Icon:
Shell_NotifyIcon(NIM_ADD, &nid);
void ShowTrayIcon(void)
You might be thinking: 'Why did I add the Icon again?', but when I hide the icon I deleted it from the tray...
Shell_NotifyIcon(NIM_ADD, &nid);
void HideTrayIcon(void)
{ you can see here.
Shell_NotifyIcon(NIM_DELETE, &nid);
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;
Shell_NotifyIcon(NIM_MODIFY, &nid);
void SetTrayIconCaption(char *msg)
Same here, just set msg to the message you want to be displayed on mouse over of the Tray Icon
strcpy(nid.szTip, msg);
Shell_NotifyIcon(NIM_MODIFY, &nid);
To clean up the icon when the program is shutting down...
void KillTrayIcon(void)
...first we delete the Icon from the tray...
Shell_NotifyIcon(NIM_DELETE, &nid);
...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:
ShowWindow(hWnd, SW_SHOW);
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.

Other 1 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.