CodeItBetter Programming Another VB Programming Blog

How to add/remove systray icon, show notification tip, show systray menu etc.

Posted on July 15, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'System & API - How to add/remove systray icon, show notification tip, show systray menu etc.
'Declarations for Systray Functions, icon and tooltip notification
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, _
    lpData As NOTIFYICONDATA) As Long
 
Private Const APP_SYSTRAY_ID = 999
Private Const NOTIFYICON_VERSION = &H3
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
Private Const NIF_INFO = &H10
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIM_SETVERSION = &H4
Private Const NIS_SHAREDICON = &H2
Private NOTIFYICONDATA_SIZE As Long
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_RBUTTONUP = &H205
 
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type
 
Private Type NOTIFYICONDATA
    cbSize As Long
    hwnd As Long
    uID As Long
    uFlags As Long
    uCallbackMessage As Long
    hIcon As Long
    szTip As String * 128
    dwState As Long
    dwStateMask As Long
    szInfo As String * 256
    uTimeoutAndVersion As Long
    szInfoTitle As String * 64
    dwInfoFlags As Long
    guidItem As GUID
End Type
 
Public Sub ShellTrayAdd(ByVal hWindow As Long, ByVal hIcon As Long)
    On Error Resume Next
    'This function is used to add icon in systray
    Dim nid As NOTIFYICONDATA
    If NOTIFYICONDATA_SIZE = 0 Then NOTIFYICONDATA_SIZE = 504
    With nid
        .cbSize = NOTIFYICONDATA_SIZE
        .hwnd = hWindow
        .uID = APP_SYSTRAY_ID
        .uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
        .dwState = NIS_SHAREDICON
        .uCallbackMessage = WM_MOUSEMOVE
        .hIcon = hIcon
        .szTip = "Test Systray" & vbNullChar
        .uTimeoutAndVersion = NOTIFYICON_VERSION
    End With
    Call Shell_NotifyIcon(NIM_ADD, nid)
    Call Shell_NotifyIcon(NIM_SETVERSION, nid)
End Sub
 
Public Sub ShellTrayRemove(ByVal hWindow As Long)
    'This function is used to remove icon from systray.
    On Error Resume Next
    Dim nid As NOTIFYICONDATA
    If NOTIFYICONDATA_SIZE = 0 Then NOTIFYICONDATA_SIZE = 504
    With nid
        .cbSize = NOTIFYICONDATA_SIZE
        .hwnd = hWindow
        .uID = APP_SYSTRAY_ID
    End With
    Call Shell_NotifyIcon(NIM_DELETE, nid)
End Sub
 
Public Sub ShellTrayModifyTip(ByVal hWindow As Long, ByVal nIconIndex As Long, _
    Optional ByVal strText As String = "Test Systray")
    On Error Resume Next
    'This function is used to modify the notification tip in systray icon
    Dim nid As NOTIFYICONDATA
    If NOTIFYICONDATA_SIZE = 0 Then NOTIFYICONDATA_SIZE = 504
    With nid
        .cbSize = NOTIFYICONDATA_SIZE
        .hwnd = hWindow
        .uID = APP_SYSTRAY_ID
        .uFlags = NIF_INFO
        .dwInfoFlags = nIconIndex
        .szInfoTitle = "Test Systray" & vbNullChar
        .szInfo = strText & vbNullChar
    End With
    Call Shell_NotifyIcon(NIM_MODIFY, nid)
End Sub
 
Public Function GenerateSysMenu(ByVal X As Single, mnuAbout As Boolean, mnuExit As Boolean)
    On Error Resume Next
    'This function is used to add menus in systray icon
    Static Popped As Boolean
    Static Msg As Long
 
    Msg = X / Screen.TwipsPerPixelX
    If Popped = False Then
        Popped = True
        Select Case Msg
        Case WM_LBUTTONDBLCLK
            'mnuPopupAbout_Click
            mnuAbout = True
        Case WM_RBUTTONUP
            'PopupMenu mnuPopup, 2, , , mnuPopupAbout
            mnuExit = True
        End Select
        Popped = False
    End If
End Function
Comments (1) Trackbacks (0)
  1. Please send me the code to add an icon in system tray. Now it is not being added.


Leave a comment


 

No trackbacks yet.