Click here to Skip to main content
Click here to Skip to main content

Adding tray icons and context menus

By , 10 Apr 2002
 

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Nish Sivakumar
United States United States
Member
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberleenert3 Jan '11 - 0:32 
GeneralMy vote of 5memberCibergod2 Aug '10 - 1:28 
GeneralNotify Icon's and timers in an applicationmemberabhey Handa7 Dec '09 - 10:18 
Questionthis solution does not workmemberhoney.hv31 Jul '09 - 21:04 
QuestionThis Solution does not workmemberhoney.hv31 Jul '09 - 20:56 
Generalcodememberyingxuzhong20 Feb '09 - 2:12 
Questionwhat about limits in the tooltipmemberabc@tempinbox.com7 Feb '08 - 8:56 
GeneralthanksmemberEvilInide9 Jul '07 - 23:48 
QuestionHow To Add Icon To FormmemberAnkush Komarwar21 Feb '07 - 19:29 
QuestionRe: How To Add Icon To FormmemberSkippums25 Apr '07 - 13:58 
AnswerRe: How To Add Icon To Formmemberpeterchen8 Aug '07 - 10:47 
QuestionRe: How To Add Icon To FormmemberPoggel, Steven Buraje2 Oct '07 - 21:56 
AnswerRe: How To Add Icon To Formmemberpeterchen3 Oct '07 - 2:07 
QuestionHelpmemberashokkumar7679 Jan '07 - 22:45 
QuestionWindows Service Tray Icons?membertheCodeDevil25 Sep '06 - 17:01 
GeneralNice codememberTejpal Garhwal11 Sep '06 - 9:22 
GeneralError - Form1 already has a disposememberRheal_Notes27 Jul '06 - 11:33 
GeneralCancel context menumemberFylom13 Apr '06 - 5:53 
GeneralMinimize to System Traymembertom183322 Mar '06 - 5:37 
GeneralVery GoodmemberOwen Hines19 Feb '06 - 10:56 
GeneralNice ArticlememberAngel_Komarov17 Feb '06 - 16:33 
QuestionWindows Mobile Tray IconmemberRedaemon29 Dec '05 - 7:28 
AnswerRe: Windows Mobile Tray IconmemberAjatashatru4 Aug '06 - 1:44 
QuestionWhat about the Compact Frameworkmemberddas7710 Nov '05 - 2:11 
AnswerRe: What about the Compact Frameworkmemberlisagitel2 Aug '07 - 10:08 
QuestionYou have any idea how to manipulate/access clock in status areamemberillegalguy7 Nov '05 - 13:27 
GeneralNice, NishmemberJon Sagara16 Aug '05 - 18:34 
GeneralUrgent Help Needed :context menu does not appearmemberGul Hunerkar13 Jul '05 - 1:42 
GeneralthanksmemberGul Hunerkar11 Jul '05 - 0:03 
GeneralDesktop switchmemberprof.hell24 Jun '05 - 12:21 
Hi,
 
Lovely article, and very useful. Well done.
 
Here is a question people .. I am implementing a desktop switch to lock down a machine until it receives an unlocking command on a TCP port. I am in the process of doing this through a tray icon application, and most of the mechanism works.
 
Problem: when I'm in desktop 1, say, and the tray app is running, listening to the port, and it gets an unlock command, I want to switch to desktop 2, with full explorer access, but I want to take the tray app with me from desktop 1 so that I could lock the desktop again. This is for a cyber caf control application.
 
Any ideas ?
 
Thanks so much.
 
Ahmed.

QuestionHow to detect double clicking on Notification IconmemberBraulio Díez15 Apr '05 - 10:59 
AnswerRe: How to detect double clicking on Notification IconmemberBraulio Díez15 Apr '05 - 11:24 
GeneralShowing a balloon from the tray in C#memberBlueGuru23 Sep '04 - 11:10 
GeneralNice...memberBassam Abdul-Baki14 Jun '04 - 6:09 
GeneralYour Article.membereggie521 Feb '04 - 13:16 
GeneralDoing the same for a Console Applicationmembermattdruid29 Feb '04 - 10:45 
GeneralRe: Doing the same for a Console ApplicationmemberSufian Mehmood Sheikh18 Jul '05 - 23:43 
GeneralOwnerDraw and NotifyIconmemberDalePres23 Jan '04 - 14:30 
Questionvb?membercoolerbob24 Nov '03 - 4:39 
Generaldisable tray menu itemmembermartinbe22 Nov '03 - 14:16 
GeneralGhost icon remains in tray upon Exit(), when this.Visible set to falsememberoblius27 Sep '03 - 21:33 
GeneralRe: Ghost icon remains in tray upon Exit(), when this.Visible set to falsememberPeter Greenall17 Feb '04 - 7:49 
GeneralRe: Ghost icon remains in tray upon Exit(), when this.Visible set to falsesussDotnetjunky30 Jun '04 - 20:13 
GeneralExtends possibilities of NotifyIconmemberJremaud6 Jul '03 - 22:06 
GeneralWrong IconsussErald Kulk22 Jun '03 - 23:11 
AnswerRe: Wrong IconmemberRichard Poole8 Nov '05 - 2:33 
GeneralHide the form window on startupmemberBryan Mau1 Dec '02 - 2:26 
GeneralRe: Hide the form window on startupmemberRichard Birkby13 Dec '02 - 7:17 
GeneralRe: Hide the form window on startupmemberPooran Prasad R.20 Jan '03 - 6:42 
GeneralRe: Hide the form window on startupmemberRichard Funke11 Dec '03 - 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 11 Apr 2002
Article Copyright 2002 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid