Attribute VB_Name = "mdlSysTray"
Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Const GW_HWNDPREV = 3
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

Public Const SC_MOVE = &HF010&
Public Const SC_RESTORE = &HF120&
Public Const SC_SIZE = &HF000&
Public Const WM_SYSCOMMAND = &H112
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201     'Button down
Public Const WM_LBUTTONUP = &H202       'Button up
Public Const WM_LBUTTONDBLCLK = &H203   'Double-click
Public Const WM_RBUTTONDOWN = &H204     'Button down
Public Const WM_RBUTTONUP = &H205       'Button up
Public Const WM_RBUTTONDBLCLK = &H206   'Double-click

' Datatype for systray entry
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 lLastIco As Long
Private lOldIco As Long
Private sOldStatus As String
Private nSysTray As NOTIFYICONDATA

'''''''''''''''''''''''''''''''''''''''''''''
' AddSysTray adds the initial systray entry for a form
' which can be manipulated by the other sub routines.
' This entry must be made in order for the modifications
' to work.
'
Public Sub AddSysTray(frm As form)
    With nSysTray
       .cbSize = Len(nSysTray)
       .hwnd = frm.hwnd
       .uId = vbNull
       .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
       .ucallbackMessage = WM_MOUSEMOVE
       .hIcon = frmSvPOPSet.imgInit.Picture.Handle
       .szTip = "Initialising..." & vbNullChar
    End With
    Call Shell_NotifyIcon(NIM_ADD, nSysTray)
End Sub

' Saves a systray entry for a form to memory
' so it can be restored later on
Public Sub SaveSysTray()
    With nSysTray
       lOldIco = .hIcon
       sOldStatus = .szTip
    End With
End Sub

' Restores a saved systray entry to an
' existing systray entry for the form
Public Sub RestoreSysTray(frm As form)
    With nSysTray
       .cbSize = Len(nSysTray)
       .hwnd = frm.hwnd
       .uId = vbNull
       .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
       .ucallbackMessage = WM_MOUSEMOVE
       .hIcon = lOldIco
       .szTip = sOldStatus
    End With
    Call Shell_NotifyIcon(NIM_MODIFY, nSysTray)
End Sub

' Modifies just the TTT (tool tip text) of the systray entry for the form
Public Sub ModSysTrayStatus(frm As form, sStatus As String)
    With nSysTray
       .cbSize = Len(nSysTray)
       .hwnd = frm.hwnd
       .uId = vbNull
       .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
       .ucallbackMessage = WM_MOUSEMOVE
       .hIcon = lLastIco
       .szTip = sStatus & vbNullChar
    End With
    Call Shell_NotifyIcon(NIM_MODIFY, nSysTray)
End Sub

' Modifies both the icon and the TTT (tool tip text) in the systray
' entry for the form
Public Sub ModSysTray(form As form, sStatus As String, iIconHandle As Long)
    With nSysTray
        'insert new values
       .cbSize = Len(nSysTray)
       .hwnd = frm.hwnd
       .uId = vbNull
       .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
       .ucallbackMessage = WM_MOUSEMOVE
       .hIcon = iIconHandle
       .szTip = sStatus & vbNullChar
           lLastIco = .hIcon
    End With
    Call Shell_NotifyIcon(NIM_MODIFY, nSysTray)
End Sub

' Modifies the icon of the systray entry
Public Sub ModSysTrayIco(frm As form, iIconHandle As Long)
    With nSysTray
       .cbSize = Len(nSysTray)
       .hwnd = frm.hwnd
       .uId = vbNull
       .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
       .ucallbackMessage = WM_MOUSEMOVE
       .hIcon = iIconHandle
       .szTip = .szTip
           lLastIco = .hIcon
    End With
    Call Shell_NotifyIcon(NIM_MODIFY, nSysTray)
End Sub

' Removes systray entry for the form
Public Sub DelSysTray()
    Call Shell_NotifyIcon(NIM_DELETE, nSysTray)
End Sub

' Generates a popup menu for the systray icon
Public Sub SysTrayPopupMenu(frm As form, lMsg As Long, bFlag As Boolean)
    If Not frm.Enabled Then Exit Sub
    If Not bFlag Then
        bFlag = True
        Select Case lMsg
            Case Is = WM_LBUTTONDBLCLK
                If Not bBusy Then
                    frm.ExeMailClient
                Else
                    Beep
                End If
            Case Is = WM_RBUTTONUP
                SetForegroundWindow frm.hwnd
                frm.PopupMenu frm.mnuSettings
        End Select
        blnFlag = False
    End If
End Sub

' Docks a form to the systray as an icon
Public Sub FormToSysTray(frm As form, bShowTB As Boolean)
    With nSysTray
        .cbSize = Len(nSysTray)
        .hwnd = frm.hwnd
        .uId = vbNull
        .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        .ucallbackMessage = WM_MOUSEMOVE
        .hIcon = frm.Icon
        .szTip = frm.Caption & vbNullChar
    End With
    Call Shell_NotifyIcon(NIM_ADD, nSysTray)
    App.TaskVisible = bShowTB
    frm.Hide
End Sub

' Undocks a form that has been docked to the systray as an icon
' and restores it to its original state
Public Sub SysTrayToForm(frm As form, bShowTB As Boolean)
    Call Shell_NotifyIcon(NIM_DELETE, nSysTray)
    frm.Show
    App.TaskVisible = bShowTB
End Sub
