'form1'
Private Sub Form_Load()
Hook Me
TraySet Me, " - Off-line", Me.Icon
End Sub
'module FormHook
Option Explicit
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
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Public Const GWL_WNDPROC = -4
Public Const WM_LBUTTONUP = &H202
Public lpPrevWndProc As Long
Global gHW As Long
Global gHW1 As Long
Global appForm As Form
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Sub Hook(frm As Form)
Set appForm = frm
lpPrevWndProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHook()
Dim lngReturnValue As Long
lngReturnValue = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub
Function WindowProc(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim retProc As Boolean
On Error Resume Next
Select Case uMsg
Case WM_LBUTTONUP
If GetAsyncKeyState(vbLeftButton) < 0 And bTraySet Then
Sleep 150
TrayRestore Main
End If
If GetAsyncKeyState(vbRightButton) < 0 And bTraySet Then
TrayNotify.TrayMenu Main
End If
retProc = True
Case Else
retProc = True
End Select
If retProc Then
WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
Else
WindowProc = 0
End If
End Function
'module TrayNotify
Option Explicit
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Declare Function CreatePopupMenu Lib "user32" () As Long
Public Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, ByVal lprc As Any) As Long
Public Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Public Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Public 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
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const MF_GRAYED = &H1&
Public Const MF_STRING = &H0&
Public Const MF_SEPARATOR = &H800&
Public Const TPM_NONOTIFY = &H80&
Public Const TPM_RETURNCMD = &H100&
Public Const TPM_HORNEGANIMATION = &H800&
Public Const TPM_HORPOSANIMATION = &H400&
Public Const TPM_VERNEGANIMATION = &H2000&
Public Const TPM_VERPOSANIMATION = &H1000&
Public Const MF_UNCHECKED = &H0&
Public Const MF_CHECKED = &H8&
Public Const MF_POPUP = &H10&
Public Const MF_MENUBARBREAK = &H20&
Public Const MF_MENUBREAK = &H40&
Public bTraySet As Boolean
Public lMenu As Long
Public nID As NOTIFYICONDATA
Public Sub TraySet(frm As Form, sztip As String, hIcon As Long)
With nID
.cbSize = Len(nID)
.hIcon = hIcon
.hwnd = frm.hwnd
.sztip = sztip & vbNullChar
.uCallbackMessage = WM_LBUTTONUP
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
.uid = 1&
End With
Shell_NotifyIcon NIM_ADD, nID
frm.Hide
bTraySet = True
End Sub
Public Sub TrayRestore(frm As Form)
Dim nID As NOTIFYICONDATA
With nID
.cbSize = Len(nID)
.hwnd = frm.hwnd
.uid = 1&
End With
Shell_NotifyIcon NIM_DELETE, nID
frm.Show
bTraySet = False
Main.TrayTimer.Enabled = False
End Sub
Public Sub TrayMenu(frm As Form)
MsgBox "test"
End Sub
Public Sub TrayModify(frm As Form, sztip As String, hIcon As Long)
With nID
.cbSize = Len(nID)
.hIcon = hIcon
.hwnd = frm.hwnd
.sztip = sztip & vbNullChar
.uCallbackMessage = WM_LBUTTONUP
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
.uid = 1&
End With
Shell_NotifyIcon NIM_MODIFY, nID
bTraySet = True
End Sub
' --------- ' end
If you want you program were running while you track popup menu you have to put you main code in diferent process:
Declare Function CreateProcess Lib "kernel32" ...) As Long