'Module1中放
Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Public Const NIM_ADD = &H0
Public Const NIF_MESSAGE = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const WM_MOUSEMOVE = &H200
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 TrayIcon As NOTIFYICONDATA
'form1
Private Sub Form_Resize()
If Me.WindowState = 1 Then
TrayIcon.cbSize = Len(TrayIcon)
TrayIcon.hWnd = Me.hWnd
TrayIcon.uId = vbNull
TrayIcon.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
TrayIcon.ucallbackMessage = WM_MOUSEMOVE
TrayIcon.hIcon = Me.Icon
TrayIcon.szTip = "Mind's Tray Icon Example" & Chr$(0)
Call Shell_NotifyIcon(NIM_ADD, TrayIcon)
App.TaskVisible = False
Me.Hide
End If
End Sub
Private 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
Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Dim TrayIcon As NOTIFYICONDATA
Private Sub Form_Load()
TrayIcon.cbSize = Len(TrayIcon)
TrayIcon.hWnd = hWnd
TrayIcon.uId = vbNull
TrayIcon.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
TrayIcon.ucallbackMessage = WM_MOUSEMOVE
TrayIcon.hIcon = Me.Icon
TrayIcon.szTip = "拖盘" & Chr(0)
'在系统栏添加拖盘
Call Shell_NotifyIcon(NIM_ADD, TrayIcon)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'构造系统拖盘结构
TrayIcon.cbSize = Len(TrayIcon)
TrayIcon.hWnd = Me.hWnd
TrayIcon.uId = vbNull
'删除系统栏的拖盘
Call Shell_NotifyIcon(NIM_DELETE, TrayIcon)
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
On Error Resume Next
Dim Message As Long
Message = X / Screen.TwipsPerPixelX
'判断鼠标消息,执行不同的功能
Select Case Message
Case WM_LBUTTONDOWN
'点击拖盘
Case WM_RBUTTONUP
SetForegroundWindow Me.hWnd
'按右键弹出菜单
'PopupMenu mnuPopup
End Select
End Sub