|
<FONT size=2>将图标添加到托盘的函数,参见模块中的解释
'注意了这是从主程序到模块的入口,本例中并没有直接调用Shell_NotifyIcon函数
AddToTray Me, mnuTray
SetTrayTip "托盘图标演示,点击右键弹出菜单"
End Sub
'在主窗体Form1大小改变时,相应改变右键菜单mnuTray的菜单项的可用属性Enabled
Private Sub Form_Resize()
Select Case WindowState
'如果窗体最小化了,把菜单项“最大化”“恢复”设为可用,
'而把“最小化”“移动”“大小”三项设为不可用.
'如果这时在托盘图标上点击鼠标右键,会发现不可用项变为灰色
Case vbMinimized
mnuTrayMaximize.Enabled = True
mnuTrayMinimize.Enabled = False
mnuTrayMove.Enabled = False
mnuTrayRestore.Enabled = True
mnuTraySize.Enabled = False
'窗体最大化时
Case vbMaximized
mnuTrayMaximize.Enabled = False
mnuTrayMinimize.Enabled = True
mnuTrayMove.Enabled = False
mnuTrayRestore.Enabled = True
mnuTraySize.Enabled = False
'一般状态下
Case vbNormal
mnuTrayMaximize.Enabled = True
mnuTrayMinimize.Enabled = True
mnuTrayMove.Enabled = True
mnuTrayRestore.Enabled = False
mnuTraySize.Enabled = True
End Select
If WindowState <> vbMinimized Then LastState = WindowState
End Sub
'保证在程序退出时删除托盘图标
Private Sub Form_Unload(Cancel As Integer)
RemoveFromTray
End Sub
'“文件”菜单的“退出”项被点击时
Private Sub mnuFileExit_Click()
Unload Me
End Sub
'托盘图标右键菜单上的“退出”项被点击时
Private Sub mnuTrayClose_Click()
Unload Me
End Sub
'托盘图标右键菜单上的“最大化”项被点击时
Private Sub mnuTrayMaximize_Click()
WindowState = vbMaximized
End Sub
'托盘图标右键菜单上的“最小化”项被点击时
Private Sub mnuTrayMinimize_Click()
WindowState = vbMinimized
End Sub
'托盘图标右键菜单上的“移动”项被点击时
Private Sub mnuTrayMove_Click()
SendMessage HWnd, WM_SYSCOMMAND, _
SC_MOVE, 0&
End Sub
'托盘图标右键菜单上的“恢复”项被点击时
Private Sub mnuTrayRestore_Click()
SendMessage HWnd, WM_SYSCOMMAND, _
SC_RESTORE, 0&
End Sub
'托盘图标右键菜单上的“退出”项被点击时
Private Sub mnuTraySize_Click()
SendMessage HWnd, WM_SYSCOMMAND, _
SC_SIZE, 0&
End Sub
'-----------------------------------------
'以下为模块中的代码:
'-----------------------------------------
Option Explicit
Public OldWindowProc As Long
Public TheForm As Form
Public TheMenu As Menu
'【VB声明】
'Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal HWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'【说明】
' 此函数发送消息到一个窗口过程
'【返回值】
' Long,依据发送的消息不同而变化
'【参数表】
' lpPrevWndFunc----- Long,原来的窗口过程地址
' HWnd-------------- Long,窗口句柄
' Msg -------------- Long,发送的消息
' wParam ----------- Long,消息类型,参考wParam参数表
' lParam ----------- Long,依据wParam参数的不同而不同
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal HWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'【VB声明】
' Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'【说明】
' 在窗口结构中为指定的窗口设置信息
'【返回值】
' Long,指定数据的前一个值
'【参数表】
' hwnd ----------- Long,欲为其取得信息的窗口的句柄
' nIndex --------- Long,请参考GetWindowLong函数的nIndex参数的说明
' dwNewLong ------ Long,由nIndex指定的窗口信息的新值
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal HWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'【VB声明】
'Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'【说明】
'【参数表】
'参数dwMessage ---- 为消息设置值,它可以是以下 |
|