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"
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:
Responding to events
This control also gives you a very limited number of events.
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
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
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
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.