Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / VBScript
Article

Notify Icon - System tray icon explored

Rate me:
Please Sign up or sign in to vote.
1.90/5 (8 votes)
15 Feb 20062 min read 51.4K   1.3K   11   3
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
          <p>Const WM_RBUTTONDBLCLK = &H206</p>
          <p>Const WM_MBUTTONDBLCLK = &H209</p>

 

 

Occurs when the user clicks the icon in the status area.
Const WM_LBUTTONDOWN = &H201
          <p>Const WM_RBUTTONDOWN = &H204</p>
          <p>Const WM_MBUTTONDOWN = &H207</p>

 

 

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<br>
          <br>
          Const WM_RBUTTONUP = &H205
          <p>Const WM_MBUTTONUP = &H208</p>

 

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.

<font color="#000000">'
'     ###         ###          ######
'    #     #   # #               #                   #   #
'     ###   # #  ###   #####     #   #####   ####     # #
'        #   #      #            #    ##  # #   ##     #
'    ####   #    ###             #    #      ###  #   #
'</font>

Structure Used

<font color="#000000">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</font>

API Used

<font color="#000000">Private Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, _
lpData As NOTIFYICONDATA) As Long</font>

Private Properties

<font color="#000000">Private Property Let bRunningInTray(ByVal vData As Boolean)
    RunningInTray = vData
End Property


Property Get bRunningInTray() As Boolean
    bRunningInTray = RunningInTray
End Property</font>

Function details

<font color="#000000">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


</font>

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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalvbscript Pin
ronpeed27-Jun-08 17:26
ronpeed27-Jun-08 17:26 
Generalparanit email Pin
ronpeed27-Jun-08 16:31
ronpeed27-Jun-08 16:31 
Generalgr8 Pin
Kumar Sundaram24-Jan-07 22:47
Kumar Sundaram24-Jan-07 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.