Click here to Skip to main content
15,886,030 members
Articles / Programming Languages / C#
Article

An Alternative Implementation of the NotifyIcon Class

Rate me:
Please Sign up or sign in to vote.
4.95/5 (59 votes)
12 Apr 20023 min read 429.5K   13K   143   115
A wrapper class for notification icons (tray icons) with support for balloon tips.

Sample Image - NotifyIconEx_img.png

Introduction

I recently wrote a Visual Source Safe sniper application. The purpose of this program is to wait patiently in the background for selected Visual Source Safe files to be checked in by fellow programmers. When the files become checked in the sniper checks these file out and notifies the user. This seemed like a perfect job for a notification icon. I was dismayed however to find out that the .NET class library's NotifyIcon class does not support balloon tips which are currently provided by the newer version of the common control library. This led to my CodeProject article 'Displaying a Notify Icon's Balloon Tool Tip'. However this approach was a hack at best and did not allow the application to receive notification when the balloon tip was clicked. So I decided to bite the bullet and write my own NotifyIcon class which would give me the ability to do everything I wanted.

Functionality

I have kept the functionality of this class to the minimum of what I think is useful. For example I do not handle mouse move messages over the notification icon as I can't think of any good use for these messages. However adding this functionality is quite easy.

List of Features

  • Fires the events 'Click', 'Double Click', 'Click Balloon'
  • Declares properties 'Visible', 'Icon', 'Text', 'Context Menu'
  • Removes icon from notification area when garbage collected (there you go Nish)
  • Re-adds icon if explorer restarts
  • Provides the method ShowBalloon(string title, string text, int timeout)

How It Works

My class uses a similar approach to the .NET class in that it creates a hidden window to receive messages from the notify icon. These messages are then forwarded to the main class NotifyIconEx where they are filtered according to the notification icon's numeric ID. My class can be used to add multiple notify icons and all messages are handled by the one hidden window. At this point I must admit that I used the 'Anakrino' decompiler to take a peek at some of the code used to implement .NET's NotifyIcon class. I mainly used this to figure out how to display the context menu at the correct location and how to make sure that when you click outside of the menu it closes for you.

Platform invoke methods are used quite extensively in this class which is unfortunate but necessary. Hopefully as .NET matures the class library will expand to allow more advanced manipulations of windows and system functions but until then it's either scrap the cool feature or fall back to the Win32 API.

Problems Encountered

I must admit that the documentation concerning the balloon tip does not seem to match the behaviour. The timeout value does not seem to work. I could not get the correct windows message when the balloon is closed by the 'x' button. I get the 'timeout' message whether the balloon times out or it is closed by the user. Let me know if I have done anything wrong in regards to these issues or if anybody gets different results than I did on my XP machine.

Watch Out

I must warn you that if you use my NotifyIconEx class at the same time as NotifyIcon strange things may happen. See if you can figure out why.

Sign Off

I think you will find that my class is as simple to use is that supplied by .NET, if not simpler (you do not have to explicitly remove my icon when your application exits). You should be able to add the class to the toolbox and drag and drop it using the form designer as you can with the built in class.

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



Comments and Discussions

 
GeneralRe: Source Safe Pin
Joel Matthias6-Mar-03 5:51
Joel Matthias6-Mar-03 5:51 
GeneralDoes this class work on Windows 98 Pin
MarkLin3-Mar-03 21:29
MarkLin3-Mar-03 21:29 
GeneralRe: Does this class work on Windows 98 Pin
Joel Matthias4-Mar-03 4:41
Joel Matthias4-Mar-03 4:41 
GeneralRe: Does this class work on Windows 98 Pin
Anonymous2-Jan-05 4:47
Anonymous2-Jan-05 4:47 
GeneralVB.Net Pin
MarkLin23-Feb-03 22:23
MarkLin23-Feb-03 22:23 
GeneralRe: VB.Net Pin
Joel Matthias3-Mar-03 11:39
Joel Matthias3-Mar-03 11:39 
GeneralRe: VB.Net Pin
intel9610-May-04 10:03
intel9610-May-04 10:03 
QuestionHow do you feel about people copying your code? Pin
Simon Gill31-Jan-03 4:22
Simon Gill31-Jan-03 4:22 
Have you licensed this code snippet at all yet? I'm thinking that it would be very useful to put in a commercial app I'm working on and I'd like to cover my ass on the legal side.

This is an absolutely fantastic piece of code and does deserve recognition, I can put you in the credits if you'd like.
AnswerRe: How do you feel about people copying your code? Pin
Joel Matthias1-Feb-03 7:49
Joel Matthias1-Feb-03 7:49 
QuestionHide Balloon? Pin
FrankyFraser19-Jan-03 9:13
sussFrankyFraser19-Jan-03 9:13 
AnswerRe: Hide Balloon? Pin
alexgnauck2-May-03 22:45
alexgnauck2-May-03 22:45 
GeneralRe: Hide Balloon? Pin
vitaminchik16-Mar-04 9:07
vitaminchik16-Mar-04 9:07 
GeneralRe: Hide Balloon? Pin
Peppe31615-Jan-05 17:18
Peppe31615-Jan-05 17:18 
GeneralRe: Hide Balloon? Pin
Weiye Chen6-Jul-05 17:02
Weiye Chen6-Jul-05 17:02 
GeneralNumber of characters in the balloon Pin
appwiz30-Dec-02 8:11
appwiz30-Dec-02 8:11 
GeneralRe: Number of characters in the balloon Pin
Joel Matthias3-Jan-03 4:46
Joel Matthias3-Jan-03 4:46 
GeneralAbout Unicode Pin
Member 16490317-Nov-02 16:52
Member 16490317-Nov-02 16:52 
GeneralRe: About Unicode Pin
Joel Matthias18-Nov-02 19:03
Joel Matthias18-Nov-02 19:03 
GeneralRe: About Unicode Pin
Anonymous17-Jul-03 4:35
Anonymous17-Jul-03 4:35 
GeneralDisplaying balloon tooltip for hidden icon Pin
SLiDeR6-Nov-02 8:19
SLiDeR6-Nov-02 8:19 
GeneralRe: Displaying balloon tooltip for hidden icon Pin
Joel Matthias6-Nov-02 8:31
Joel Matthias6-Nov-02 8:31 
GeneralBalloonClick does not work Pin
Uffe28-Oct-02 8:00
Uffe28-Oct-02 8:00 
GeneralRe: Balloonclick does not work Pin
Chad McCune11-Nov-02 6:41
sussChad McCune11-Nov-02 6:41 
GeneralRe: Balloonclick does not work Pin
Anonymous11-Nov-02 7:49
Anonymous11-Nov-02 7:49 
GeneralRe: Balloonclick does not work Pin
Joel Matthias12-Nov-02 19:10
Joel Matthias12-Nov-02 19:10 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.