Accessing System Tray from Application – 2
This post extends my previous post Accessing System Tray from Application adding context sensitive menu to the System Tray icon as described in Working with menus in Visual Basic
Open the System tray icon project. Add the code below to the MouseMove event of the form, thus calling the PopMenu method of form when Right-clicked on the icon. The menu item mnuSysTray would be shown with menu item mnuDefault marked as Default making it Bold.
Now open the Menu Editor and add a top level menu named mnuSysTray. Uncheck the visible option, which will ensure that the menu and it's submenus are not visible on the main form. Add four menu items as submenu to mnuSysTray named mnuDefault captioned "open", mnuMinimized captioned "Minimize to System Tray", mnuSep captioned "-" and mnuClose captioned "Close". Check the "Checked" property of mnuMinimized.

Now add the code for Click events of the three menu items, Fourth mnuSep is just a seperator.
Private Sub mnuDefault_Click()
Form1.WindowState = 0
Form1.Show
Shell_NotifyIcon NIM_DELETE, nid
End Sub
Private Sub mnuMinimize_Click()
chkSysTray = False
Form1.Show
Shell_NotifyIcon NIM_DELETE, nid
End Sub
Private Sub mnuClose_Click()
Shell_NotifyIcon NIM_DELETE, nid
End
End Sub
Clicking on mnuDefault opens the Form as it did when double-clicked. Clicking on mnuMinimize shows up the form in minimized state, i.e. the form is seen on the task bar and the checkbox is unchecked. Clicking mnuClose ends the execution of the program. All the three events remove the icon from the system tray.
With this the system tray icon is placed with all possible functionalities.
The complete code adding to the preious post is as under :
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
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 Const WINDOW_NORMAL = 0
Private Const WINDOW_MINIMIZED = 1
Private Const WINDOW_MAXIMIZED = 2
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203 'Double-click
Private Const WM_LBUTTONDOWN = &H201 'Button down
Private Const WM_LBUTTONUP = &H202 'Button up
Private Const WM_RBUTTONDBLCLK = &H206 'Double-click
Private Const WM_RBUTTONDOWN = &H204 'Button down
Private Const WM_RBUTTONUP = &H205 'Button up
Dim nid As NOTIFYICONDATA
Private Sub Form_Resize()
If Form1.WindowState = WINDOW_MINIMIZED Then
If chkSysTray Then
Form1.Hide
Call ShowAppInSysTray
End If
End If
End Sub
Private Sub ShowAppInSysTray()
nid.cbSize = Len(nid)
nid.hWnd = Form1.hWnd
nid.uId = vbNull
nid.uFlags = NIF_MESSAGE Or NIF_TIP Or NIF_ICON
nid.uCallBackMessage = WM_MOUSEMOVE
nid.hIcon = Form1.Icon
nid.szTip = "Double-Click to Open " & Form1.Caption & vbNullChar
Shell_NotifyIcon NIM_ADD, nid
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
Dim msg As Long
msg = X / Screen.TwipsPerPixelX
If msg = WM_LBUTTONDBLCLK Then
Form1.WindowState = 0
Form1.Show
Shell_NotifyIcon NIM_DELETE, nid
End If
If msg = WM_RBUTTONUP Then
Me.PopupMenu Me.mnuSysTray, , , , mnuDefault
End If
End Sub
Private Sub mnuDefault_Click()
Form1.WindowState = 0
Form1.Show
Shell_NotifyIcon NIM_DELETE, nid
End Sub
Private Sub mnuMinimize_Click()
chkSysTray = False
Form1.Show
Shell_NotifyIcon NIM_DELETE, nid
End Sub
Private Sub mnuClose_Click()
Shell_NotifyIcon NIM_DELETE, nid
End
End Sub


