Attribute VB_Name = "mTrayIcon"
Option Explicit

'-------------------------------------------------------
' API Declares....
'-------------------------------------------------------
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcW" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByRef lParam As Long) As Long '-İRd-
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongW" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

'-------------------------------------------------------
' API Constants...
'-------------------------------------------------------

' Identifier of the notification message (Private Window Messages)
Public Const WM_USER = &H400&
Public Const TRAY_CALLBACK = (WM_USER + 101&) ' Used in uCallbackMessage

Private Const GWL_WNDPROC = (-4&)

'---------------------------------------------------------
Private TrayIconCtrl As TrayIcon ' TrayIcon class variable
'---------------------------------------------------------

Private m_PrevWndProc As Long
Private m_hWndTarget As Long

' Initialise the subclass window procedure.

'------------------------------------------------------------
Public Sub HookCtrl(ByVal Ctrl_hWnd As Long, ByVal TrayIcon As TrayIcon)
'------------------------------------------------------------
    On Error GoTo ErrHandler

    Set TrayIconCtrl = TrayIcon ' Set object pointer to local variable
    m_PrevWndProc = SetWindowLong(Ctrl_hWnd, GWL_WNDPROC, AddressOf SubWndProc)
    m_hWndTarget = Ctrl_hWnd

ErrHandler:
    If (Err <> 0&) Then Beep: Debug.Print "mTrayIcon.HookCtrl Error! #" & Err & " - " & Err.Description
'------------------------------------------------------------
End Sub
'------------------------------------------------------------

' Terminate the subclass window procedure.

'------------------------------------------------------------
Public Sub UnhookCtrl()
'------------------------------------------------------------
    On Error GoTo ErrHandler

    If (m_hWndTarget <> 0&) Then

        SetWindowLong m_hWndTarget, GWL_WNDPROC, m_PrevWndProc
        Set TrayIconCtrl = Nothing ' Nullify object pointer
        m_hWndTarget = 0&

    End If

ErrHandler:
    If (Err <> 0&) Then Beep: Debug.Print "mTrayIcon.UnhookCtrl Error! #" & Err & " - " & Err.Description
'------------------------------------------------------------
End Sub
'------------------------------------------------------------

' The SubWndProc procedure below allows the application to detect when the
' system has received a mouse message for your System Tray icon.

' The wParam argument contains the identifier of the tray icon that received
' the mouse message, and the lParam argument contains the actual message:

'-------------------------------------------------------
Private Function SubWndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'-------------------------------------------------------
' This is the control subclassed window proc.
'-------------------------------------------------------
    On Error GoTo ErrHandler

    Select Case Msg
      Case TRAY_CALLBACK ' Callback message received when user clicks on system tray...

        ' Send windows message/user event to control
        TrayIconCtrl.SendEvent lParam, wParam
        ' lParam contains the mouse message, wParam is the icon's ID

    End Select

ErrHandler:
    ' Forward all messages to previous window procedure...(This must be done)
    SubWndProc = CallWindowProc(m_PrevWndProc, hWnd, Msg, wParam, ByVal lParam)

    If (Err <> 0&) Then Beep: Debug.Print "mTrayIcon.SubWndProc Error! #" & Err & " - " & Err.Description
'-------------------------------------------------------
End Function
'-------------------------------------------------------
