http://progtuts.info/14/your-c-program-in-the-system-tray/
This tutorial will cover the use of the NotifyIcon, better known as the System Tray Icon in c#. The NotifyIcon is a cool feature to add to your application, and you can see numerous examples of it's usage in for example μTorrent, MSN messenger, and your favourite antivirus program. Am sure I don't need to tell you the possibilities, so I'm just going to tell you how to use them. Best of all, NotifyIcon is easy to use, and provides lots of fun. So let's get to work!
What will this tutorial cover?
-How to make a tray icon
-How to make your application minimize to the tray
-How to add a context menu to the tray icon(but I won't explain the context menu itself here)
-How to show messages from the tray icon through a balloon
This tutorial is meant for beginning c# programmers who have mastered the basics, and are ready to add some more advanced features to their applications.
So how do I make a Tray Icon?
Right, let's make a new Windows Forms Application, name it as you please. Now go to the ToolBox, and select NotifyIcon, it's under the Common Controls.
Click on it, and drag it to your form. You will see NotifyIcon1 showing up in a gray box under your form. Now we need to add an icon to it, because without it, nothing shows up in the tray. So find a .ICO file, right click on NotifyIcon1, select properties, and then under Icon you can add your icon. Remember though, it will only accept .ICO files. Now when you run your program you'll see a tray icon showing up. That was easy right! Now let's make the little icon a bit more useful.
How do I minimize my application to the tray?
Ok let's create some code that does this: when the minimize button is clicked, the program minimizes to tray and the tray icon shows up. Double clicking on the tray icon shows the form again and removes the icon.
Set the NotifyIcon's visible property to false in the property editor. Now go to the property editor of Form1, click on the little lightning symbol to access the events, and double click on the Resize event, and change the code to:
|
| |
|
|
|
| |
| | |
|
| |
| |
|
| |
|
|
Finally we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer, to make the event handler for the double_click event show up, and type:
|
| |
|
|
| | |
| |
| | |
|
|
And there you have it, your program closes to tray, and pops back up again when needed. You can change the name the user sees when hovering over the icon by changing the Text property.
How do I add a context menu to the tray icon?
Easy enough, but for the sake of keeping this tutorial short, I'm going to assume you know how to make a context menu. If not, it's in the toolbox under “Menus and toolbars” called ContextMenuStrip. Play with it, google it, am sure you can figure it out.
Now to add it to your tray icon, go to the property editor, and when clicking on the ContextMenuStrip property opens up a drop down list where you can select the appropriate Context Menu in case you have several, and the .NET framework handles the rest and makes it show up when right clicked on the tray icon.
How to pass messages through balloons?
Am sure you know those little balloons popping up from your tray, giving you info about things that have happened while the program was running in the background. You can specify a title, text, and pick an icon from a list of three possible ones(info, warning,error, or no icon). There are basically 2 ways of doing this:
By setting the BalloonTipIcon, BalloonTipText and BalloonTipTitle and then calling:
|
|
But a more flexible way is using an overload of ShowBalloonTip:
ShowBalloonTip(Int32, String, String, ToolTipIcon)
To give an example in code, let's make a balloon pop up three seconds after we minimized our form, just to simulate some event in your program. So go back to the Form1_Resize event we used earlier and add the last few lines to it so it becomes:
|
| |
|
|
| if | |
| |
| | |
| |
|
| |
| |
| |
| |
| | |
| |
|
|
Don't forget to add: using System.Threading; to the top of your code or the Thread.Sleep part won't work. Now you'll see that three seconds after you minimize a balloon will pop up as shown in the picture in the top of this tutorial. Pretty cool right.
This finishes this tutorial, I hope it was useful. Feedback would be appreciated
Change the icon of the notifyIcon
notifyIcon.Icon = new
System.Drawing.Icon(AppDomain.CurrentDomain.BaseDi rectory + "running.ICO");