Programming » Visual Basic 6 » Visual Basic 6 Code » Application » ");?>
Add your own icon to the taskbar and respond to it being clicked etc.
Public Enum enmTrayIcon
    NIM_ADD = &H0
    NIM_MODIFY = &H1
    NIM_DELETE = &H2
End Enum
Private Enum enmTrayAction
    taMouseMove = 512
    taLeftMouseDown = 513
    taLeftMouseUp = 514
    taLeftDoubleClick = 515
    taRightMouseDown = 516
    taRightMouseUp = 517
    taRightDoubleClick = 518
End Enum
Private Const NIF_TIP = &H4
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const WM_MOUSEMOVE = &H200
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
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" _
        Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, _
        lpData As NOTIFYICONDATA) As Long

Public Sub TrayIcon(ByVal RequestType As enmTrayIcon, _
            ByVal IconID As LongByVal hWnd As Long, _
            ByVal hIcon As LongByVal ToolTip As String)
    Dim nid As NOTIFYICONDATA
    Dim X As Long
    nid.cbSize = LenB(nid)
    nid.hWnd = hWnd
    nid.uID = IconID
    nid.uFlags = NIF_TIP Or NIF_MESSAGE Or NIF_ICON
    nid.uCallbackMessage = WM_MOUSEMOVE
    nid.hIcon = hIcon
    nid.szTip = ToolTip & vbNullChar
    X = Shell_NotifyIcon(RequestType, nid)
End Sub

' To use the code, start a new project and add two command buttons to the form.
' Name the first 'cmdIcon" and make the Visible property False. Name the
' second "cmdChangeTip" and leave it visible. Add the following code and run
' the application:

Private Sub Form_Load()
    TrayIcon NIM_ADD, 1, cmdIcon.hWnd, Me.Icon, "Created"
End Sub
Private Sub cmdChangeTip_Click()
    TrayIcon NIM_MODIFY, 1, cmdIcon.hWnd, Me.Icon, Format$(Now, "hh:mm:ss")
End Sub
Private Sub Form_Unload(Cancel As Integer)
    TrayIcon NIM_DELETE, 1, cmdIcon.hWnd, Me.Icon, ""
End Sub
Private Sub cmdIcon_MouseMove(Button As Integer, Shift As Integer, X As _
            Single, Y As Single)
    Dim lngAction As Long
    lngAction = X / Screen.TwipsPerPixelX
    Select Case lngAction
        Case taMouseMove:    ' take any desired action for each case...
        Case taLeftMouseDown:
        Case taLeftMouseUp:
        Case taLeftDoubleClick:
        Case taRightMouseDown:
        Case taRightMouseUp:
        Case taRightDoubleClick:
        Case Else:    ' unknown action
    End Select
End Sub

' Note 1: I have heard that using this code in conjunction with a modal Form
' containing databound controls causes erratic results.
' Note 2: If you need multiple icons in the tray then they must all have
' unique Icon IDs and you should use a different hWnd as the message target
' for each. The MouseMove parameters do not include information on what Icon
' generated the message.