Skip to main content
Email Password   helpLost your password?

Introduction

As MFC/SDK programmers move into .NET, what surprises them most is the fact that everything is now so much more easier then ever before. Christian Graus was complaining that it was too easy and that the abominable allowance of gotos annoyed him. He might have a point there, but making coding easy is not such a bad thing after all. It's funny when you think of all the effort Chris Maunder and others put into those MFC & SDK tray icon classes that are ever so popular with copy/paste programmers. I dedicate this article to Chris M and others involved in the brilliant tray icon class project over the last few years.

Adding  the icon to your project

Ctrl-Shift-A will bring up the Add-New-Item dialog box. Select Icon File from the list of available templates. If the list is too populated to your liking select Resources from the tree control on the left. This will bring up a smaller list on the right and it will be easier for you to select Icon File. Now click open. You'll end up with the VS.NET icon editor. You may now create your icon here or copy/paste an icon from elsewhere.

Now right click on this icon from Solution Explorer. Take properties. And change the Build Action property to Embedded Resource. This will instruct the compiler to embed this icon along with your EXE file, thus saving you the annoyance of having to distribute the icon with your EXE.

Adding the NotifyIcon member to your form

Okay. Now that we have our icon ready we need to add it to our form class.

private NotifyIcon m_notifyicon;

Alright, so we have added a NotifyIcon member. Now let's initialize it and set some default properties. This should be done from the form object's constructor.

m_notifyicon = new NotifyIcon();
m_notifyicon.Text = "Hello, what's cooking?"; 
m_notifyicon.Visible = true; 
m_notifyicon.Icon = new Icon(GetType(),"Icon1.ico"); //Thanks to Hasaki for this.

Alright, now compile and run your program. You'll see the icon in your tray. That was rather simple, huh? But usually people like to add a context menu to their tray icons. A tray icon simply sitting there is not very useful.

Adding a context menu to the tray icon

The first thing we need to do is to add a ContextMenu member to our form.

private ContextMenu m_menu;  

Now we need to initialize it and add some menu items.

m_menu = new ContextMenu(); 
m_menu.MenuItems.Add(0, 
    new MenuItem("Show",new System.EventHandler(Show_Click))); 
m_menu.MenuItems.Add(1, 
    new MenuItem("Hide",new System.EventHandler(Hide_Click))); 
m_menu.MenuItems.Add(2, 
    new MenuItem("Exit",new System.EventHandler(Exit_Click)));

We have added three menu items and have also associated click event handlers for each of those menu items. I could have created an array of MenuItem objects but that's not really needed here.

Now we need to associate this ContextMenu with our tray icon. So we do this.

m_notifyicon.ContextMenu = m_menu;

Now let's fill up those event handlers.

protected void Exit_Click(Object sender, System.EventArgs e) 
{
    Close();
}
protected void Hide_Click(Object sender, System.EventArgs e) 
{
    Hide();
}
protected void Show_Click(Object sender, System.EventArgs e) 
{
    Show();
}

Okay. Compile and run it. Now right clicking on the tray icon brings up the context menu. You can hide and show the form window using the two menu options. And the "Exit" option will exit the application.

A small problem

Now you'll notice a slight annoyance. The tray icon does not vanish when you exit the program. But when you move the mouse over the tray the icon vanishes. So, what do we do to avoid that? Again as with everything else with this whole .NET thing, it's as easy as 1,2,3. Override your form object's Dispose function and put the following lines of code into it.

protected override void Dispose( bool disposing ) 
{ 
    if( disposing ) 
    { 
        this.m_notifyicon.Dispose(); //we dispose our tray icon here

    }
    base.Dispose( disposing );
}

Full Source Listing

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace TrayTest
{
    public class Form1 : System.Windows.Forms.Form
    {
        private NotifyIcon m_notifyicon;    
        private ContextMenu m_menu;        

        public Form1()
        {
            Text = "TrayIcon test program";    
        
            m_menu = new ContextMenu();                                    
            m_menu.MenuItems.Add(0,
                new MenuItem("Show",new System.EventHandler(Show_Click)));
            m_menu.MenuItems.Add(1,
                new MenuItem("Hide",new System.EventHandler(Hide_Click)));
            m_menu.MenuItems.Add(2,
                new MenuItem("Exit",new System.EventHandler(Exit_Click)));

            m_notifyicon = new NotifyIcon();
            m_notifyicon.Text = "Right click for context menu";
            m_notifyicon.Visible = true;
            m_notifyicon.Icon = new Icon(GetType(),"Icon1.ico");
            m_notifyicon.ContextMenu = m_menu;            
            
        }
        
        protected void Exit_Click(Object sender, System.EventArgs e) 
        {
            Close();
        }
        protected void Hide_Click(Object sender, System.EventArgs e) 
        {
            Hide();
        }
        protected void Show_Click(Object sender, System.EventArgs e) 
        {
            Show();
        }
        
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                this.m_notifyicon.Dispose();
            }
            base.Dispose( disposing );
        }
        
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }
        
    }
}

        

Conclusion

I would like to thank James Johnson for his valuable tips while I was struggling with embedding icons into my exe. Also a special thanks to Colin for keeping me cheered up with Bobs while I was burying myself in despair after my bad experiences with the new CD writer.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Questionthis solution does not work Pin
honey.hv
22:04 31 Jul '09  
QuestionThis Solution does not work Pin
honey.hv
21:56 31 Jul '09  
Generalcode Pin
yingxuzhong
3:12 20 Feb '09  
Questionwhat about limits in the tooltip Pin
abc@tempinbox.com
9:56 7 Feb '08  
Generalthanks Pin
EvilInide
0:48 10 Jul '07  
QuestionHow To Add Icon To Form Pin
Ankush Komarwar
20:29 21 Feb '07  
QuestionRe: How To Add Icon To Form Pin
Skippums
14:58 25 Apr '07  
AnswerRe: How To Add Icon To Form Pin
peterchen
11:47 8 Aug '07  
QuestionRe: How To Add Icon To Form Pin
Poggel, Steven Buraje
22:56 2 Oct '07  
AnswerRe: How To Add Icon To Form Pin
peterchen
3:07 3 Oct '07  
QuestionHelp Pin
ashokkumar767
23:45 9 Jan '07  
QuestionWindows Service Tray Icons? Pin
theCodeDevil
18:01 25 Sep '06  
GeneralNice code Pin
Tejpal Garhwal
10:22 11 Sep '06  
GeneralError - Form1 already has a dispose Pin
Rheal_Notes
12:33 27 Jul '06  
GeneralCancel context menu Pin
Fylom
6:53 13 Apr '06  
GeneralMinimize to System Tray Pin
tom1833
6:37 22 Mar '06  
GeneralVery Good Pin
Owen Hines
11:56 19 Feb '06  
GeneralNice Article Pin
Angel_Komarov
17:33 17 Feb '06  
QuestionWindows Mobile Tray Icon Pin
Redaemon
8:28 29 Dec '05  
AnswerRe: Windows Mobile Tray Icon Pin
Ajatashatru
2:44 4 Aug '06  
QuestionWhat about the Compact Framework Pin
ddas77
3:11 10 Nov '05  
AnswerRe: What about the Compact Framework Pin
lisagitel
11:08 2 Aug '07  
QuestionYou have any idea how to manipulate/access clock in status area Pin
illegalguy
14:27 7 Nov '05  
GeneralNice, Nish Pin
Jon Sagara
19:34 16 Aug '05  
GeneralUrgent Help Needed :context menu does not appear Pin
Gul Hunerkar
2:42 13 Jul '05  


Last Updated 10 Apr 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009