Minimize the application to the Tray system is done with the control NotifyIcon
of Visual Studio.
NotifyIcon
is in namespace System.Windows.Forms.
1. Notifyicon control
Drag and drop control NotifyIcon
for your form and put your name on the same notifyIcon1.
2. Changing the Notifyicon icon
We need to change the icon of NotifyIcon
for it to appear on system Tray, otherwise nothing will show. For this, put the following line of code in your form’s constructor:
public Form1()
{
InitializeComponent();
notifyIcon1.Icon = new Icon(GetType(), "placeholder.ico");
}
The icon must be a . ico and must be set to Embeddedresource in the icon properties after adding it to the project.
3. Configuring the form
No . Cs of your Form, put the following:
private bool allowVisible;
protected override void SetVisibleCore(bool value) {
if (!allowVisible) {
value = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(value);
}
protected override void OnFormClosing(FormClosingEventArgs e) {
if (!allowClose) {
this.Hide();
e.Cancel = true;
}
base.OnFormClosing(e);
}
3. Showing the application
Enter the code below in the event DoubleClick
of control NotifyIcon
to display your application by double-clicking the icon on Tray system:
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
allowVisible = true;
Show();
}
Once you start your application, it will go straight to the Tray system with the icon you set earlier. To open the application, simply double-click on the icon on Tray system.
I have a personal project at Github, a mini web server made in C#, that does just that. It starts only with an icon in the Windows notification bar, and when you right click on it a menu with the program options appears, when you left click a terminal screen appears, showing the log events.
– Miguel Angelo