Add Icon to notification area in Windows tray

During a recent conversation with a client they mentioned they wanted a Windows Forms Application to pull information from their SharePoint site. We discussed the requirements and I said yes that fine we can do that. They then said they would like the application to run in the background and to have an Icon in the Notification area, see screenshot below, which pops up telling them some new content has been added to SharePoint.

NotificationArea

My immediate reaction was it must be relatively easy but it was not something I had tried before. When I got back to the office I created a test application and I was surprised by how easy this was. I have included below some same code which hides the application when it is started, adds an item to the notification area and displays a popup every 10 seconds. If this popup is click it will open the application or if the icon in the notification area is click it will also open the application.

Hopefully you find this useful and can use this as a starting point for further developments. As always this code should always be tested before being deployed to any live environments and is used at your own risk.

Code Snippet
  1. public partial class Form1 : Form
  2.     {
  3.         NotifyIcon notify;
  4.         Timer timer;
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.             timer = new Timer();
  9.             timer.Interval = 10000;
  10.                 timer.Start();
  11.             timer.Tick +=new EventHandler(timer_Tick);
  12.  
  13.             notify = new NotifyIcon();
  14.             notify.Icon =
  15.    new System.Drawing.Icon(System.Environment.GetFolderPath
  16.    (System.Environment.SpecialFolder.Personal)
  17.    + @"\logo.ico");
  18.             notify.Visible = true;
  19.             notify.Text = "Test notification area icon";
  20.  
  21.             this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
  22.             this.Visible = false;
  23.             this.ShowInTaskbar = false;
  24.         }
  25.  
  26.         public void  timer_Tick(object sender, EventArgs e)
  27.         {
  28.             notify.Click += new EventHandler(notify_Click);
  29.             notify.ShowBalloonTip(3000, "Test notification area header", "Test notification area test", ToolTipIcon.Info);
  30.             notify.BalloonTipClicked += new EventHandler(notify_BalloonTipClicked);
  31.         }
  32.  
  33.         void notify_Click(object sender, EventArgs e)
  34.         {
  35.             this.WindowState = System.Windows.Forms.FormWindowState.Normal;
  36.             this.Visible = true;
  37.         }
  38.  
  39.         void notify_BalloonTipClicked(object sender, EventArgs e)
  40.         {
  41.             this.WindowState = System.Windows.Forms.FormWindowState.Normal;
  42.             this.Visible = true;
  43.         }
  44.     }

Advertisements

One Response to Add Icon to notification area in Windows tray

  1. Surinder says:

    Nice example. I needed to do something similar.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: