How to Minimize Applications in the System Tray in C#

Adnan Ashraf Feb 02, 2024
  1. Windows Forms Application
  2. System Tray
  3. System Tray and Notification
  4. Minimize Windows Forms Application to the System Tray
  5. Conclusion
How to Minimize Applications in the System Tray in C#

This article explains minimizing Windows Form applications to the System Tray instead of the Taskbar using C#. Before going ahead, we need a little background on Windows Form applications and the System Tray, so let’s start with that.

Windows Forms Application

Windows Forms is a built-in library in the .NET framework used to create Graphical User Interfaces (GUIs) for desktop applications (and web apps as well).

Typical controls on a Windows Forms application include buttons, menus, text fields, checkboxes, etc. A sign-up or a login form can be simple examples of a Window Form application.

You can find more information about the Window Form application here.

System Tray

Almost all the Windows Forms apps have the functionality to minimize the application to the Taskbar as usual routine. This is perfect for applications that are used frequently and require rapid access.

What happens, though, if we need to keep a program running in the background that is only used occasionally? If we minimize such an app toward the Taskbar, it fills up the Taskbar, and the Taskbar becomes cluttered with open programs that are difficult to navigate.

For this purpose, we can minimize the application to the System Tray. The System Tray is located at the bottom-right corner of the Taskbar.

Every application is symbolized by a little icon in the System Tray. The capacity to easily access it is still there, but it is concealed from your view.

Long-running applications like chats and antivirus software frequently use this method to avoid obstructing the user’s view. However, these apps continue to work in the background and can notify the user with a balloon tip if needed or if some specific event occurs.

System Tray and Notification

To use the System Tray, we utilized the NotifyIcon control under the namespace called System.Windows.Forms.

Some important NotifyIcon properties are as follows:

  • BalloonTipIcon: This indicates the icon which will appear with the balloon tip.
  • BalloonTipText: This text will appear in the balloon tip.
  • BalloonTipTitle: This is the title given to the balloon tip.
  • Icon: This is the most important property to be set to an icon file displayed in the System Tray. Only .ico files are supported.
  • Text: This is the text that appears when you move your cursor over the icon in the System Tray.
  • Visible: This is the property to set the icon’s visibility in the System Tray.

Some properties that we will set in our application are as follows.

private void Form1_Load(object sender, EventArgs e) {
  notifyIcon1.BalloonTipTitle = "System Tray";
  notifyIcon1.BalloonTipText = "Some Notification";
  notifyIcon1.Text = "System Tray App";
}

Minimize Windows Forms Application to the System Tray

Here is a step-by-step procedure for minimizing the application on the System Tray instead of the Taskbar.

  • Open the Visual Studio. After launching the Visual Studio, the following menu should appear:

    Visual Studio

  • Select Create a new project from the menu, as shown in the snippet above.
  • Create a Windows Forms application by selecting WindowsFormApp from the menu shown in the application. Then, click on the Next button.

    Create Project

  • Finally, we type the name of the Windows Forms application and click on the Create button, as shown in the figure below:

    Project Name

  • After building a standard Windows Forms application, the first task is to take the NotifyIcon from the toolbar and put it onto the form. Double-click on the toolbox and select the NotifyIcon from the menu, as shown in the figure below:

    System Tray - NotifyIcon

  • After doing this, open the property window of NotifyIcon and set the visible state to False, as shown in the figure below:

    NotifyIcon

  • Select an icon for your application that will display on the System Tray. If you did not select an icon for your application, then you will not see the application in the System Tray after minimizing it.
  • Add the following code in the NotifyIcon in the MouseDoubleClick Method:
    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) {
      this.Show();
      notifyIcon1.Visible = false;
      WindowState = FormWindowState.Normal;
    }
    
  • The next step is to add an event to our form. To do this, double-click on the form and select the resize event from the event menu, as shown below:

    Resizing the Form

  • Now, add the following code to activate the event.
    private void Form1_Resize(object sender, EventArgs e) {
      if (WindowState == FormWindowState.Minimized) {
        this.Hide();
        notifyIcon1.Visible = true;
        notifyIcon1.ShowBalloonTip(1000);
      } else if (FormWindowState.Normal == this.WindowState) {
        notifyIcon1.Visible = false;
      }
    }
    
  • Also, add the following code in the Form1_Load method to set the various properties of NotifyIcon.
    private void Form1_Load(object sender, EventArgs e) {
      notifyIcon1.BalloonTipTitle = "System Tray";
      notifyIcon1.BalloonTipText = "Some Notification";
      notifyIcon1.Text = "System Tray App";
    }
    
  • We are done, so let’s test our application. When we run the application, the following form will be displayed:

    Generated Form

  • The figure given shows the System Tray status before minimizing the app. Here you can see that the app is shown on the Taskbar.

    System Tray Status Before Minimizing

  • When we click the minimize button on the form, as shown below. Then the application will be minimized in the System Tray.

    Generated Form 2

  • The figure shows the System Tray’s status after minimizing the app. Here you can see that the application is shown on the System Tray.

    System Tray Status After Minimizing

Conclusion

The Windows Forms applications are default set to minimize on the Taskbar. However, the applications that usually run in the background (e.g., antivirus software) should not be minimized to the Taskbar, as it is not a good idea to rush the Taskbar for less frequently used background applications.

Thanks to the NotifyIcon in Windows Forms, we can override to minimize these applications to the System Tray instead of the Taskbar.