'------------------------------------------------------------------- '类模块:托盘图标的添加 '-------------------------------------------------------------------Option ExplicitPrivate Declare Function Shell_NotifyIcon Lib "shell32" Alias _ "Shell_NotifyIconA" (ByVal dwMessage As Long, _ pNid As NOTIFYICONDATA) As BooleanPrivate Const NIM_ADD = &H0 Private Const NIM_MODIFY = &H1 Private Const NIM_DELETE = &H2Private Const NIF_MESSAGE = &H1 Private Const NIF_ICON = &H2 Private Const NIF_TIP = &H4Private Const WM_MOUSEMOVE = &H200 Private Const WM_LBUTTONDOWN = &H201 Private Const WM_LBUTTONUP = &H202 Private Const WM_LBUTTONDBLCLK = &H203 Private Const WM_RBUTTONDOWN = &H204 Private Const WM_RBUTTONUP = &H205 Private Const WM_RBUTTONDBLCLK = &H206Private Type NOTIFYICONDATA lSize As Long hWnd As Long lId As Long lFlags As Long lCallBackMessage As Long hIcon As Long szTip As String * 64 End TypePrivate mNID As NOTIFYICONDATA Private WithEvents mPic As PictureBoxPublic Event RButtonDown() '鼠标右键按下 Public Event RButtonUp() '鼠标右键放开 Public Event RButtonDblClick() '鼠标右键双击 Public Event LButtonDown() '鼠标左键按下 Public Event LButtonUp() '鼠标左键放开 Public Event LButtonDblClick() '鼠标左键双击Private Sub Class_Initialize() With mNID .lSize = Len(mNID) .lCallBackMessage = WM_MOUSEMOVE .lFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE .lId = 1& End With End SubPrivate Sub Class_Terminate() DeleteIcon Set mPic = Nothing End SubPublic Property Let PicBox(ByVal PicBox As PictureBox) Set mPic = PicBox With mNID .hWnd = mPic.hWnd .hIcon = mPic End With End PropertyPublic Property Get TipText() As String TipText = mNID.szTip End PropertyPublic Property Let TipText(ByVal TipText As String) mNID.szTip = TipText & Chr$(0) Shell_NotifyIcon NIM_MODIFY, mNID End PropertyPublic Function ShowIcon() As Boolean If mPic Is Nothing Then ShowIcon = False Else Shell_NotifyIcon NIM_ADD, mNID ShowIcon = True End If End FunctionPublic Sub DeleteIcon() Shell_NotifyIcon NIM_DELETE, mNID End SubPrivate Sub mPic_Change() mNID.hIcon = mPic Shell_NotifyIcon NIM_MODIFY, mNID End SubPrivate Sub mPic_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Static bRec As Boolean Dim lMsg As Long lMsg = X / Screen.TwipsPerPixelX If bRec = False Then bRec = True Select Case lMsg Case WM_LBUTTONDBLCLK: 'RaiseEvent LButtonDblClick frmTray.Text1.Text = "左键双击" Case WM_LBUTTONDOWN: 'RaiseEvent LButtonDown frmTray.Text1.Text = "左键按下" Case WM_LBUTTONUP: 'RaiseEvent LButtonUp frmTray.Text1.Text = "左键放开" Case WM_RBUTTONDBLCLK: 'RaiseEvent RButtonDblClick frmTray.Text1.Text = "右键双击" Case WM_RBUTTONDOWN: 'RaiseEvent RButtonDown frmTray.Text1.Text = "右键按下" Case WM_RBUTTONUP: 'RaiseEvent RButtonUp frmTray.Text1.Text = "右键放开" End Select bRec = False End If End Sub '窗体: Option ExplicitDim WithEvents Tray As CTrayPrivate Sub Form_Load() Set Tray = New CTray With Tray '.TipText = Trim(txtTipText.Text) .PicBox = picNotify '一个用于托盘的图标 End With End SubPrivate Sub Form_Unload(Cancel As Integer) Set Tray = Nothing End SubPrivate Sub cmdShow_Click() Tray.ShowIcon '添加图标在托盘 End SubPrivate Sub cmdDelete_Click() Tray.DeleteIcon '删除托盘图标 End Sub Private Sub Tray_LButtonDblClick() With txtNotification .Text = "左键双击" & vbCrLf & .Text End With End SubPrivate Sub Tray_LButtonDown() With txtNotification .Text = "左键按下" & vbCrLf & .Text End With End SubPrivate Sub Tray_LButtonUp() With txtNotification .Text = "左键放开" & vbCrLf & .Text End With End SubPrivate Sub Tray_RButtonDblClick() With txtNotification .Text = "右键双击" & vbCrLf & .Text End With End SubPrivate Sub Tray_RButtonDown() With txtNotification .Text = "右键按下" & vbCrLf & .Text End With End SubPrivate Sub Tray_RButtonUp() With txtNotification .Text = "右键放开" & vbCrLf & .Text End With End Sub
http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=197lihonggen0写的例子:
http://www.csdn.net/cnshare/soft/16/16015.shtm
'类模块:托盘图标的添加
'-------------------------------------------------------------------Option ExplicitPrivate Declare Function Shell_NotifyIcon Lib "shell32" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, _
pNid As NOTIFYICONDATA) As BooleanPrivate Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_RBUTTONDOWN = &H204
Private Const WM_RBUTTONUP = &H205
Private Const WM_RBUTTONDBLCLK = &H206Private Type NOTIFYICONDATA
lSize As Long
hWnd As Long
lId As Long
lFlags As Long
lCallBackMessage As Long
hIcon As Long
szTip As String * 64
End TypePrivate mNID As NOTIFYICONDATA
Private WithEvents mPic As PictureBoxPublic Event RButtonDown() '鼠标右键按下
Public Event RButtonUp() '鼠标右键放开
Public Event RButtonDblClick() '鼠标右键双击
Public Event LButtonDown() '鼠标左键按下
Public Event LButtonUp() '鼠标左键放开
Public Event LButtonDblClick() '鼠标左键双击Private Sub Class_Initialize()
With mNID
.lSize = Len(mNID)
.lCallBackMessage = WM_MOUSEMOVE
.lFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.lId = 1&
End With
End SubPrivate Sub Class_Terminate()
DeleteIcon
Set mPic = Nothing
End SubPublic Property Let PicBox(ByVal PicBox As PictureBox)
Set mPic = PicBox
With mNID
.hWnd = mPic.hWnd
.hIcon = mPic
End With
End PropertyPublic Property Get TipText() As String
TipText = mNID.szTip
End PropertyPublic Property Let TipText(ByVal TipText As String)
mNID.szTip = TipText & Chr$(0)
Shell_NotifyIcon NIM_MODIFY, mNID
End PropertyPublic Function ShowIcon() As Boolean
If mPic Is Nothing Then
ShowIcon = False
Else
Shell_NotifyIcon NIM_ADD, mNID
ShowIcon = True
End If
End FunctionPublic Sub DeleteIcon()
Shell_NotifyIcon NIM_DELETE, mNID
End SubPrivate Sub mPic_Change()
mNID.hIcon = mPic
Shell_NotifyIcon NIM_MODIFY, mNID
End SubPrivate Sub mPic_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Static bRec As Boolean
Dim lMsg As Long lMsg = X / Screen.TwipsPerPixelX If bRec = False Then
bRec = True
Select Case lMsg
Case WM_LBUTTONDBLCLK:
'RaiseEvent LButtonDblClick
frmTray.Text1.Text = "左键双击"
Case WM_LBUTTONDOWN:
'RaiseEvent LButtonDown
frmTray.Text1.Text = "左键按下"
Case WM_LBUTTONUP:
'RaiseEvent LButtonUp
frmTray.Text1.Text = "左键放开"
Case WM_RBUTTONDBLCLK:
'RaiseEvent RButtonDblClick
frmTray.Text1.Text = "右键双击"
Case WM_RBUTTONDOWN:
'RaiseEvent RButtonDown
frmTray.Text1.Text = "右键按下"
Case WM_RBUTTONUP:
'RaiseEvent RButtonUp
frmTray.Text1.Text = "右键放开"
End Select
bRec = False
End If
End Sub
'窗体:
Option ExplicitDim WithEvents Tray As CTrayPrivate Sub Form_Load()
Set Tray = New CTray
With Tray
'.TipText = Trim(txtTipText.Text)
.PicBox = picNotify '一个用于托盘的图标
End With
End SubPrivate Sub Form_Unload(Cancel As Integer)
Set Tray = Nothing
End SubPrivate Sub cmdShow_Click()
Tray.ShowIcon '添加图标在托盘
End SubPrivate Sub cmdDelete_Click()
Tray.DeleteIcon '删除托盘图标
End Sub
Private Sub Tray_LButtonDblClick()
With txtNotification
.Text = "左键双击" & vbCrLf & .Text
End With
End SubPrivate Sub Tray_LButtonDown()
With txtNotification
.Text = "左键按下" & vbCrLf & .Text
End With
End SubPrivate Sub Tray_LButtonUp()
With txtNotification
.Text = "左键放开" & vbCrLf & .Text
End With
End SubPrivate Sub Tray_RButtonDblClick()
With txtNotification
.Text = "右键双击" & vbCrLf & .Text
End With
End SubPrivate Sub Tray_RButtonDown()
With txtNotification
.Text = "右键按下" & vbCrLf & .Text
End With
End SubPrivate Sub Tray_RButtonUp()
With txtNotification
.Text = "右键放开" & vbCrLf & .Text
End With
End Sub