The ability to minimize the application to the Taskbar is a standard behavior of the vast majority of Windows applications. This is ideal for programs you need quick access to and work frequently with. But what if you need to have an application opened that performs some background workload and you access it only occasionally? Minimizing such app to the Taskbar will populate it for no reason and create a mess of open applications you have a hard time to orientate within.
To be able to use the system tray, we use the NotifyIcon control from the System.Windows.Forms namespace. Therefore, after creating a regular Windows Forms project, the first step is to drag and drop the NotifyIcon control from the toolbox to the form.
Once done, there are several NotifyIcon's properties we should pay attention to.
Important NofityIcon properties:
- BalloonTipIcon: specifies the icon that will be shown with the balloon tip.
- BalloonTipText: the actual text that will be displayed on the balloon tip.
- BalloonTipTitle: the title of the balloon tip.
- ContextMenuStrip: context menu that is associated with the NotifyIcon.
- Icon: probably the most important property that represents the icon that will be shown in the system tray. Only .ico files can be used.
- Text: the text that will be shown when you hover your mouse on the icon in the system tray.
- Visible: indicates whether the icon is visible in the system tray.
For now, we will set the icon, text and also the visibility to false since we don't want the icon to be seen right from the start of the application. We want it to be shown only when the application is minimized.
Image 02: NotifyIcon's properties
Now when the NotifyIcon control is properly set, we need to alter the application's behavior.
Minimizing To System Tray
When a system tray is used, the typical behavior is that the application minimizes to the system tray instead of the Taskbar. Therefore, the form's resize event is the one we need to pay attention to. What we basically need to do is check whether the form has been minimized and if so, hide it from the Taskbar and set the NotifyIcon's visibility to True to display it in the system tray.
- private void Form1_Resize(object sender, EventArgs e)
- {
-
-
-
- if (this.WindowState == FormWindowState.Minimized)
- {
- Hide();
- notifyIcon.Visible = true;
- }
- }
So far so good, our simple WebForms application can be minimized to the system tray with just a few lines of code. But how do we bring the application back to life when minimized? Simply by responding to one of the click events of the NotifyIcon control. We will choose the MouseDoubleClick event and in its response we will un-minimize the application, show it on the Taskbar once again and hide the system tray icon.
- private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- Show();
- this.WindowState = FormWindowState.Normal;
- notifyIcon.Visible = false;
- }
Now, we have successfully integrated the system tray icon into our application. When minimized, a neat icon is shown along with some information text when we hover the mouse over the icon.
Image 03: application icon show in the system tray
Adding the BalloonTip
Adding some handy information in the form of a balloon tip is as simple as filling in a few related properties and calling the ShowBallonTip method with the timespan in a form of milliseconds. This specifies how long the balloon tip will be shown. As you may have guessed, the right place to call this method is the resize event.
There are 3 properties related to the balloon tip, the title, the actual text and an icon (none/error/info/warning).
Image 04: BalloonTip related properties
- notifyIcon.ShowBalloonTip(1000);
Image 05: Balloon tip in action
Note: If you leave the BalloonTipTitle blank, the BalloonTipIcon won't be shown.
Summary
System tray is a great alternative of a standard application's behavior when it is being minimized. Instead of minimizing to the Taskbar a system tray is used with a smaller icon that doesn’t bother the user’s view. Still, the application is quickly accessible.
The System tray is suited for applications that run in the background to perform its tasks and the user’s cooperation is decreased to a minimum. We use the NotifyIcon control to implement it.