by Hema (4 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Fri 8th August 2003
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
Create an Icon in System tray
API Declarations
'u can send ur sugestions and comments to [email protected]
'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
'The following constant is the message sent when a mouse event occurs
'within the rectangular boundaries of the icon in the taskbar status area.
Private Const WM_MOUSEMOVE = &H200
'The following constants are the flags that indicate the valid
'members of the NOTIFYICONDATA data type.
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
'the icon in the taskbar status area.
'Left-click constants.
Private Const WM_LBUTTONDBLCLK = &H203 'Double-click
Private Const WM_LBUTTONDOWN = &H201 'Button down
Private Const WM_LBUTTONUP = &H202 'Button up
'Right-click constants.
Private Const WM_RBUTTONDBLCLK = &H206 'Double-click
Private Const WM_RBUTTONDOWN = &H204 'Button down
Private Const WM_RBUTTONUP = &H205 'Button up
'Declare the API function call.
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
'Dimension a variable as the user-defined data type.
Dim nid As NOTIFYICONDATA
'if ur application as MDI write in MDI_Load else the first form_load event
Private Sub Form_Load()
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
Shell_NotifyIcon NIM_ADD, nid
MsgBox "In system tray"
End Sub"
'create a menu by using menu editor
' one main menu by Caption"Menu" name "M"
' one sub menu by name caption "Exit" name "Ex"
'make the Menu invisible by changing the "visibility" option in properties window
'write the following in MouseMove event
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
' Me.PopupMenu M 'if u want u can call the popup menu
Case WM_LBUTTONUP
Case WM_LBUTTONDBLCLK
Case WM_RBUTTONDOWN
Case WM_RBUTTONUP
Me.PopupMenu m 'call the popup menu
Case WM_RBUTTONDBLCLK
End Select
End Sub
'write the following in form_unload event
'delete the icon when the form is unloaded
Private Sub Form_Unload(Cancel As Integer)
Shell_NotifyIcon NIM_DELETE, nid
End Sub
'write the following for the sub menu
'delete the icon when exit sub menu is selected
Private Sub Ex_Click()
Shell_NotifyIcon NIM_DELETE, nid
End
End Sub