-
Notifications
You must be signed in to change notification settings - Fork 12
/
SystemTrayManager.cls
464 lines (284 loc) · 11.8 KB
/
SystemTrayManager.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "SystemTrayManager"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'--------------------------------------------------------------------------------
' Component : SystemTrayManager
' Project : ViDock
'
' Description: (Not related to the OS System Tray) manages all functionality
' to do with the dock manager
'
'--------------------------------------------------------------------------------
Option Explicit
Private m_isExplorer64bit As Boolean
Private m_trayhWnd As Long
Private m_traypID As Long
Private m_buttons As Collection
Private m_bitmap As GDIPBitmap
Private m_graphics As GDIPGraphics
Private m_popup As DockPopup
Private m_dimensions As gdiplus.RECTL
Private m_lastSelectedButton As TrayButtonInfo
Private m_columnLimit As Long
Private m_finalRowcount As Long
Private m_lastIconCount As Long
Public HostForm As Form
Public Event onChange()
Public Function IsPopupShowing() As Boolean
On Error GoTo Handler:
IsPopupShowing = m_popup.Visible
Exit Function
Handler:
End Function
Public Property Let ColumnLimit(newLimit As Long)
m_columnLimit = newLimit
UpdateCalculations
UpdateImage
End Property
Private Function UpdateCalculations()
If m_columnLimit = 0 Then Exit Function
m_lastIconCount = CountIcons
m_finalRowcount = MathHelper.Ceiling(m_lastIconCount / m_columnLimit)
End Function
Public Property Get Dimensions() As gdiplus.RECTL
Dimensions.Top = m_dimensions.Top
Dimensions.Left = m_dimensions.Left
Dimensions.Width = m_bitmap.Image.Width
Dimensions.Height = m_bitmap.Image.Height
End Property
Public Property Let Dimensions(newDimensions As gdiplus.RECTL)
m_dimensions.Left = newDimensions.Left
m_dimensions.Top = newDimensions.Top
End Property
Public Property Get Image() As GDIPImage
Set Image = m_bitmap.Image
End Property
Public Property Get Buttons() As Collection
Set Buttons = m_buttons
End Property
Private Sub Class_Initialize()
Dim hProcess As Long
Dim ret As Long
Set m_bitmap = New GDIPBitmap
m_bitmap.CreateFromSizeFormat 1, ICON_SIZE, GDIPlusWrapper.Format32bppArgb
Set m_graphics = New GDIPGraphics
m_graphics.FromImage m_bitmap.Image
m_trayhWnd = SystemTrayHelper.FindTrayToolbarWindowHiddenIcons
If m_trayhWnd = -1 Then Exit Sub
ret = GetWindowThreadProcessId(m_trayhWnd, m_traypID)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, m_traypID)
If hProcess = 0 Then Exit Sub
m_isExplorer64bit = IsProcess64bit(hProcess)
CloseHandle hProcess
Set m_buttons = New Collection
Update
End Sub
Public Property Let Popup(newPopup As DockPopup)
Set m_popup = newPopup
End Property
Public Function Update()
Dim trayInfo As TrayButtonInfo
Dim iconIndex As Long
Dim changed As Boolean
Dim buttonsThisUpdate As Collection
Set buttonsThisUpdate = New Collection
For iconIndex = 0 To SysTrayIconCount(m_trayhWnd)
Set trayInfo = GetButtonInfo(iconIndex)
If trayInfo.Visible Then
If Not ExistInCol(buttonsThisUpdate, trayInfo.App_Identifier) Then buttonsThisUpdate.Add trayInfo, trayInfo.App_Identifier
End If
Next
For Each trayInfo In m_buttons
If Not ExistInCol(buttonsThisUpdate, trayInfo.App_Identifier) Then
changed = True
Exit For
End If
Next
If Not changed Then
For Each trayInfo In buttonsThisUpdate
If Not ExistInCol(m_buttons, trayInfo.App_Identifier) Then
changed = True
Exit For
End If
Next
End If
If Not changed Then
For Each trayInfo In buttonsThisUpdate
If trayInfo.hIcon <> m_buttons(trayInfo.App_Identifier).hIcon Then
changed = True
Exit For
End If
Next
End If
If changed Then
Set m_buttons = buttonsThisUpdate
UpdateCalculations
UpdateImage
Debug.Print "onChanged!"
RaiseEvent onChange
End If
End Function
Private Function UpdateImage()
Dim trayInfo As TrayButtonInfo
Dim buttonIndex As Long
Dim thisAlphaIcon As AlphaIcon
Dim rowIndex As Long
Dim columnIndex As Long
Set thisAlphaIcon = New AlphaIcon
If m_bitmap.Image.Width <> m_columnLimit * (ICON_SIZE + MARGIN) Or m_bitmap.Image.Height <> m_finalRowcount * (ICON_SIZE + MARGIN) Then
Set m_bitmap = New GDIPBitmap
'm_bitmap.CreateFromSize m_buttons.count * (SystemTrayHelper.ICON_SIZE + SystemTrayHelper.MARGIN), SystemTrayHelper.ICON_SIZE
m_bitmap.CreateFromSizeFormat m_columnLimit * (ICON_SIZE + MARGIN), m_finalRowcount * (ICON_SIZE + MARGIN), GDIPlusWrapper.Format32bppArgb
m_graphics.FromImage m_bitmap.Image
End If
m_graphics.Clear
For Each trayInfo In m_buttons
If trayInfo.Visible Then
thisAlphaIcon.CreateFromHICON trayInfo.hIcon
If columnIndex = m_columnLimit Then
rowIndex = rowIndex + 1
columnIndex = 0
End If
'm_graphics.DrawImage thisAlphaIcon.Image, (buttonIndex * (SystemTrayHelper.ICON_SIZE + SystemTrayHelper.MARGIN)), 0, SystemTrayHelper.ICON_SIZE, SystemTrayHelper.ICON_SIZE
m_graphics.DrawImage thisAlphaIcon.Image, columnIndex * (ICON_SIZE + MARGIN), rowIndex * (ICON_SIZE + MARGIN), ICON_SIZE, ICON_SIZE
columnIndex = columnIndex + 1
buttonIndex = buttonIndex + 1
End If
Next
End Function
Public Function CountIcons() As Long
CountIcons = SysTrayIconCount(m_trayhWnd)
End Function
Private Function GetButtons() As Collection
Dim trayInfo As TrayButtonInfo
Dim iconIndex As Long
Dim Buttons As Collection
Set Buttons = New Collection
Set GetButtons = Buttons
Dim sysTrayIconCountSnapshot As Long
sysTrayIconCountSnapshot = SysTrayIconCount(m_trayhWnd)
For iconIndex = 0 To sysTrayIconCountSnapshot
Set trayInfo = GetButtonInfo(iconIndex)
If Not ExistInCol(Buttons, trayInfo.App_Identifier) Then Buttons.Add trayInfo, trayInfo.App_Identifier
Next
End Function
Private Function GetButtonInfo(iIndex As Long) As TrayButtonInfo
If m_isExplorer64bit Then
Set GetButtonInfo = SysTrayGetButtonInfo_64(m_trayhWnd, m_traypID, iIndex)
Else
Set GetButtonInfo = SysTrayGetButtonInfo_86(m_trayhWnd, m_traypID, iIndex)
End If
End Function
Public Function MouseDblClick()
If m_lastSelectedButton Is Nothing Then
Exit Function
End If
Dim cursorPos As POINTL
GetCursorPos cursorPos
PostMessage ByVal m_lastSelectedButton.hWnd, ByVal m_lastSelectedButton.uCallbackMessage, ByVal MAKEWPARAM(cursorPos.X, cursorPos.Y), ByVal WM_LBUTTONDBLCLK
End Function
Public Function MouseUp(Button As Integer, X As Single, Y As Single)
Dim cursorPos As POINTL
GetCursorPos cursorPos
If m_lastSelectedButton Is Nothing Then
Exit Function
End If
If Button = vbRightButton Then
'WM_NCRBUTTONDOWN
'PostMessage ByVal m_lastSelectedButton.hWnd, ByVal m_lastSelectedButton.uCallbackMessage, ByVal 0, ByVal WM_NCRBUTTONUP
'PostMessage ByVal m_lastSelectedButton.hWnd, ByVal m_lastSelectedButton.uCallbackMessage, ByVal m_lastSelectedButton.uCallbackMessage, ByVal WM_RBUTTONUP
PostMessage ByVal m_lastSelectedButton.hWnd, ByVal m_lastSelectedButton.uCallbackMessage, ByVal MAKEWPARAM(cursorPos.X, cursorPos.Y), ByVal WM_CONTEXTMENU
ElseIf Button = vbLeftButton Then
PostMessage ByVal m_lastSelectedButton.hWnd, ByVal m_lastSelectedButton.uCallbackMessage, ByVal MAKEWPARAM(cursorPos.X, cursorPos.Y), ByVal WM_LBUTTONUP
End If
End Function
Public Function MouseDown(Button As Integer, X As Single, Y As Single)
On Error GoTo Handler
If m_lastSelectedButton Is Nothing Then
Exit Function
End If
Dim mainhWnd As Long
Dim winStyle As Long
Dim cursorPos As POINTL
GetCursorPos cursorPos
'mainhWnd = GetMainWindowhWnd_Method3(m_lastSelectedButton.hWnd)
'SetWindowPos m_lastSelectedButton.hWnd, 0, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
If mainhWnd > 0 Then
winStyle = GetWindowLong(mainhWnd, GWL_STYLE)
If (winStyle And WS_MINIMIZE) = WS_MINIMIZE Then
'ShowWindow mainhWnd, SW_RESTORE
End If
If Not (winStyle And WS_VISIBLE) = WS_VISIBLE Then
'ShowWindow mainhWnd, SW_SHOW
End If
'SetForegroundWindow mainhWnd
Else
'mainhWnd = Me.HostForm.hWnd
'SetForegroundWindow mainhWnd
'SetActiveWindow mainhWnd
'LogError 0, "MouseDown", "SystemTrayManager", "Couldn't find a master window"
End If
If Button = vbRightButton Then
'PostMessage ByVal m_lastSelectedButton.hWnd, ByVal m_lastSelectedButton.uCallbackMessage, ByVal 0, ByVal WM_NCRBUTTONDOWN
PostMessage ByVal m_lastSelectedButton.hWnd, ByVal m_lastSelectedButton.uCallbackMessage, ByVal MAKEWPARAM(cursorPos.X, cursorPos.Y), ByVal WM_RBUTTONDOWN
ElseIf Button = vbLeftButton Then
PostMessage ByVal m_lastSelectedButton.hWnd, ByVal m_lastSelectedButton.uCallbackMessage, ByVal MAKEWPARAM(cursorPos.X, cursorPos.Y), ByVal WM_LBUTTONDOWN
'ActivateAllPopupWindows m_lastSelectedButton.hWnd
End If
Exit Function
Handler:
'MsgBox Err.Description
LogError 0, "MouseDown", "SystemTrayManager", Err.Description
End Function
Private Function GetSelectedItemIndex(ByVal X As Single, ByVal Y As Single) As Long
Dim yIndex As Long
Dim xIndex As Long
Dim theItemIndex As Long
GetSelectedItemIndex = -1
yIndex = Floor(Y / (MARGIN + ICON_SIZE))
xIndex = Floor(X / (MARGIN + ICON_SIZE)) + 1
theItemIndex = xIndex + (yIndex * m_columnLimit)
If theItemIndex > 0 And theItemIndex <= m_lastIconCount Then
GetSelectedItemIndex = theItemIndex
End If
End Function
Public Function MouseMove(Position As POINTS)
Dim selectedButton As TrayButtonInfo
Dim selectedButtonIndex As Long
Dim cursorPos As POINTL
selectedButtonIndex = GetSelectedItemIndex(Position.X, Position.Y)
GetCursorPos cursorPos
If selectedButtonIndex <= 0 Or selectedButtonIndex > m_buttons.Count Then
Exit Function
End If
Set selectedButton = m_buttons(selectedButtonIndex)
If selectedButton Is Nothing Then
Exit Function
End If
PostMessage ByVal selectedButton.hWnd, ByVal selectedButton.uCallbackMessage, ByVal MAKEWPARAM(cursorPos.X, cursorPos.Y), ByVal WM_MOUSEMOVE
If m_lastSelectedButton Is selectedButton Then
Exit Function
End If
Set m_lastSelectedButton = selectedButton
If selectedButton Is Nothing Then
Exit Function
End If
If selectedButton.Tooltip = vbNullString Then
'm_popup.Hide
Else
Debug.Print "selectedButton.Tooltip:: '" & selectedButton.Tooltip & "'"
'm_popup.Top = HostForm.Top + HostForm.Height
'm_popup.ShowTextPopup selectedButton.Tooltip, Dimensions.Left + (HostForm.Left / Screen.TwipsPerPixelX) + _
(selectedButtonX) + (SystemTrayHelper.ICON_SIZE / 2)
End If
End Function