The NotifyIcon component is invisible at runtime. A program can use the NotifyIcon to display an icon in the system tray. The system tray (also called the status area) is the little area holding small icons in the lower-left part of the taskbar. The program can use this icon to indicate the application’s state.
Figure G-14 shows program UseNotifyIcon, which uses the NotifyIcon component to display an icon in the system tray. When you select the Happy box, this program displays the happy face shown in Figure G-14. When you select the Sad box, the program displays a sad face.
Figure G-14: The NotifyIcon component displays an icon in the system tray.
In Figure G-14, the icon shown by the NotifyIcon component is in the system tray on the lower-right near the time. The form’s title bar, the system toolbar at the bottom of the screen, and the Task Manager also display a happy face. The pictures used for these come from the form’s Icon property, not from the NotifyIcon component, so you can display different images for these and the one in the system tray.
Notification icons are particularly useful for programs that have no user interface or that run in the background. For example, a program that monitors the system’s load could use its system tray icon to give the user an idea of the current load.
These sorts of programs, particularly those without normal user interfaces, often add a context menu to their tray icons so that the user can interact with them. This menu might include commands to minimize or restore the application if it has a user interface, or to make the application exit.
The NotifyIcon component only has a few interesting properties. Its Icon property determines the icon that the component displays. Its Text property sets the tooltip text that the component displays when the user hovers the mouse over the icon. The Visible property determines whether the icon is visible. Finally, the component’s ContextMenuStrip property sets the ContextMenuStrip control that displays when the user right-clicks the icon.
The following code shows how the UseNotifyIcon program works. The program includes four icon resources that are used by the program. The happy and sad icons contain happy and sad images at two sizes: 16 × 16 pixels and 32 × 32 pixels. The happy_small and sad_small icons contain only the smaller 16 × 16 pixel images.
When the user clicks the Happy button, the program sets the nicoStatus NotifyIcon component’s Icon property to the happy_small icon and it sets the form’s Icon property to the happy icon. It would be more convenient to use the same icon for both purposes. However, if you use the happy icon for both, the NotifyIcon component uses the 32 × 32 pixel image shrunk to a smaller size and the result looks bad. If you use the happy icon for both purposes, the Task Manager enlarges the 16 × 16 pixel image and that also looks bad.
When the user clicks the Sad button, the program similarly sets the nicoStatus NotifyIcon component’s Icon property to the sad_small icon and it sets the form’s Icon property to the sad icon.
At design time, a ContextMenuStrip was attached to the NotifyIcon component. When the user right-clicks the icon and then selects the menu’s Exit command, the ExitToolStripMenuItem_Click event handler closes the program’s form.
Public Class Form1 ' Display the happy status icon. Private Sub radHappy_CheckedChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles radHappy.CheckedChanged nicoStatus.Icon = My.Resources.happy_small Me.Icon = My.Resources.happy End Sub ' Display the sad status icon. Private Sub radSad_CheckedChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles radSad.CheckedChanged nicoStatus.Icon = My.Resources.sad_small Me.Icon = My.Resources.sad End Sub ' Close the application. Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click Me.Close() End Sub End Class