Option ExplicitPublic preWinProc As Long Public NewForm As Form Public NewMenu As MenuPublic Const WM_USER = &H400 Public Const WM_LBUTTONUP = &H202 Public Const WM_MBUTTONUP = &H208 Public Const WM_RBUTTONUP = &H205 Public Const TRAY_CALLBACK = (WM_USER + 1001&) Public Const GWL_WNDPROC = (-4) Public Const GWL_USERDATA = (-21) Public Const NIF_ICON = &H2 Public Const NIF_TIP = &H4 Public Const NIM_ADD = &H0 Public Const NIF_MESSAGE = &H1 Public Const NIM_MODIFY = &H1 Public Const NIM_DELETE = &H2Declare 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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) 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 TypePrivate NOTI As NOTIFYICONDATAPublic Function NewWindone(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long If Msg = TRAY_CALLBACK Then
If lParam = WM_LBUTTONUP Then ' 单击左键,弹出窗口 MsgBox "left button" ' NewForm.Show ' NewForm.WindowState = 0 Exit Function End If If lParam = WM_RBUTTONUP Then ' 单击右键,弹出菜单 NewForm.PopupMenu NewMenu Exit Function End If End If NewWindone = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam) End FunctionPublic Sub AddToTray(frm As Form, mnu As Menu)
With NOTI .uID = 0 .hwnd = frm.hwnd .cbSize = Len(NOTI) .hIcon = frm.Icon.Handle .uFlags = NIF_ICON .uCallbackMessage = TRAY_CALLBACK .uFlags = .uFlags Or NIF_MESSAGE .cbSize = Len(NOTI) End With Shell_NotifyIcon NIM_ADD, NOTI End Sub '屏蔽托盘 Public Sub RemoveFromTray() With NOTI .uFlags = 0 End With Shell_NotifyIcon NIM_DELETE, NOTI
SetWindowLong NewForm.hwnd, GWL_WNDPROC, preWinProc End SubPublic Sub SetTrayTip(tip As String) With NOTI .szTip = tip & vbNullChar .uFlags = NIF_TIP End With Shell_NotifyIcon NIM_MODIFY, NOTI End SubPublic Sub SetTrayIcon(pic As Picture) If pic.Type <> vbPicTypeIcon Then Exit Sub With NOTI .hIcon = pic.Handle .uFlags = NIF_ICON End With Shell_NotifyIcon NIM_MODIFY, NOTI End Sub 需要时AddToTray 退出时一定不要忘记RemoveFromTray
Public NewForm As Form
Public NewMenu As MenuPublic Const WM_USER = &H400
Public Const WM_LBUTTONUP = &H202
Public Const WM_MBUTTONUP = &H208
Public Const WM_RBUTTONUP = &H205
Public Const TRAY_CALLBACK = (WM_USER + 1001&)
Public Const GWL_WNDPROC = (-4)
Public Const GWL_USERDATA = (-21)
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIF_MESSAGE = &H1
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2Declare 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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) 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 TypePrivate NOTI As NOTIFYICONDATAPublic Function NewWindone(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = TRAY_CALLBACK Then
If lParam = WM_LBUTTONUP Then
' 单击左键,弹出窗口
MsgBox "left button"
' NewForm.Show
' NewForm.WindowState = 0
Exit Function
End If
If lParam = WM_RBUTTONUP Then
' 单击右键,弹出菜单
NewForm.PopupMenu NewMenu
Exit Function
End If
End If
NewWindone = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End FunctionPublic Sub AddToTray(frm As Form, mnu As Menu)
Set NewForm = frm
Set NewMenu = mnu
preWinProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf NewWindone)
With NOTI
.uID = 0
.hwnd = frm.hwnd
.cbSize = Len(NOTI)
.hIcon = frm.Icon.Handle
.uFlags = NIF_ICON
.uCallbackMessage = TRAY_CALLBACK
.uFlags = .uFlags Or NIF_MESSAGE
.cbSize = Len(NOTI)
End With
Shell_NotifyIcon NIM_ADD, NOTI
End Sub
'屏蔽托盘
Public Sub RemoveFromTray()
With NOTI
.uFlags = 0
End With
Shell_NotifyIcon NIM_DELETE, NOTI
SetWindowLong NewForm.hwnd, GWL_WNDPROC, preWinProc
End SubPublic Sub SetTrayTip(tip As String)
With NOTI
.szTip = tip & vbNullChar
.uFlags = NIF_TIP
End With
Shell_NotifyIcon NIM_MODIFY, NOTI
End SubPublic Sub SetTrayIcon(pic As Picture) If pic.Type <> vbPicTypeIcon Then Exit Sub
With NOTI
.hIcon = pic.Handle
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, NOTI
End Sub
需要时AddToTray
退出时一定不要忘记RemoveFromTray