Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Notify Icon - System tray icon explored

0.00/5 (No votes)
15 Feb 2006 2  
A simple class that creates an icon in the status area.

Sample Image - maximum width is 600 pixels

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

Const WM_RBUTTONDBLCLK = &H206

Const WM_MBUTTONDBLCLK = &H209

 

 

Occurs when the user clicks the icon in the status area.
Const WM_LBUTTONDOWN = &H201

Const WM_RBUTTONDOWN = &H204

Const WM_MBUTTONDOWN = &H207

 

 

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

Const WM_RBUTTONUP = &H205

Const WM_MBUTTONUP = &H208

 

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here