ARTICLE
Animating System Tray in VB.NET
Tags: . Animation in VB.NET., .NET, Animation, Context Menu, Design Time, Icon, Timer, Timer Control, VB.NET, Windows Application, Windows Forms
I am sure those who have worked with Visual C++ would have experienced the problem of displaying an icon in system tray and manipulating with context menus for that. This application gives you a picture of how to use .NET's TrayIcon class.
Download
Files:
Description.
I am sure those who have worked with Visual C++ would have experienced the problem of displaying an icon in system tray and manipulating with context menus for that. Previously I use to display System tray icons with Shell_NotifyIcon by filling a structure and manipulating with a context menu for that icon is another problem. We need to create a menu dynamically and call TrackPopMenu and call DestroyMenu().
This application gives you a picture of how to use .NET's TrayIcon class. With the introduction of this class in .NET, no need to call Shell_NotifyIcon Win32 API and TrackPopMenu() and DestroyMenu() APIs. Designing of your icon, creating context menu, adding event handlers are all done during design time itself. The complete source code is available in AnimateTray.zip file.
Source Code:
Public Sub AnimateIcon(sender As Object, e As System.EventArgs)
If Not (m_Icon1 Is Nothing) And Not (m_Icon2 Is Nothing) Then
If m_bIconFlag = True Then
m_trayIcon.Icon = m_Icon2
m_bIconFlag = False
Else
m_trayIcon.Icon = m_Icon1
m_bIconFlag = True
End If
End If
End Sub 'AnimateIcon
' Start menu handler
Protected Sub OnClickStop(sender As Object, e As System.EventArgs)m_timer.Start()
menuItem4.Enabled = True
menuItem1.Enabled = False
End Sub 'OnClickStop
' Stop menu handler
Protected Sub OnClickStop(sender As Object, e As System.EventArgs)
m_timer.Stop()
menuItem4.Enabled = False
menuItem1.Enabled = True
End Sub 'OnClickStop
Clicking the right mouse button on the system tray icon, a menu is populated. Selecting Start option from the menu will create a timer. This timer object is associated with AnimateIcon() function. m_Icon1 and m_Icon2 are which are associated with Icon1.ico and Icon2.ico files resp .The two icon files Icon1.ico and Icon2.ico should exist in the same directory.