Adding tray icons and context menus

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.

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.

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.

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.

Share

About the Author

Nish Nishant is the Principal Software Architect/Consultant for Ganymede Software Solutions LLC, and is based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Comments and Discussions

i have a problem , when i follow your solution step by step in VS2008 and build my project ,finally I run bin\Release\Myproject.exe and recieve a windows message "Myproject.ex has encountered a problem and needs to close. We are sorry for the inconvenience."
this is my code