How to Minimize application to system tray in C#
By FoxLearn 12/26/2024 7:20:47 AM 15.71K
One of the most convenient features in Windows is the Taskbar. It not only keeps your open applications organized but also allows you to hide icons, making it easy to keep things out of sight.
In this tutorial, we will use Visual Studio, which makes this process quite simple.
How to Minimize an Application to the Taskbar Tray in C#
Open your Visual Studio, then create a new Windows Forms application project.
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.
Setting Up the NotifyIcon
The key to this functionality is the NotifyIcon object. NotifyIcon is designed to simplify tasks like adding an icon to the Taskbar and sending notifications to users.
Clicking the arrow icon on NotifyIcon, then select an icon.
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 Balloon Notifications
One of the best features of NotifyIcon is the ability to show notifications. These are the "balloon" tips that pop up from the Taskbar. You can use this to notify the user about various events in your app, such as when it's 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; notifyIcon1.ShowBalloonTip(1000); } }
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
This will show a notification bubble with a title and message when the app is minimized. You can customize the text and duration of the bubble as needed. The NotifyIcon object opens up endless possibilities for notifications, and you can use it for other actions in your app as well, not just minimizing.
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#.