(Print this page)

The NotifyIcon class
Published date: Thursday, January 1, 2004
On: Moer and Éric Moreau's web site

The NotifyIcon class lets you add your own application to the System Tray easily. This is located at the bottom right of the screen, just beside the clock. This part of the screen is also called the status notification area in Windows XP and in the MSDN Help. 

A NotifyIcon control also wraps the features of the class. I don't know why but this class cannot be inherited.

Creating a NotifyIcon instance

If you want to create the instance by using the control, you just need to find the control named "NotifyIcon" into the "Windows Forms" tab of your toolbox. The instance will then be created into the designer's tray (the section below the form's designer) of your current form.

You could also create an instance of the class using code. All you need is to write this:

"Private mNotifyIcon As NotifyIcon" 
This line needs to be somewhere in your form's declarations section. If you want the events raised by the class as well (which is normally the case), don't forget to add the WithEvents keyword.

For this article, I will use the control.

Setting the first properties

If you add a NotifyIcon control to a form and if you look at the properties from the properties panel, you will find that the control as not much properties! The main properties are:

Properties Description 
Text The text that will appear as the tool tip when the mouse hovers over the icon into the system tray. 
Icon The icon to display into the system tray. 
Visible Indicates whether the icon is visible or hidden from the system tray. Its value is True by default. 
ContextMenu The menu to show when the user right-clicks the icon.

Responding to events

This control also gives you a very limited number of events.

Events Description 
Click The user clicked the icon. 
DoubleClick The user double-clicked the icon.
MouseDown, MouseUpThe user pressed/released a mouse button on the icon.
MouseMove The user moves the mouse pointer over the icon.

   

Creating a first example

This object is so simple that even now, without more explanations, we are ready for a first example. For this first example, we will just add a feature to send the form to the taskbar or to the system tray. The form will be resized to the normal size when the icon of the system tray is clicked.

Figure 1:The first example

Add a NotifyIcon control to the form and let the properties to their default value. Add a Button control that will be used to minimize the form (so you can name it and label it appropriately). Also add a Checkbox (that will let the user decide if he wants the form in the task bar or in the system tray), and name it chkToSystemTray.

Add these lines to the Form_Load event:

With NotifyIcon1
    .Icon = Me.Icon
    .Text = "UT Mag Notify Icon control demo."
    .Visible = False
End With
These lines are setting the icon to whatever icon is set in the form's icon property. The text that will appear as the tooltip is also set. Finally, the visible property is set false to hide the icon from the tray (the icon will be displayed later when the user selects it).

You also need to add these lines to the Click event of your button to minimize the current form:

NotifyIcon1.Visible = chkToSystemTray.Checked
Me.ShowInTaskbar = Not chkToSystemTray.Checked
Me.WindowState = FormWindowState.Minimized
Depending on the checkbox state, the visibility of the icon in the system tray and the visibility of application in the task bar are set. The form itself is then minimized.

To re-hydrate the form from the icon in the system tray, you need to add these lines of code to the DoubleClick event of the NotifyIcon control:

Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
Me.ShowInTaskbar = True
It is now ready to run! Change the state of the checkbox and click the button to minimize the form. You can see the tooltip appearing when you hover the mouse over the icon in the system tray. The form will reappear when you click the icon in the system tray.

Adding a menu to the icon in the system tray

Your icon can very easily handle a pop-menu when the user right-clicks it. This is done using the ContextMenu property of the NotifyIcon control. Have you ever noticed that almost all Windows Forms controls have the ContextMenu property?

Figure 2: The Context menu creation.

Find the ContextMenu from the "Windows Forms" tab of your toolbox and double-click on it. An instance will be created into the designer's tray of your current form. When ContextMenu1 is selected in the designer's tray, the menu editor is available from the top of the form. Add items to the menu so it looks like the figure 2.

Once the menu is built, you need to associate this menu with your NotifyIcon control. To do this, simply add this line to the Form_Load event (somewhere into the With ... End With block):

.ContextMenu = ContextMenu1

All you have to do now is to handle the click event of your pop-menu.

For example, to handle the first item (Display Demo form), you can copy the code that is in the DoubleClick event of the NotifyIcon control.

The Quit option is as simple as this line:

Me.Close()

The other menu items, for my example, all use that Process class (sounds familiar -that was my subject in the December edition of the UTMag) to starts the Notepad, the Calculator, the Internet Explorer, and the Windows Explorer. For example, starting the Notepad is:

Dim MyProcess As New Process
MyProcess.Start("notepad.exe")

The downloadable demo implements the other events.

Conclusion

I don't know if it could be easier! The only thing I would have like is the possibility of inheriting from this control to change things like the tooltip that is a bit too basic!

I hope you appreciated the topic and see you next month.


(Print this page)