.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()
     {
       InitializeComponent();

     }

     private System.Drawing.Icon DrawIcon(string IconMessage)
     {

       Icon oIcon = null;
       int dimension=16;
       try
       {
                 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());
                oFont.Dispose();
                g.Dispose();
                bm.Dispose();
       }
       catch (Exception e)
       {
         Debug.WriteLine(e.Message);
       }

       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;
         return;
       }
        
       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)
           {
             components.Dispose();
           }
        }
         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";

    }
    #endregion


    [STAThread]
     static void Main()
    {
      Application.Run(new Form1());
    }

  }
}



By Robbe Morris   Popularity  (3827 Views)
Picture
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