Declare
Declare Function CallWindowProc Lib "USER32" Alias "CallWindowProcA" _
	(ByVal lpPrevWndFunc As Long, _
	ByVal hwnd As Long, _
	ByVal msg As Long, _
	ByVal wParam As Long, _
	ByVal lParam As Long) As Long
' System Tray

Public Type NOTIFYICONDATA
	cbsize As Long
	hwnd As Long
	uID As Long
	uFlag As Long
	uCallBackMessage As Long
	hIcon As Long
	szTip As String * 64
End Type

Public Const NIM_ADD As Long = &H0
Public Const NIM_MODIFY As Long = &H1
Public Const NIM_DELETE As Long = &H2

Public Const NIF_ICON As Long = &H2 'adding an ICON
Public Const NIF_TIP As Long = &H4 'adding a TIP
Public Const NIF_MESSAGE As Long = &H1 'want return messages


Public NID As NOTIFYICONDATA

Public Declare Function Shell_NotifyIcon Lib "shell32.dll" _
	Alias "Shell_NotifyIconA" _
	(ByVal dwMessage As Long, _
	lpData As NOTIFYICONDATA) As Long
Procedures
Public Sub addSysIcon(hwnd As Long, ID As Long, IconImage As Long, Tip As String, msgID As Long, NID As NOTIFYICONDATA)

	NID.cbsize = Len(NID)
	NID.hwnd = hwnd
	NID.uID = ID ' User-defined value 1001
	NID.uFlag = NIF_MESSAGE + NIF_ICON + NIF_TIP
	NID.hIcon = IconImage
	NID.szTip = Tip + Chr(0)
	NID.uCallBackMessage = msgID

	Shell_NotifyIcon NIM_ADD, NID
End Sub

Public Sub changeSysIcon(hwnd As Long, ID As Long, IconImage As Long, Tip As String, msgID As Long, NID As NOTIFYICONDATA)

	NID.cbsize = Len(NID)
	NID.hwnd = hwnd
	NID.uID = ID
	NID.uFlag = NIF_MESSAGE + NIF_ICON + NIF_TIP
	NID.hIcon = IconImage
	NID.szTip = Tip + Chr(0)
	NID.uCallBackMessage = msgID

	Shell_NotifyIcon NIM_MODIFY, NID
End Sub
Public Sub UnSubClass()
	'restore the default message handling
	'before exiting
	On Error Resume Next
	If defWindowProc Then
		SetWindowLong fMain.hwnd, GWL_WNDPROC, defWindowProc
		defWindowProc = 0
	End If
End Sub

Public Sub SubClass(hwnd As Long)
	'assign our own window message
	'procedure (WindowProc)
	On Error Resume Next
	defWindowProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
 
Public Function WindowProc(ByVal hwnd As Long, _
                        ByVal uMsg As Long, _
                        ByVal wParam As Long, _
                        ByVal lParam As Long) As Long

    Dim Tip As String
    Dim ValidPass As String
    
    ' Respond to mouse event in systray
    On Error GoTo WndProcErr
    'On Error Resume Next
    'Debug.Print uMsg
    'Debug.Print lParam
    Select Case hwnd
     'form-specific handler
      Case fMain.hwnd
        'Debug.Print "hwnd"
         Select Case uMsg
          'check uMsg for the application-defined
          'identifier (NID.uID) assigned to the
          'systray icon in NOTIFYICONDATA (NID).
  
           'WM_MYHOOK was defined as the message sent
           'as the .uCallbackMessage member of
           'NOTIFYICONDATA the systray icon
            Case WM_MYHOOK
             'Debug.Print "uMsg - myhook"
              
              Select Case fMain.Winsock(sIdx).State
                Case sckClosed
                   Tip = "x Chat - [Closed]"
                Case sckListening
                   Tip = "x Chat - [Listening]"
                Case sckConnected
                   Tip = "x Chat - [Connected] " & fMain.Winsock(sIdx).RemoteHostIP
                Case sckError
                   Tip = "x Chat - [Socket Error]"
                Case sckConnecting
                    Tip = "x Chat - [Connecting]"
              End Select
              changeSysIcon fMain.hwnd, 1001, fMain.Icon, Tip, WM_MYHOOK, NID
              'NID.szTip = tip + Chr(0)
              
              'lParam is the value of the message
              'that generated the tray notification.
               Select Case lParam
                  Case WM_RBUTTONUP
                    
                    If ActiveProtect And PassSet2 Then
                        fMain.mnuSysAutoConnect.Enabled = False
                        fMain.mnuSysProprerty.Enabled = False
                    End If
                    If frmUnlock.Visible = False And frmPassLock.Visible = False Then _
                        fMain.PopupMenu fMain.mnuTray
                  Case WM_LBUTTONDBLCLK
                    If frmUnlock.Visible = False Then fMain.ShowMain
               End Select
           'handle any other form messages by
           'passing to the default message proc
            Case Else
               WindowProc = CallWindowProc(defWindowProc, _
                                            hwnd, _
                                            uMsg, _
                                            wParam, _
                                            lParam)
               Exit Function
         End Select
     'this takes care of messages when the
     'handle specified is not that of the form
      Case Else
          WindowProc = CallWindowProc(defWindowProc, _
                                      hwnd, _
                                      uMsg, _
                                      wParam, _
                                      lParam)
   End Select

WndProcErr:
    If Err.Number <> 0 Then
        'removeSysIcon fMain.hwnd, 1001
        End
    End If
End Function