Notify Icon - System tray icon explored






1.90/5 (8 votes)
Feb 15, 2006
2 min read

52211

1341
A simple class that creates an icon in the status area.
Introduction
In my article System Tray Icon, I introduced a simple Class that creates an icon in the status area. Icons in the status area are short cuts to processes that are running in the background of a computer, such as a virus protection program or a volume control. These processes do not come with their own user interfaces. Notify Icon provides a way to program in this functionality.
Class Details
Public Methods
ShowIcon (ByRef SysTrayForm As Form) |
Sets the current icon.
|
RemoveIcon (SysTrayForm As Form) |
Remove the current icon.
|
ChangeIcon (SysTrayForm As Form, picNewIcon As
PictureBox)
|
Sets the ToolTip text displayed when the mouse hovers over a status area icon. |
(RestoreIcon (SysTrayForm As Form) |
Restore default icon.
|
ChangeToolTip (SysTrayForm As Form, strNewTip As String) |
Sets the ToolTip text displayed when the mouse
hovers over a status area icon.
|
Public Events
Const WM_LBUTTONDBLCLK = &H203
|
Occurs when the user clicks the icon in the status area. |
Const WM_LBUTTONDOWN = &H201
|
Occurs when the user presses the mouse button while the pointer is over the icon in the status notification area of the taskbar. |
Const WM_MOUSEMOVE = &H200
|
Occurs when the user moves the mouse while the pointer
is over the icon in the status notification area of the taskbar.
|
Const WM_LBUTTONUP = &H202
|
Occurs when the user releases the mouse button while the pointer is over the icon in the status notification area of the taskbar. |
Using the code
A brief description of how to use the article or code.
' ' ### ### ###### ' # # # # # # # ' ### # # ### ##### # ##### #### # # ' # # # # ## # # ## # ' #### # ### # # ### # # '
Structure Used
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
API Used
Private Declare Function Shell_NotifyIcon Lib "shell32" _ Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, _ lpData As NOTIFYICONDATA) As Long
Private Properties
Private Property Let bRunningInTray(ByVal vData As Boolean) RunningInTray = vData End Property Property Get bRunningInTray() As Boolean bRunningInTray = RunningInTray End Property
Function details
Public Sub ShowIcon(ByRef SysTrayForm As Form) With nic .cbSize = Len(nic) .Hwnd = SysTrayForm.Hwnd .uId = vbNull .uFlags = 7 .ucallbackMessage = 512 'On Mouse Move .hIcon = SysTrayForm.Icon .szTip = SysTrayForm.Caption + Chr(0) End With Shell_NotifyIcon 0, nic RunningInTray = True End Sub Public Sub RemoveIcon(SysTrayForm As Form) With nic .cbSize = Len(nic) .Hwnd = SysTrayForm.Hwnd .uId = vbNull .uFlags = 7 .ucallbackMessage = vbNull .hIcon = SysTrayForm.Icon .szTip = Chr(0) End With Shell_NotifyIcon 2, nic If SysTrayForm.Visible = False Then SysTrayForm.Show 'Incase user can't see form RunningInTray = False End Sub Public Sub ChangeIcon(SysTrayForm As Form, picNewIcon As PictureBox) If RunningInTray = True Then 'If running in the tray With nic .cbSize = Len(nic) .Hwnd = SysTrayForm.Hwnd .hIcon = picNewIcon.Picture End With Shell_NotifyIcon 1, nic End If End Sub Public Sub RestoreIcon(SysTrayForm As Form) If RunningInTray = True Then 'If running in the tray With nic .cbSize = Len(nic) .Hwnd = SysTrayForm.Hwnd .hIcon = SysTrayForm.Icon End With Shell_NotifyIcon 1, nic End If End Sub Public Sub ChangeToolTip(SysTrayForm As Form, strNewTip As String) If RunningInTray = True Then 'If running in the tray With nic .cbSize = Len(nic) .Hwnd = SysTrayForm.Hwnd .szTip = strNewTip & Chr(0) End With Shell_NotifyIcon 1, nic End If End Sub
History
- v1.1 (16/February/2006)
First release