-
Notifications
You must be signed in to change notification settings - Fork 3
/
CSystrayIcon.cls
147 lines (133 loc) · 3.91 KB
/
CSystrayIcon.cls
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CSystrayIcon"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
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 MAX_TIP_LENGTH As Long = 64
Private Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uId As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * MAX_TIP_LENGTH
End Type
Private nidTrayIcon As NOTIFYICONDATA
Private bIconDisplayed As Boolean
Private bUpdateOnChange As Boolean
Public Event NIError(ByVal ErrorNumber As Long)
Public PopUpMessage As String
Public Function Initialize(ByVal hWnd As Long, ByVal hIcon As Long, ByVal sTip As String, Optional ByVal uCallbackMessage As Long = WM_MOUSEMOVE) As Long
With nidTrayIcon
.cbSize = Len(nidTrayIcon)
.hIcon = hIcon
.hWnd = hWnd
.szTip = Left(sTip, MAX_TIP_LENGTH - 1) & vbNullChar
.uCallbackMessage = uCallbackMessage
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uId = vbNull
End With
bIconDisplayed = False
bUpdateOnChange = True
End Function
Public Function ShowIcon() As Boolean
If Not bIconDisplayed Then
ShowIcon = Shell_NotifyIcon(NIM_ADD, nidTrayIcon)
If ShowIcon = False Then
RaiseEvent NIError(GetLastError)
Else
bIconDisplayed = True
End If
End If
End Function
Public Function HideIcon() As Boolean
If bIconDisplayed Then
HideIcon = Shell_NotifyIcon(NIM_DELETE, nidTrayIcon)
If HideIcon = False Then
RaiseEvent NIError(GetLastError)
Else
bIconDisplayed = False
End If
End If
End Function
Public Property Let IconHandle(ByVal hIcon As Long)
nidTrayIcon.hIcon = hIcon
If bUpdateOnChange Then
nidTrayIcon.uFlags = NIF_ICON
Update
nidTrayIcon.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
End If
End Property
Public Property Let TipText(ByVal sTip As String)
nidTrayIcon.szTip = Left(sTip, MAX_TIP_LENGTH - 1) & vbNullChar
If bUpdateOnChange Then
nidTrayIcon.uFlags = NIF_TIP
Update
nidTrayIcon.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
End If
End Property
Public Property Let CallbackMessage(ByVal uCallbackMessage As Long)
nidTrayIcon.uCallbackMessage = uCallbackMessage
If bUpdateOnChange Then
nidTrayIcon.uFlags = NIF_MESSAGE
Update
nidTrayIcon.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
End If
End Property
Public Function Update() As Boolean
If bIconDisplayed Then
Update = Shell_NotifyIcon(NIM_MODIFY, nidTrayIcon)
If Update = False Then
RaiseEvent NIError(GetLastError)
End If
End If
End Function
Public Property Get IconHandle() As Long
IconHandle = nidTrayIcon.hIcon
End Property
Public Property Get TipText() As String
TipText = Left(nidTrayIcon.szTip, Len(nidTrayIcon.szTip) - 1)
End Property
Public Property Get CallbackMessage() As Long
CallbackMessage = nidTrayIcon.uCallbackMessage
End Property
Public Property Let UpdateOnChange(bUpdate As Boolean)
bUpdateOnChange = bUpdate
End Property
Private Property Get UpdateOnChange() As Boolean
UpdateOnChange = bUpdateOnChange
End Property
Private Sub Class_Terminate()
HideIcon
End Sub
Public Property Get Visible() As Boolean
If bIconDisplayed Then
Visible = True
End If
End Property
Public Property Let Visible(ByVal bVisible As Boolean)
If bVisible Then
ShowIcon
Else
HideIcon
End If
End Property