How to Minimize application to system tray in C#

By FoxLearn 7/20/2024 2:31:24 AM   14.92K
Minimizing an application to the system tray in a C# Windows Forms application involves a few steps.

Open your Visual Studio, then create a new Windows Forms application project.

Next, Drag and drop a NotifyIcon control from the Visual Studio toolbox to your form. This control represents an icon that appears in the system tray.

C# start application minimized to tray

Clicking the arrow icon on NotifyIcon, then select an icon.

c# notifyicon

Set the Icon property of the NotifyIcon control to the icon you want to display in the system tray. Set the Visible property to false initially, as you don't want the icon to be visible until the application is minimized

Adding a Load event handler to your form allows you to set title and content for the notifyicon.

// c# minimize to tray
private void frmSystemTray_Load(object sender, EventArgs e)
    notifyIcon1.BalloonTipText = "Application Minimized.";
    notifyIcon1.BalloonTipTitle = "FoxLearn";

Adding a Resize event handler to your form allows you to detect when the form is minimized and hide it from the taskbar. You can easily check the status of the window if it is minimized we will display the notification icon in the system tray.

// c# form minimize
private void frmSystemTray_Resize(object sender, EventArgs e)
    // c# form minimize event
    if (this.WindowState == FormWindowState.Minimized)
        ShowInTaskbar = false;
        notifyIcon1.Visible = true;

C# system tray notification

Adding a MouseDoubleClick event handler to the NotifyIcon allows you to restore the form when the user double-clicks the icon in the system tray.

// c# windows form minimize to system tray
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    ShowInTaskbar = true;
    notifyIcon1.Visible = false;
    WindowState = FormWindowState.Normal;

C# run application in system tray

When you run and minimize apps you'll see a notification in the system tray.

c# run application in system tray

Make sure to set the frmSystemTray_Resize event handler for the form's Resize event, and set the notifyIcon1_MouseDoubleClick event handler for the NotifyIcon's MouseDoubleClick event.

This code will hide the form and display the NotifyIcon in the system tray when the form is minimized, and restore the form when the NotifyIcon is double-clicked. Through this c# example, I showed you how to create a system tray icon in a windows application using c#.