Your C# program in the system tray:
note:This post isn't by me, main source here
When creating desktop applications it is sometimes necessary for the application to run in the background, for example firewall, anti-virus and media player software. Instead of permanently taking up space in your taskbar these applications are accessible from the system tray.
This tutorial will show you how to do this in a C# application using Microsoft Visual Studio. I will also show you how to add a right click context menu to the tray icon and how to programmatically change the tray icon.
1) The Notify Icon Control
The control used to make an icon appear in the system tray is a NotifyIcon control. So go ahead and make a new project. Open up the Visual Studio Toolbox and drag a NotifyIcon onto your form. The control will named notifyIcon1 by default and placed below the form because it has no visual representation on the form itself.
Next set the Text property on the NotifyIcon to whatever you want appear when you hover over your icon in the system tray. In this case I just put “System Tray Demo” but usually this would be the name of your application.
Next you need to set the Icon property on the NotifyIcon. You should really make a unique icon for your program to set it apart from the many other applications that often run in the system tray. In this example I’m going to use an email icon (you’ll see why later when we change the icons). So go ahead and choose it.
Note: When I say choose an icon file i don’t just mean any image format. I mean a .ico file. Many image editing applications allow you to save in this format. If you don’t have such an application you can use a website such as www.favicon.cc to generate one.
2) Events
Now time for some coding. In this example our program will minimize to the tray but you can do the same thing for other events such as the window closing etc.
In MS Visual Studio click on your form (in design mode). Then in the properties box click on the small lightning icon (Events) near the top of the properties box. Scroll down to the “Layout” sub-heading and double click on the Resize event. This will generate some code which should look a bit like this.
private void Form1_Resize(object sender, EventArgs e)
{
}
Next add the following code so that when the window is minimized it will be hidden and not shown in the taskbar.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
this.ShowInTaskbar = false;
}
}
Now before you get too excited and try and run the program remember we need to be able to show the window again. To do this click on notifyIcon1 (in design mode) then go to its events list (same as above) and double click on the DoubleClick event. Then add the following code so the window will be restored when the system tray icon is double clicked.
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
Now you can try out your program. Run it and make sure it is hidden when you click the minimize button and it can be restored when you double click the system tray icon.
3) Right Click Context Menu
To make it easy to access your application from the system tray the next thing we will add is a right click context menu. To do this go to the Visual Studio Toolbox and drag a ContextMenuStrip control onto your form. It will be placed below the form again as it has no visual representation on the form itself.
Right click on the contextMenuStrip1 and click “Edit Items…”. Add two Menu Items and change their Text properties to “Show” and “Exit”. Obviously in your own application you can add whatever you want here.
When you click on contextMenuStrip1 (in design mode) you should see the menu appear on your form. Don’t worry it won’t actually be there when you run your application. As before double click on the items to create an event for them. For the Show item we just need to copy the code from the notifyIcon1_DoubleClick method we made above. You should have something like this.
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.ShowInTaskbar = true;
}
For the Exit item all we need to do is call the Close() method.
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
this.Close();
}
The last step is to set the NotifyIcon control’s ContextMenuStrip property to our new context menu. Do this the same way you set other properties. The name of our context menu should appear in the dropdown box.
Run your application and you should get something like this when you right click on the system tray icon.
4) Changing the Icon
In some applications the system tray icon might change to reflect the status of the application. For example the AVG Antivirus icon changes when your virus database is out of date. In this example imagine we have an email checking application. We have already set the normal icon, but what if we have new mail? We might want a different icon to show that we have new mail.
To do this right click on your Project in the Visual Studio Solution Explorer. Then go to “Add -> Existing Item…”. Add any icons you want to add. In this example I am adding three icons: normalIcon.ico, workingIcon.ico and newmailIcon.ico. You should see your icons in the solution explorer.
Next you must set the Build Action properties of all your icons to Embedded Resource. If you don’t do this it won’t work. Setting properties is just like above.
Now changing your icons is actually quite easy. All you have to do is programmatically set your icons. For example.
public void checkMail()
{
//Set a working icon
this.notifyIcon1.Icon = new Icon(GetType(), "workingIcon.ico");
//... do some checking here...
//if you have new mail
this.notifyIcon1.Icon = new Icon(GetType(), "newmailIcon.ico");
//else reset the icon
this.notifyIcon1.Icon = new Icon(GetType(), "normalIcon.ico");
}
It should look something like this when it’s working.
Conclusion
In this tutorial we have looked at putting your C# program in the system tray, adding a context menu to the tray icon and programmatically changing the tray icon. Any comments and suggestions you have are always appreciated.
4 comments:
Another example in german
thaanks a lot it is very nice post
but what the meaning of
"set the Build Action properties of all your icons to Embedded Resource. "
I just make add existing items but there is error
after adding your Icons, you will find them in your solution explorer.Right Click on the Icon and choose properties you will find a property called "Build Action" , change its value to Embedded Resource
note: your icons extensions should be .ico
Post a Comment