Page 1 of 1

Make your application minimize to System Tray in C# Also covers context menus and balloon popup texts

#1 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Post icon  Posted 22 July 2009 - 03:50 PM

Making a System Tray Icon for your application

Attached Image

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.
Attached Image
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:

		private void Form1_Resize(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)//this code gets fired on every resize
			{																					   //so we check if the form was minimized
				Hide();//hides the program on the taskbar
				notifyIcon1.Visible = true;//shows our tray icon
			}
		}

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:

		private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			Show();//shows the program on taskbar
			this.WindowState = FormWindowState.Normal;//undoes the minimized state of the form
			notifyIcon1.Visible = false;//hides tray icon again
		}


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:

notifyIcon1.ShowBalloonTip(1000);//1000 is the time in milliseconds the balloon will show up


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:

		private void Form1_Resize(object sender, EventArgs e)
		{
			if (this.WindowState == FormWindowState.Minimized)
			{												 
				Hide();
				notifyIcon1.Visible = true;
				
				Thread.Sleep(3000);//pause for 3 seconds
				//shows a balloon for 1 sec with a title, some text, and the info icon
				//other possibilities are: TooltipIcon.None, Tooltipicon.Error, and TooltipIcon.Warning
				notifyIcon1.ShowBalloonTip(1000, "Hello", "This is a balloontip!", ToolTipIcon.Info);
			}
		}


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.

Is This A Good Question/Topic? 2
  • +

Replies To: Make your application minimize to System Tray in C#

#2 cool_solar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-July 09

Posted 04 August 2009 - 12:26 PM

Great tutorial, short and with no waste talk :) I have only one small problem with balloons, they don't hide after ex. 1000 ms but few seconds later. Maybe do you know why is that?

This post has been edited by cool_solar: 04 August 2009 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Posted 05 August 2009 - 03:48 PM

Thanks :) About your question:

Quote

Minimum and maximum timeout values are enforced by the operating system and are typically 10 and 30 seconds, respectively, however this can vary depending on the operating system. Timeout values that are too large or too small are adjusted to the appropriate minimum or maximum value. In addition, if the user does not appear to be using the computer (no keyboard or mouse events are occurring) then the system does not count this time towards the timeout.

Source: MSDN

I guess that's the answer. I'm using vista and while it does not dissapear after say 1000 ms, it does start to fade after that. What OS are you using?

Edit: just did a quick test, and it seems it starts to dissapear after 2-3secs, and the total animation before it's completely gone is about 1- secs. I wish msdn was a bit clearer on this subject, but it seems we just don't get that much control over the balloon tip.

This post has been edited by Renagado: 05 August 2009 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 cool_solar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-July 09

Posted 06 August 2009 - 07:32 AM

thank you for your answer.
Was This Post Helpful? 0
  • +
  • -

#5 sns.cool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-November 09

Posted 20 November 2009 - 10:32 AM

pretty good and simple way to explain.
thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1