Recipe 14.11. Creating an Icon in the System Tray


Problem

You wish to use a System Tray icon to regularly notify the user of the status of your application.

Solution

Sample code folder: Chapter 14\SystemTrayIcon

Add a NotifyIcon control to your application's form. It includes properties that simplify displaying a System Tray icon and its related notification "balloon.". Once you've added the control to your form, assign an icon (.ico) file or image to its Icon property, and ensure that its Visible property is set to true. That's it. If you want to enable a tooltip for the icon, set the Text property as needed.

Discussion

The NotifyIcon control also includes support for simple notification balloons. Use the BalloonTipIcon, BalloonTipText, and BalloonTipTitle properties to set the icon, main text, and title of the balloon, respectively.

Create a new Windows Forms application. Add a Button control named Button1 to the form, and set its Text property to Show Warning. Then add a NotifyIcon control named NotifyIcon1 to the form. Set the following properties on that control:

  • Set BalloonTipIcon to Warning.

  • Set BalloonTipText to Your system is in need of repair.

  • Set BalloonTipTitle to Repair Warning.

  • Set the Icon property to any valid .ico icon file. (See below for a source for icon files.)

Now add the following source code to Form1's class template:

 Private Sub Button1_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles Button1.Click    ' ----- Show the balloon for 3 seconds by default.    NotifyIcon1.ShowBalloonTip(3000) End Sub 

Run the program, and click on the Show Warning button to view the notice bubble, as shown in Figure 14-8.

Figure 14-8. A notification icon with a warning balloon


The NotifyIcon control includes many events that can detect various types of clicks or double-clicks on the icon or its balloon.

If you need a notification icon for your application, you can try one of the many icons included with Visual Studio. Depending on how you installed the product, you may find a compressed folder named VS2005ImageLibrary.zip in the Common7\ VS2005ImageLibrary folder of the main product install folder (usually at c:\Program Files\Microsoft Visual Studio 8). This archive includes an icons folder with many professionally designed icons in it. You can include them freely in applications for your personal use, but be sure to read the Visual Studio license agreement if you plan to use these icons in your commercial applications.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net