How To Minimize Your Application To System Tray In C#

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.

For this purpose, we have an option to minimize (or close) the program to the system tray (officially known as the Notification Area) that is usually located next to the Taskbar in the bottom-right corner. In the system tray, each application is represented by a small icon, so it is hidden from your eyes while still providing the ability to quickly access it. Many long-running programs like chats or anti-viruses use this approach so they don't bother the user's view. Still they continue to work in the background and are able to notify the user with a balloon tip if needed or if some specific event occurs.

NotifyIcon and System Tray

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.

 
Image 01: Toolbox and NotifyIcon control
 
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.

  1. private void Form1_Resize(object sender, EventArgs e)  
  2. {  
  3.      //if the form is minimized  
  4.      //hide it from the task bar  
  5.      //and show the system tray icon (represented by the NotifyIcon control)  
  6.      if (this.WindowState == FormWindowState.Minimized)  
  7.      {  
  8.           Hide();  
  9.           notifyIcon.Visible = true;                  
  10.      }  
  11. }  

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.

  1. private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)  
  2. {  
  3.      Show();  
  4.      this.WindowState = FormWindowState.Normal;  
  5.      notifyIcon.Visible = false;  
  6. }  

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 
 
  1. 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.

Up Next
    Ebook Download
    View all
    Learn
    View all