Even More Windows Secrets for Visual Basic .NET, Page 4
Secrets of Working with the System Tray
Working with the Windows system tray was never the easiest of tasks. Officially called the "status notification area," it always involved a bundle of API calls and a little too much effort than it actually deserved. In .NET, however, it's all about knowing which controls to use.
The heart of the whole process is the NotifyIcon component. Found in the toolbox, you'll need to drag-and-drop this little beast straight onto your form or component. Then you need to get editing those properties: Change the Icon property to the icon you wish to use and Text to the name you wish to appear as a tool tip.
Try running your form or component as it stands so far: exactly zero lines of code later and your application can already display an icon in the system tray. But I'm guessing you want to do just a little more than that.
Most applications display a menu when the user selects the icon. For this, you need to add another toolbox component: the ContextMenu. If you've dropped this straight onto a form, you'll be able to edit it just like a regular menu: add separators, write code to respond to the Click events of the individual menu items, the works. Then, change the ContextMenu property of the NotifyIcon component to point to your new menu. Next, run your application and click your icon in the system tray—result achieved!
If, on the other hand, you simply want to run a little code or display a form when your icon is clicked, check out the useful events supplied by the NotifyIcon property. You have Click, DoubleClick, MouseDown, MouseMove, and MouseUp. Simply use the code window to select one of these, then start writing your code.
And that's it. Two controls, a couple of properties, and a handful of events are all you need to know to master the system tray.
Caption: Is that a banana in my system tray?