.NET System Tray Icon Drawing In Windows Forms

Demonstrates how to use the Bitmap, Graphics, Icon classes to dynamically render an icon in the System Tray and give it a context menu.

  Download C# Source Code

The little code sample below demonstrates how to dynamically create icons for the system tray using GDI+.  In the sample, we'll use a windows form and a timer that increments every second.  At each timer interval, we'll draw the icon from scratch without a base image to work from.  It will have a transparent background, a Turquoise circle, and the current second displayed in black.  And, just for kicks, we'll add different right click context menus at runtime and assign them to the icon based on the current timer interval.

// Form1.cs
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;  
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

namespace NotifyIcon

   public class Form1 : System.Windows.Forms.Form
     private System.Windows.Forms.Timer timer1;
     private System.Windows.Forms.NotifyIcon notifyIcon1;
     private System.ComponentModel.IContainer components;

     public Form1()


     private System.Drawing.Icon DrawIcon(string IconMessage)

       Icon oIcon = null;
       int dimension=16;
                 var bm = new Bitmap(dimension,dimension);
                 var g = Graphics.FromImage((Image)bm);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                 var oFont = new Font("Arial",8,FontStyle.Regular,GraphicsUnit.Pixel);
                 g.FillRectangle(Brushes.Transparent,new Rectangle(0, 0, bm.Width, bm.Height));  
                g.FillEllipse(Brushes.Turquoise ,0,0,dimension,dimension);  
                 g.DrawString(IconMessage,oFont,new SolidBrush(System.Drawing.Color.Black), 2,3);
                oIcon = Icon.FromHandle(bm.GetHicon());
       catch (Exception e)

       return oIcon;


     private void timer1_Tick(object sender, System.EventArgs e)
       int Seconds = System.DateTime.Now.Second;
       System.Windows.Forms.ContextMenu oMenu=null;
       this.notifyIcon1.Icon = DrawIcon(Seconds.ToString());  
       oMenu = new System.Windows.Forms.ContextMenu();

       if ((Seconds>0) && (Seconds<10))
         oMenu.MenuItems.Add("Less Than 10",new EventHandler(LessThanTen1_Popup));  
         oMenu.MenuItems.Add("Less Than 10",new EventHandler(LessThanTen2_Popup));  
         this.notifyIcon1.ContextMenu = oMenu;
       oMenu.MenuItems.Add("Default Menu Pop Up",new EventHandler(DefaultIcon_Popup));
       this.notifyIcon1.ContextMenu = oMenu;

     private void LessThanTen1_Popup(object sender, System.EventArgs e)
       MessageBox.Show("less than 10 item 1");

     private void LessThanTen2_Popup(object sender, System.EventArgs e)
       MessageBox.Show("less than 10 item 2");

     private void DefaultIcon_Popup(object sender, System.EventArgs e)
       MessageBox.Show("Default Menu Item");

     protected override void Dispose( bool disposing )
       if( disposing )
           if (components != null)
         base.Dispose( disposing );

     #region Windows Form Designer generated code

     private void InitializeComponent()
       this.components = new System.ComponentModel.Container();
       this.timer1 = new System.Windows.Forms.Timer(this.components);
       this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

       this.timer1.Enabled = true;
       this.timer1.Interval = 1000;
       this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

       this.notifyIcon1.Text = "notifyIcon1";
       this.notifyIcon1.Visible = true;

       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
       this.ClientSize = new System.Drawing.Size(292, 273);
       this.Name = "Form1";
       this.Text = "Form1";


     static void Main()
      Application.Run(new Form1());


By Robbe Morris   Popularity  (5003 Views)
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP
Here's my most recent course on Pluralsight. I think it has some interesting insight on IT professional job interviews and using words in your resume to influence the questions you'll be asked. Resumes, Job Seeking, and Interviews in context.