VB6托盘

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。


'以下为实现系统托盘的函数与变量及数据结构的声明
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_SYSCOMMAND = &H112
Private Const SC_RESTORE = &HF120&

Private LastState As Integer '记录原窗口状态变量

'---------- dwMessage可以是以下NIM_ADD、NIM_DELETE、NIM_MODIFY 标识符之一----------
Private Const NIM_ADD = &H0 '在任务栏中增加一个图标
Private Const NIM_DELETE = &H2 '删除任务栏中的一个图标
Private Const NIM_MODIFY = &H1 '修改任务栏中个图标信息

Private Const NIF_MESSAGE = &H1 'NOTIFYICONDATA结构中uFlags的控制信息
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

Private Const WM_MOUSEMOVE = &H200 '当鼠标指针移至图标上

Private Const WM_LBUTTONUP = &H202
Private Const WM_RBUTTONUP = &H205

Private Type NOTIFYICONDATA
cbSize As Long '该数据结构的大小
hwnd As Long '处理任务栏中图标的窗口句柄
uID As Long '定义的任务栏中图标的标识
uFlags As Long '任务栏图标功能控制,可以是以下值的组合(一般全包括)
'NIF_MESSAGE 表示发送控制消息;
'NIF_ICON表示显示控制栏中的图标;
'NIF_TIP表示任务栏中的图标有动态提示。
uCallbackMessage As Long '任务栏图标通过它与用户程序交换消息,处理该消息的窗口由hWnd决定
hIcon As Long '任务栏中的图标的控制句柄
szTip As String * 64 '图标的提示信息
End Type

Dim myData As NOTIFYICONDATA



'鼠标移动的过程
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case CLng(X)
Case WM_RBUTTONUP '鼠标在图标上右击时弹出菜单
Me.PopupMenu mnuTray
Case WM_LBUTTONUP '鼠标在图标上左击时窗口若最小化则恢复窗口位置
If Me.WindowState = vbMinimized Then
Me.Show
Me.WindowState = LastState
'Me.SetFocus
'Form_Activate
End If
End Select
End Sub

'窗口尺寸变化时记录窗口状态信息
Private Sub Form_Resize()
Select Case WindowState
Case vbMinimized
mnuMini.Enabled = False
mnuRestore.Enabled = True

Me.Hide '使窗口在任务栏上消失
Case vbNormal
mnuMini.Enabled = True
mnuRestore.Enabled = False
End Select
If WindowState <> vbMinimized Then LastState = WindowState
End Sub

'双击图标,恢复原有大小
Private Sub Form_DblClick()
SendMessage hwnd, WM_SYSCOMMAND, SC_RESTORE, 0&
End Sub

'菜单"最小化"过程
Private Sub mnuMini_Click()
WindowState = vbMinimized
End Sub

'菜单"恢复"过程
Private Sub mnuRe

store_Click()
SendMessage hwnd, WM_SYSCOMMAND, SC_RESTORE, 0&
End Sub

'菜单"关闭"过程
Private Sub mnuClose_Click()
Unload Me
End Sub

'窗口卸载时,释放助手资源
Private Sub Form_Unload(Cancel As Integer)

Shell_NotifyIcon NIM_DELETE, myData '窗口卸载时,将状态栏中的图标一同卸载
Set agtReal = Nothing
clearLink
End
End Sub



'为实现系统托盘的初始化过程
Private Sub trayLoad()
If WindowState = vbMinimized Then
LastState = vbNormal
Else
LastState = WindowState
End If

With myData
.cbSize = Len(myData)
.hwnd = Me.hwnd
.uID = 0
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon.Handle '默认为窗口图标
.szTip = Me.Caption & vbNullChar
End With

Shell_NotifyIcon NIM_ADD, myData

End Sub


相关文档
最新文档