1.bas 模块中: Option Explicit 'user defined type required by Shell_NotifyIcon API call 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 'constants required by Shell_NotifyIcon API call: 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 WM_MOUSEMOVE = &H200 Public Const WM_LBUTTONDOWN = &H201 'Button down Public Const WM_LBUTTONUP = &H202 'Button up Public Const WM_LBUTTONDBLCLK = &H203 'Double-click Public Const WM_RBUTTONDOWN = &H204 'Button down Public Const WM_RBUTTONUP = &H205 'Button up Public Const WM_RBUTTONDBLCLK = &H206 'Double-click Public Declare Function SetForegroundWindow Lib "user32" _ (ByVal hwnd As Long) As Long Public Declare Function Shell_NotifyIcon Lib "shell32" _ Alias "Shell_NotifyIconA" _ (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean Public nid As NOTIFYICONDATA2.form1中 Private Sub Form_Load() With nid .cbSize = Len(nid) .hwnd = Me.hwnd .uId = vbNull .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE .uCallBackMessage = WM_MOUSEMOVE .hIcon = Me.Icon .szTip = "Your ToolTip" & vbNullChar End With
End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'this procedure receives the callbacks from the System Tray icon. Dim Result As Long Dim msg As Long 'the value of X will vary depending upon the scalemode setting If Me.ScaleMode = vbPixels Then msg = X Else msg = X / Screen.TwipsPerPixelX End If Select Case msg Case WM_LBUTTONUP '514 restore form window Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show Case WM_LBUTTONDBLCLK '515 restore form window Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show Case WM_RBUTTONUP '517 display popup menu Result = SetForegroundWindow(Me.hwnd) Me.PopupMenu Me.mPopupSys 'mpopupsys为菜单名 End Select End Sub Private Sub Form_Resize() 'this is necessary to assure that the minimized window is hidden If Me.WindowState = vbMinimized Then Me.Hide Shell_NotifyIcon NIM_ADD, nid End Sub Private Sub Form_Unload(Cancel As Integer) 'this removes the icon from the system tray Shell_NotifyIcon NIM_DELETE, nid End Sub Private Sub mPopExit_Click() 'called when user clicks the popup menu Exit command Unload Me End Sub Private Sub mPopRestore_Click() 'called when the user clicks the popup menu Restore command Dim Result As Long Me.WindowState = vbNormal Result = SetForegroundWindow(Me.hwnd) Me.Show End Sub
首先在你的窗体上放置一个PictureBox。本例假设该控件名为Picture1。将其Visible属性设为False。将其DragIcon属性设定为你想要显示的To 图标文件。使用下面的代码在Form_Load事件中创建提示图标。在Form_Unload事件中删除提示图标。把下面的代码放在一模块的声明段中。DefInt A-ZDeclare Function Shell_NotifyIconA Lib "SHELL32" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As LongType 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' commands & flags for NotifyIcons Global Const NIM_ADD = &H0& Global Const NIM_MODIFY = &H1 Global Const NIM_DELETE = &H2 Global Const NIF_MESSAGE = &H1 Global Const NIF_ICON = &H2 Global Const NIF_TIP = &H4 Global Const WM_MOUSEMOVE = &H200 Global NI as NOTIFYICONDATA创建提示图标将下面的代码放在Form_Load事件中以产生一个提示图标。所有的鼠标事件都将会传递到PictureBox的MouseMove事件中。' stock NOTIFYICONDATA structure NI.cbSize = Len(NI) 'length of this structure NI.hWnd =Picture1.hwnd 'control to receive messages NI.uID = 0 ' uniqueID NI.uFlags = NIF_MESSAGE or NIF_ICON or NIF_TIP ' Operation Flags NI.uCallbackMessage = WM_MOUSEMOVE ' message to send to control NI.hIcon = Picture1.DragIcon ' handle to Icon NI.szTip = "My Tool Tip" + Chr$(0) ' Tool Tip ' 必须给提示图标分配一个唯一的ID号 ' so increment until creation is successfulDo NI.uID = NI.uID + 1 result = Shell_NotifyIconA(NIM_ADD, NI) Loop While result = 0修改提示图标Modifying NOTIFYICON下面的例子可以修改图标NI.hIcon = Picture2.DragIcon NI.szTip = "Different Tool Tip" + Chr$(0) ' modifies an existing NotifyIcon result = Shell_NotifyIconA(NIM_MODIFY, NI)删除提示图标Deleting NOTIFYICON将下面的代码放在Form_Unload事件中' 删除已有的提示图标 result = Shell_NotifyIconA(NIM_DELETE, NI)下面的代码放在 PictureBox的MouseMove事件中' 从提示图标接收消息 ' 消息通过X参数传递Dim Msg as Long Msg = (X And &HFF) * &H100 Select Case Msg Case 0 ' 鼠标移动 ' 在此输入你的代码 Case &HF00 ' 鼠标左键被按下 ' 在此输入你的代码 Case &H1E00 ' ' 在此输入你的代码 Case &H2D00 ' 双击鼠标左键 ' 在此输入你的代码 Case &H3C00 ' 鼠标右键被按下 ' 在此输入你的代码 Case &H4B00 ' 鼠标右键弹起 ' 在此输入你的代码 Case &H5A00 ' 双击鼠标右键 ' 在此输入你的代码 End Select
Option Explicit 'user defined type required by Shell_NotifyIcon API call
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 'constants required by Shell_NotifyIcon API call:
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 WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDOWN = &H201 'Button down
Public Const WM_LBUTTONUP = &H202 'Button up
Public Const WM_LBUTTONDBLCLK = &H203 'Double-click
Public Const WM_RBUTTONDOWN = &H204 'Button down
Public Const WM_RBUTTONUP = &H205 'Button up
Public Const WM_RBUTTONDBLCLK = &H206 'Double-click Public Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Public Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean Public nid As NOTIFYICONDATA2.form1中
Private Sub Form_Load()
With nid
.cbSize = Len(nid)
.hwnd = Me.hwnd
.uId = vbNull
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uCallBackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon
.szTip = "Your ToolTip" & vbNullChar
End With
End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'this procedure receives the callbacks from the System Tray icon.
Dim Result As Long
Dim msg As Long
'the value of X will vary depending upon the scalemode setting
If Me.ScaleMode = vbPixels Then
msg = X
Else
msg = X / Screen.TwipsPerPixelX
End If
Select Case msg
Case WM_LBUTTONUP '514 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_LBUTTONDBLCLK '515 restore form window
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
Case WM_RBUTTONUP '517 display popup menu
Result = SetForegroundWindow(Me.hwnd)
Me.PopupMenu Me.mPopupSys 'mpopupsys为菜单名
End Select
End Sub Private Sub Form_Resize()
'this is necessary to assure that the minimized window is hidden
If Me.WindowState = vbMinimized Then Me.Hide
Shell_NotifyIcon NIM_ADD, nid
End Sub Private Sub Form_Unload(Cancel As Integer)
'this removes the icon from the system tray
Shell_NotifyIcon NIM_DELETE, nid
End Sub Private Sub mPopExit_Click()
'called when user clicks the popup menu Exit command
Unload Me
End Sub Private Sub mPopRestore_Click()
'called when the user clicks the popup menu Restore command
Dim Result As Long
Me.WindowState = vbNormal
Result = SetForegroundWindow(Me.hwnd)
Me.Show
End Sub
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type' commands & flags for NotifyIcons
Global Const NIM_ADD = &H0&
Global Const NIM_MODIFY = &H1
Global Const NIM_DELETE = &H2
Global Const NIF_MESSAGE = &H1
Global Const NIF_ICON = &H2
Global Const NIF_TIP = &H4
Global Const WM_MOUSEMOVE = &H200
Global NI as NOTIFYICONDATA创建提示图标将下面的代码放在Form_Load事件中以产生一个提示图标。所有的鼠标事件都将会传递到PictureBox的MouseMove事件中。' stock NOTIFYICONDATA structure
NI.cbSize = Len(NI) 'length of this structure
NI.hWnd =Picture1.hwnd 'control to receive messages
NI.uID = 0 ' uniqueID
NI.uFlags = NIF_MESSAGE or NIF_ICON or NIF_TIP ' Operation Flags
NI.uCallbackMessage = WM_MOUSEMOVE ' message to send to control
NI.hIcon = Picture1.DragIcon ' handle to Icon
NI.szTip = "My Tool Tip" + Chr$(0) ' Tool Tip ' 必须给提示图标分配一个唯一的ID号
' so increment until creation is successfulDo
NI.uID = NI.uID + 1
result = Shell_NotifyIconA(NIM_ADD, NI)
Loop While result = 0修改提示图标Modifying NOTIFYICON下面的例子可以修改图标NI.hIcon = Picture2.DragIcon
NI.szTip = "Different Tool Tip" + Chr$(0)
' modifies an existing NotifyIcon
result = Shell_NotifyIconA(NIM_MODIFY, NI)删除提示图标Deleting NOTIFYICON将下面的代码放在Form_Unload事件中' 删除已有的提示图标
result = Shell_NotifyIconA(NIM_DELETE, NI)下面的代码放在 PictureBox的MouseMove事件中' 从提示图标接收消息
' 消息通过X参数传递Dim Msg as Long
Msg = (X And &HFF) * &H100
Select Case Msg Case 0 ' 鼠标移动
' 在此输入你的代码 Case &HF00 ' 鼠标左键被按下
' 在此输入你的代码 Case &H1E00 '
' 在此输入你的代码 Case &H2D00 ' 双击鼠标左键
' 在此输入你的代码 Case &H3C00 ' 鼠标右键被按下
' 在此输入你的代码 Case &H4B00 ' 鼠标右键弹起
' 在此输入你的代码 Case &H5A00 ' 双击鼠标右键
' 在此输入你的代码 End Select