Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine


Create Windows Tray Apps With Animated Icons
by Leonard Lobel

September 2003 Issue

Technology Toolbox: VB.NET
Level: Intermediate

The NotifyIcon control is one of many new cool controls that Microsoft added to the Visual Studio .NET toolbox. This control makes it easy to create applications that run in the system "tray" area, which normally appears in the lower right corner of the Windows desktop. I'll explain how to use this control, apply a context menu to it, and provide animation by displaying a series of successive "frame" icons.

ADVERTISEMENT

Create a Visual Basic Windows application named AnimateTrayIcon, and delete the Form1.vb file that VS.NET creates by default. Right-click on the project, choose Add Component..., and name the component AnimateTrayIcon.vb. A component is a hybrid between a class (which has no visible user interface) and a form (which has a design surface). Now drag three controls from the Windows Forms toolbox onto the component design surface: a NotifyIcon (name it tryIcon), a ContextMenu (name it mnuOptions), and an ImageList (name it imlIcons). Select the Images property of the ImageList control, and click on the ellipses to open the Image Collection Editor. Add the eight moon images provided by VS.NET (Moon01.ico through Moon08.ico located in the \Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Elements folder). Be sure to load the collection in proper sequence, so that the animation effect depicts a spinning globe. By preloading the ImageList in this manner, the icons get burned into the application's executable file and don't need to be deployed as eight separate external files.

Now right-click on AnimateTrayIcon.vb in the Solution Explorer and choose View Code to switch to the code editor. Add a shared Main() procedure to serve as the entry point to the application:

'Entry point
Public Shared Sub Main()
   'Only run one instance
   If Process.GetProcessesByName _
      ("AnimateTrayIcon").GetUpperBound(0) = _
      0 Then
      Dim oAnimateTrayIcon As New AnimateTrayIcon
      oAnimateTrayIcon.Run()
   End If
End Sub

The startup code first ensures that the user doesn't invoke multiple instances; this would result in multiple icons appearing in the tray. The Process.GetProcessesByName method returns an array of Process components running the specified executable on the local machine. The GetUpperBound method returns the number of elements (counting from zero) in the array. The first time the application is run, GetUpperBound returns zero and the application starts to run. If the user attempts to fire up a second instance, GetUpperBound returns one and exits immediately.

You can place the application in an ongoing run state by creating an instance of the component and invoking its Run method:

'Set flag True while running (set False to exit)
Private mbRunning As Boolean = True

Public Sub Run()

   'Build the menu
   Me.mnuOptions.MenuItems.Add _
      ("Exit", AddressOf mnuExit)

   'Start the tray icon
   With Me.tryIcon
      .Text = _
         "Tray Application (right-click for menu)"
      .ContextMenu = Me.mnuOptions
   End With

   'Primary loop
   Do While Me.mbRunning
         System.Threading.Thread.Sleep(350)
         Application.DoEvents()
      Me.AnimateIcon()
   Loop

End Sub

Back to top

Printer-Friendly Version













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home