Minimizing the application into System Tray

Minimizing the application into System Tray: Sometimes we may need to open huge number of programs in the start bar of OS. So we may need to minimize the number of open applications to avoid confusion.

Here is the code snippet that used to minimize the application to the system tray.

Main features....
1. Here in thie code snippet we can use the icon which we like which then will be shown in the system tray.
2. We can control the default visibility.
3. Control the minimize and maximize events when double clicking on the icon.

/*
 * Minimising your application to the system tray
 */

// this variable goes in the main scope of the class
NotifyIcon TrayIcon = new NotifyIcon(); // create the icon to show in the tray

// put this in the constructor on form
this.TrayIcon.Icon = new Icon(".\\icon.ico"); // apply an icon to it
this.TrayIcon.Visible = false; // make sure it isn't displaying by default
this.Resize += new EventHandler(Form_Resize); // add a resize event
this.TrayIcon.MouseDoubleClick += new MouseEventHandler(TrayIcon_MouseDoubleClick);

// then put these 2 methods in your class
void Form_Resize(object sender, EventArgs e)
{
 if (this.WindowState == FormWindowState.Minimized)
 { // if the form has been minimised
  this.Hide(); // hide the form
  this.TrayIcon.Visible = true; // display the tray icon
 }
}

void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
 this.TrayIcon.Visible = false; // hide the tray icon
 this.Show(); // display the window
 this.WindowState = FormWindowState.Normal; // reset the form state from minimised
}

/** EXAMPLE USAGE **/
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DIC
{
 public partial class MainForm : Form
 {
  // this variable goes in the main scope of your class
  NotifyIcon TrayIcon = new NotifyIcon(); // create the icon to show in the tray
  public MainForm()
  {
   InitializeComponent();
   this.Icon = new Icon(".\\icon.ico");
   
   // put this in the constructor for your form
   this.TrayIcon.Icon = new Icon(".\\icon.ico"); // apply an icon to it
   this.TrayIcon.Visible = false; // make sure it isn't displaying by default
   this.Resize += new EventHandler(Form_Resize); // add a resize event
   this.TrayIcon.MouseDoubleClick += new MouseEventHandler(TrayIcon_MouseDoubleClick);
  }

  void Form_Resize(object sender, EventArgs e)
  {
   if (this.WindowState == FormWindowState.Minimized)
   { // if the form has been minimised
    this.Hide(); // hide the form
    this.TrayIcon.Visible = true; // display the tray icon
   }
  }
  
  void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
  {
   this.TrayIcon.Visible = false; // hide the tray icon
   this.Show(); // display the window
   this.WindowState = FormWindowState.Normal; // reset the form state from minimised
  }
 }
}

By Venkat K   Popularity  (1555 Views)
Biography - Venkat K
Completed Masters in Computer Applications. 9+ years experience in asp.net, c#. Proficient in SQL Server. Hands on experiene in MS Excel, MOSS.