这个问题已经有人问过多次,可能搜索起来太困难,把我的代码关键部分贴出来吧:Windows 相关 API 声明:Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) 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 Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public 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自定义消息常量和全局变量:
Public g_lPrevProc As LongPublic Const WM_SYSTRAYMESSAGE = WM_USER + 1975FORM 初始化:Private Sub Form_Load()
g_lPrevProc = SetWindowLong(frmMain.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End SubWindowProc:Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
if uMsg = WM_SYSTRAYMESSAGE Then
If lParam = WM_LBUTTONUP Then
DeleteSysTrayIcon hwnd
frmMain.WindowState = vbNormal
frmMain.Show
ElseIf lParam = WM_RBUTTONUP Then
SetForegroundWindow frmMain.hwnd
frmMain.PopupMenu frmMain.mnuPopup
ElseIf lParam = WM_MOUSEMOVE Then
If Not g_bMouseMove Then
g_bMouseMove = True
g_strTipText = Format$(Now, "mm/dd/yyyy hh:nn:ss")
ModifySysTrayTip frmMain.hwnd, g_strTipText
End If
End If
Else
WindowProc = CallWindowProc(g_lPrevProc, hwnd, uMsg, wParam, lParam)
End If
End FunctionSYSTRAY 函数:Public Sub ModifySysTrayTip(hwnd As Long, strTipText As String)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hwnd = hwnd
.szTip = strTipText & vbNullChar
.uFlags = NIF_TIP
End With
Call Shell_NotifyIcon(NIM_MODIFY, stNotify)
End SubPublic Sub AddSysTrayIcon(hwnd As Long, hIcon As Long, strTipText As String)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hIcon = hIcon
.hwnd = hwnd
.szTip = strTipText & vbNullChar
.uCallbackMessage = WM_SYSTRAYMESSAGE
.uFlags = NIF_ICON + NIF_TIP + NIF_MESSAGE
End With
Call Shell_NotifyIcon(NIM_ADD, stNotify)
End SubPublic Sub DeleteSysTrayIcon(hwnd As Long)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hwnd = hwnd
End With
Call Shell_NotifyIcon(NIM_DELETE, stNotify)
End Sub仅供参考。
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public 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自定义消息常量和全局变量:
Public g_lPrevProc As LongPublic Const WM_SYSTRAYMESSAGE = WM_USER + 1975FORM 初始化:Private Sub Form_Load()
g_lPrevProc = SetWindowLong(frmMain.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End SubWindowProc:Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
if uMsg = WM_SYSTRAYMESSAGE Then
If lParam = WM_LBUTTONUP Then
DeleteSysTrayIcon hwnd
frmMain.WindowState = vbNormal
frmMain.Show
ElseIf lParam = WM_RBUTTONUP Then
SetForegroundWindow frmMain.hwnd
frmMain.PopupMenu frmMain.mnuPopup
ElseIf lParam = WM_MOUSEMOVE Then
If Not g_bMouseMove Then
g_bMouseMove = True
g_strTipText = Format$(Now, "mm/dd/yyyy hh:nn:ss")
ModifySysTrayTip frmMain.hwnd, g_strTipText
End If
End If
Else
WindowProc = CallWindowProc(g_lPrevProc, hwnd, uMsg, wParam, lParam)
End If
End FunctionSYSTRAY 函数:Public Sub ModifySysTrayTip(hwnd As Long, strTipText As String)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hwnd = hwnd
.szTip = strTipText & vbNullChar
.uFlags = NIF_TIP
End With
Call Shell_NotifyIcon(NIM_MODIFY, stNotify)
End SubPublic Sub AddSysTrayIcon(hwnd As Long, hIcon As Long, strTipText As String)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hIcon = hIcon
.hwnd = hwnd
.szTip = strTipText & vbNullChar
.uCallbackMessage = WM_SYSTRAYMESSAGE
.uFlags = NIF_ICON + NIF_TIP + NIF_MESSAGE
End With
Call Shell_NotifyIcon(NIM_ADD, stNotify)
End SubPublic Sub DeleteSysTrayIcon(hwnd As Long)
Dim stNotify As NOTIFYICONDATA
With stNotify
.cbSize = Len(stNotify)
.hwnd = hwnd
End With
Call Shell_NotifyIcon(NIM_DELETE, stNotify)
End Sub仅供参考。
能让别人更快的找到信息!
避免重复回答!!!