Ok, I have decided to post this in the code bank since I have been posting this in more then a few posts lately.
This code show you how to make your form show in the system tray using the Shell_NotifyIcon API.
Befor Using this code keep in mined that:
- There is systray icon menu named "TrayMenu"
- Exit button in that menu named "Ex"
- A Show button in that menu named "Show"
I have attached the example file of this exact code for better help. Check bottom of my post.
VB Code:
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean 'declaration for putting the application to system tray 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 'declare constants Private Const NIM_ADD = &H0 Private Const NIM_MODIFY = &H1 Private Const NIM_DELETE = &H2 Private Const WM_MOUSEMOVE = &H200 Private Const NIF_MESSAGE = &H1 Private Const NIF_ICON = &H2 Private Const NIF_TIP = &H4 'The following constants are used to determine the mouse input on the 'Left-click Private Const WM_LBUTTONDBLCLK = &H203 'Double click Private Const WM_LBUTTONDOWN = &H201 'down Private Const WM_LBUTTONUP = &H202 'up 'Right-click Private Const WM_RBUTTONDBLCLK = &H206 'Double click Private Const WM_RBUTTONDOWN = &H204 'down Private Const WM_RBUTTONUP = &H205 'up 'Dimension a variable as the user-defined data type. Dim nid As NOTIFYICONDATA Private Sub Ex_Click() Unload Me Shell_NotifyIcon NIM_DELETE, nid 'Delete the form's icon from the sys tray End Sub ' one main menu by Caption"Menu" name "TrayMenu" ' one sub menu by name caption "Exit" name "Ex" ' one sub menu by name caption "Show" name "Show" Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim msg As Long Dim sFilter As String msg = X / Screen.TwipsPerPixelX Select Case msg Case WM_LBUTTONDOWN MsgBox "you clicked me!" ' Me.PopupMenu TrayMenu 'if u want u can call the popup menu Case WM_LBUTTONUP Case WM_LBUTTONDBLCLK Case WM_RBUTTONDOWN Case WM_RBUTTONUP Me.PopupMenu TrayMenu 'call the popup menu Case WM_RBUTTONDBLCLK End Select End Sub Private Sub Form_Resize() If Me.WindowState = 1 Then Me.Hide nid.cbSize = Len(nid) nid.hwnd = Me.hwnd nid.uId = vbNull nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE nid.uCallBackMessage = WM_MOUSEMOVE nid.hIcon = Me.Icon 'u can give a tool tip text here nid.szTip = "My System Tray Message" & vbNullChar 'Call the Shell_NotifyIcon function to add the icon to the System Tray, defulte form's icon Shell_NotifyIcon NIM_ADD, nid Else End If End Sub Private Sub Show_Click() Me.WindowState = 0 Me.Show Shell_NotifyIcon NIM_DELETE, nid 'Delete the form's icon from the sys tray End Sub
