新建工程myProject,新建窗体frmMain,新建模块modMain
及其他组件(cmdAdd,cmdModify,cmdDelete,iIcon1,iIcon2),如图:
frmMain中:
'*************************************************************************
'**定义对象
'*************************************************************************
Dim myData1 As NOTIFYICONDATA
Dim myData2 As NOTIFYICONDATA
'*************************************************************************
'**添加托盘图标
'*************************************************************************
Private Sub cmdAdd_Click()
AddSysTrayIcon myData1
End Sub
'*************************************************************************
'**删除托盘图标
'*************************************************************************
Private Sub cmdDelete_Click()
Err.Clear
DeleteSysTrayIcon myData1
DeleteSysTrayIcon myData2
Err.Clear
End Sub
'*************************************************************************
'**修改托盘图标
'*************************************************************************
Private Sub cmdModify_Click()
ModifySysTrayIcon myData2
End Sub
'*************************************************************************
'**设置托盘图标
'*************************************************************************
Private Sub Form_Load()
With myData1
.cbSize = Len(myData1)
.hwnd = Me.hwnd
.uId = 0
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.ucallbackMessage = WM_MOUSEMOVE
.hIcon = iIcon1.Picture.Handle
.szTip = "托盘程序测试!" & vbNullChar
End With
With myData2
.cbSize = Len(myData2)
.hwnd = Me.hwnd
.uId = 0
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.ucallbackMessage = WM_MOUSEMOVE
.hIcon = iIcon2.Picture.Handle
.szTip = "托盘程序测试!" & vbNullChar
End With
End Sub
'*************************************************************************
'**鼠标事件
'*************************************************************************
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Message As Long
Message = X / Screen.TwipsPerPixelX
Select Case Message
Case WM_RBUTTONUP
MsgBox "点击右键", vbInformation, "系统提示"
Case WM_LBUTTONUP
MsgBox "点击左键", vbInformation, "系统提示"
End Select
End Sub
-----------------------------------------------------------------------------------------------------------------------------------------------
modMain中:
'*************************************************************************
'**托盘程序
'*************************************************************************
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'*************************************************************************
'**shell_NotifyIcon中的 dwMessage可以是以下NIM_ADD、NIM_DELETE、NIM_MODIFY 标识符之一
'*************************************************************************
Public Const NIM_ADD = &H0 '在任务栏中增加一个图标
Public Const NIM_DELETE = &H2 '删除任务栏中的一个图标
Public Const NIM_MODIFY = &H1 '修改任务栏中个图标信息
'*************************************************************************
'**获得鼠标信息
'*************************************************************************
Public Const WM_MOUSEMOVE = &H200 '在图标上移动鼠标
Public Const WM_LBUTTONDOWN = &H201 '鼠标左键按下
Public Const WM_LBUTTONUP = &H202 '鼠标左键释放
Public Const WM_LBUTTONDBLCLK = &H203 '双击鼠标左键
Public Const WM_RBUTTONDOWN = &H204 '鼠标右键按下
Public Const WM_RBUTTONUP = &H205 '鼠标右键释放
Public Const WM_RBUTTONDBLCLK = &H206 '双击鼠标右键
Public Const WM_SETHOTKEY = &H32 '响应您定义的热键
'*************************************************************************
'**shell_NotifyIcon中的 NOTIFYICONDATA结构信息
'*************************************************************************
Public Type NOTIFYICONDATA
cbSize As Long '该数据结构的大小
hwnd As Long '处理任务栏中图标的窗口句柄
uId As Long '定义的任务栏中图标的标识
uFlags As Long '任务栏图标功能控制,可以是以下值的组合(一般全包括)
ucallbackMessage As Long '任务栏图标通过它与用户程序交换消息,处理该消息的窗口由hWnd决定
hIcon As Long '任务栏中的图标的控制句柄
szTip As String * 64 '图标的提示信息
End Type
'*************************************************************************
'**shell_NotifyIcon中的 NOTIFYICONDATA结构中uFlags的控制信息
'*************************************************************************
Public Const NIF_MESSAGE = &H1 'NIF_MESSAGE 表示发送控制消息;
Public Const NIF_ICON = &H2 'NIF_ICON表示显示控制栏中的图标;
Public Const NIF_TIP = &H4 'NIF_TIP表示任务栏中的图标有动态提示。
'*************************************************************************
'**函数名:AddSysTrayIcon
'**参 数:TrayIconData为NOTIFYICONDATA类型
'**功 能:添加图标到系统托盘
'**返回值:无
'*************************************************************************
Public Function AddSysTrayIcon(TrayIconData As NOTIFYICONDATA)
Shell_NotifyIcon NIM_ADD, TrayIconData
End Function
'*************************************************************************
'**函数名:DelectSysTrayIcon
'**参 数:TrayIconData为NOTIFYICONDATA类型
'**功 能:删除系统托盘图标
'**返回值:无
'*************************************************************************
Public Function DeleteSysTrayIcon(TrayIconData As NOTIFYICONDATA)
Shell_NotifyIcon NIM_DELETE, TrayIconData
End Function
'*************************************************************************
'**函数名:ModifySysTrayIcon
'**参 数:TrayIconData为NOTIFYICONDATA类型
'**功 能:修改系统托盘图标
'**返回值:无
'*************************************************************************
Public Function ModifySysTrayIcon(TrayIconData As NOTIFYICONDATA)
Shell_NotifyIcon NIM_MODIFY, TrayIconData
End Function