Click here to Skip to main content
11,576,692 members (57,942 online)
Click here to Skip to main content

An Alternative Implementation of the NotifyIcon Class

, 12 Apr 2002 330.1K 12.2K 142
Rate this:
Please Sign up or sign in to vote.
A wrapper class for notification icons (tray icons) with support for balloon tips.
<!-- Article image -->

Sample Image - NotifyIconEx_img.png

<!-- Add the rest of your HTML here -->

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

Share

About the Author


You may also be interested in...

Comments and Discussions

 
GeneralExplorer Crash and UAC Pin
Elmue7-Nov-09 12:02
memberElmue7-Nov-09 12:02 
General.NET standard NotifyIcon Pin
abnote_au26-Mar-09 16:16
memberabnote_au26-Mar-09 16:16 
QuestionNice work - Allowed for commercial use? Pin
electrowizard14-Jul-08 4:58
memberelectrowizard14-Jul-08 4:58 
AnswerRe: Nice work - Allowed for commercial use? Pin
electrowizard14-Jul-08 5:00
memberelectrowizard14-Jul-08 5:00 
GeneralDuplicate Tray Icon Pin
Eylon Yogev.21-May-07 10:10
memberEylon Yogev.21-May-07 10:10 
Generalwhere I look for the value ? [modified] Pin
Junrich11-May-07 17:11
memberJunrich11-May-07 17:11 
GeneralNotifyIconEx Sample Component_CLASS Pin
Deemc23-Aug-06 16:17
memberDeemc23-Aug-06 16:17 
QuestionChange Right to Left Pin
Mahdi Afsar28-Jul-06 15:24
memberMahdi Afsar28-Jul-06 15:24 
GeneralTimeout values Pin
coolman545327-Apr-06 7:23
membercoolman545327-Apr-06 7:23 
GeneralProblems under Citrix Pin
mav.northwind4-Aug-05 2:24
membermav.northwind4-Aug-05 2:24 
GeneralBalloon does not work on win2k3 Pin
Brad.Net2-Jun-05 6:36
memberBrad.Net2-Jun-05 6:36 
GeneralDrag and Drop Pin
adneale22-May-05 7:23
memberadneale22-May-05 7:23 
GeneralHiding the balloon again Pin
DummyDUmb22-Apr-05 2:07
memberDummyDUmb22-Apr-05 2:07 
GeneralRe: Hiding the balloon again Pin
Kieran Lee11-Jan-06 23:06
memberKieran Lee11-Jan-06 23:06 
GeneralNIN_BALLOONTIMEOUT Pin
dubitskiy1-Feb-05 23:37
memberdubitskiy1-Feb-05 23:37 
GeneralGreat job Pin
pekica5-Aug-04 4:01
memberpekica5-Aug-04 4:01 
GeneralRe: Great job Pin
Taha Zayed24-Sep-04 15:41
memberTaha Zayed24-Sep-04 15:41 
GeneralVb.net Usage Assistance Needed Pin
intel9611-May-04 2:57
memberintel9611-May-04 2:57 
GeneralRe: Vb.net Usage Assistance Needed Pin
meraydin7-May-05 13:56
membermeraydin7-May-05 13:56 
GeneralBetter... Pin
lianwk@pc.jaring.my7-Apr-04 22:54
memberlianwk@pc.jaring.my7-Apr-04 22:54 
GeneralFormatted text Pin
Jonathan Cooper7-Apr-04 8:59
sussJonathan Cooper7-Apr-04 8:59 
GeneralBalloon not working as expected Pin
smurtagh9-Dec-03 3:12
membersmurtagh9-Dec-03 3:12 
GeneralRe: Balloon not working as expected Pin
Xenite11-Mar-05 10:51
memberXenite11-Mar-05 10:51 
GeneralStrange thing happen if explorer dies Pin
smurtagh9-Dec-03 2:54
membersmurtagh9-Dec-03 2:54 
GeneralRe: Strange thing happen if explorer dies Pin
cimnik0294-Jan-05 14:22
membercimnik0294-Jan-05 14:22 
AnswerRe: Strange thing happen if explorer dies Pin
Member 469200814-Aug-08 3:13
memberMember 469200814-Aug-08 3:13 
QuestionHelp! Did anyone get click events to work with Win2k? How? Pin
jl32327-Oct-03 8:03
memberjl32327-Oct-03 8:03 
GeneralThe Tooltip text doesn't change! Pin
kvanpat79030-Aug-03 8:48
memberkvanpat79030-Aug-03 8:48 
GeneralRe: The Tooltip text doesn't change! Pin
kvanpat79030-Aug-03 9:17
memberkvanpat79030-Aug-03 9:17 
GeneralError in TaskBarCreated Pin
VanRoy24-Jul-03 0:14
memberVanRoy24-Jul-03 0:14 
GeneralRe: Error in TaskBarCreated Pin
Joel Matthias25-Jul-03 9:55
memberJoel Matthias25-Jul-03 9:55 
GeneralBalloon click event and W2k Pin
EmaBa14-May-03 6:33
memberEmaBa14-May-03 6:33 
QuestionIs it possible to display web url in the balloon? Pin
APINTO11-Mar-03 9:53
memberAPINTO11-Mar-03 9:53 
AnswerRe: Is it possible to display web url in the balloon? Pin
Joel Matthias11-Mar-03 9:58
memberJoel Matthias11-Mar-03 9:58 
AnswerRe: Is it possible to display web url in the balloon? Pin
Joel Matthias11-Mar-03 10:04
memberJoel Matthias11-Mar-03 10:04 
GeneralRe: Is it possible to display web url in the balloon? Pin
APINTO12-Mar-03 7:08
memberAPINTO12-Mar-03 7:08 
GeneralRe: Is it possible to display web url in the balloon? Pin
Joel Matthias12-Mar-03 7:32
memberJoel Matthias12-Mar-03 7:32 
GeneralSource Safe Pin
Jeff Palmer6-Mar-03 5:25
memberJeff Palmer6-Mar-03 5:25 
GeneralRe: Source Safe Pin
Joel Matthias6-Mar-03 5:51
memberJoel Matthias6-Mar-03 5:51 
GeneralDoes this class work on Windows 98 Pin
MarkLin3-Mar-03 21:29
memberMarkLin3-Mar-03 21:29 
GeneralRe: Does this class work on Windows 98 Pin
Joel Matthias4-Mar-03 4:41
memberJoel Matthias4-Mar-03 4:41 
GeneralRe: Does this class work on Windows 98 Pin
Anonymous2-Jan-05 4:47
sussAnonymous2-Jan-05 4:47 
GeneralVB.Net Pin
MarkLin23-Feb-03 22:23
memberMarkLin23-Feb-03 22:23 
GeneralRe: VB.Net Pin
Joel Matthias3-Mar-03 11:39
memberJoel Matthias3-Mar-03 11:39 
GeneralRe: VB.Net Pin
intel9610-May-04 10:03
memberintel9610-May-04 10:03 
QuestionHow do you feel about people copying your code? Pin
Simon Gill31-Jan-03 4:22
memberSimon Gill31-Jan-03 4:22 
AnswerRe: How do you feel about people copying your code? Pin
Joel Matthias1-Feb-03 7:49
memberJoel Matthias1-Feb-03 7:49 
QuestionHide Balloon? Pin
FrankyFraser19-Jan-03 9:13
sussFrankyFraser19-Jan-03 9:13 
AnswerRe: Hide Balloon? Pin
Alex Gnauck2-May-03 22:45
sussAlex Gnauck2-May-03 22:45 
GeneralRe: Hide Balloon? Pin
vitaminchik16-Mar-04 9:07
membervitaminchik16-Mar-04 9:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 13 Apr 2002
Article Copyright 2002 by Joel Matthias
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid