You are on page 1of 2

'Add the following line to the top of your main form...

Public MyTrayIcon As New <NAME OF ADDED CLASS MODULE (see below)>


'"MyTrayIcon" is the name of the actual trayicon, this icon would
'be classed as an object. The following functions are the events
'of this object.
'To use the tray icon you must add a "Class Module"
'to your project and place the following code into it

Option Explicit
Private Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uId As Long
uFlags As Long
ucallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type
Private FormHandle As Long
Private mvarbRunningInTray As Boolean
Private SysIcon As NOTIFYICONDATA
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA
" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Property Let bRunningInTray(ByVal vData As Boolean)
mvarbRunningInTray = vData
End Property
Property Get bRunningInTray() As Boolean
bRunningInTray = mvarbRunningInTray
End Property
Public Sub ShowIcon(ByRef sysTrayForm As Form)
SysIcon.cbSize = Len(SysIcon)
SysIcon.hwnd = sysTrayForm.hwnd
SysIcon.uId = vbNull
SysIcon.uFlags = 7
SysIcon.ucallbackMessage = 512
SysIcon.hIcon = sysTrayForm.Icon
SysIcon.szTip = sysTrayForm.Caption + Chr(0)
Shell_NotifyIcon 0, SysIcon
mvarbRunningInTray = True
End Sub
Public Sub RemoveIcon(sysTrayForm As Form)
SysIcon.cbSize = Len(SysIcon)
SysIcon.hwnd = sysTrayForm.hwnd
SysIcon.uId = vbNull
SysIcon.uFlags = 7
SysIcon.ucallbackMessage = vbNull
SysIcon.hIcon = sysTrayForm.Icon
SysIcon.szTip = Chr(0)

Shell_NotifyIcon 2, SysIcon
If sysTrayForm.Visible = False Then sysTrayForm.Show
ee form
mvarbRunningInTray = False
End Sub

'Incase user can't s

Public Sub ChangeIcon(sysTrayForm As Form, picNewIcon As PictureBox)


If mvarbRunningInTray = True Then 'If running in the tray
SysIcon.cbSize = Len(SysIcon)
SysIcon.hwnd = sysTrayForm.hwnd
'SysIcon.uId = vbNull
'SysIcon.uFlags = 7
'SysIcon.ucallbackMessage = 512
SysIcon.hIcon = picNewIcon.Picture
'SysIcon.szTip = sysTrayForm.Caption + Chr(0)
Shell_NotifyIcon 1, SysIcon
End If
End Sub
Public Sub ChangeToolTip(sysTrayForm As Form, strNewTip As String)
If mvarbRunningInTray = True Then 'If running in the tray
SysIcon.cbSize = Len(SysIcon)
SysIcon.hwnd = sysTrayForm.hwnd
SysIcon.szTip = strNewTip & Chr(0)
Shell_NotifyIcon 1, SysIcon
End If
End Sub

You might also like