1、TRAY.bas源代码
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
Public a As Long
'以下为 Shell_NotifyIcon将用到的常量
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIM_MODIFY = &H1
'Shell_NotifyIcon的函数声明
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'处理消息将用到的结构、常量、API声明
Type POINTAPI
x As Long
y As Long
End Type
Type Msg
hwnd As Long
message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
Public Const WM_USER = &H400
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_LBUTTONDOWN = &H201
Public Const GWL_WNDPROC = -4
Public trayflag As Boolean
Global lpPrevWndProc As Long
Global gHW As Long
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
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'以下过程为消息循环处理
Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If hw = Form1.hwnd And uMsg = WM_USER+100 Then'检测到鼠标点动托盘图标
Select Case lParam
Case WM_RBUTTONDOWN '鼠标右键按下
Form1.PopupMenu Form1.traymnu '弹出菜单
Case WM_LBUTTONDOWN '鼠标左键按下
Form1.PopupMenu Form1.mnutray2 '弹出菜单
Case Else
End Select
Else '调用缺省窗口指针
WindowProc = CallWindowProc(lpPrevWndProc, hw,uMsg, wParam, lParam)
End If
End Function
2、TRAY.frm源代码
Dim MyNot As NOTIFYICONDATA '定义一个托盘结构
Private Sub Command1_Click() '鼠标按下删除按钮
With MyNot
.hIcon = Form1.Icon '托盘图标指针
.hwnd = Form1.hwnd '窗体指针
.szTip = "" '弹出提示字符串
.uCallbackMessage = WM_USER+100 '对应程序定义的消息
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE '标志
.uID = 1 '图标识别符
.cbSize = Len(MyNot) '计算该结构所占字节数
End With
hh = Shell_NotifyIcon(NIM_DELETE, MyNot) '删除该图标
trayflag = False '图标删除后trayflag为假
End Sub
Private Sub Command2_Click() '按下创建按钮
Dim hh As Long
With MyNot
.hIcon = Form1.Icon
.hwnd = Form1.hwnd
.szTip = "托盘图标" & Chr(&H0)
.uCallbackMessage = WM_USER+100
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uID = 1
.cbSize = Len(MyNot)
End With
hh = Shell_NotifyIcon(NIM_ADD, MyNot) '添加一个托盘图标
trayflag = True '图标添加后trayflag为真
End Sub
Private Sub Command3_Click() '修改托盘图标
Dim hh As Long
Set P = LoadPicture("c:\dos\bridge.ico") '导入一个新图标
With MyNot
.hIcon = P '将托盘图标改为新图标
.hwnd = Form1.hwnd
.szTip = "桥梁图标" & Chr(&H0) '更改提示信息
.uCallbackMessage = WM_USER+100
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.uID = 1
.cbSize = Len(MyNot)
End With
hh = Shell_NotifyIcon(NIM_MODIFY, MyNot) '修改托盘的某些特征
End Sub
Private Sub Command4_Click() 'END按钮被按下
Quit '调用退出函数Quit
End Sub
Private Sub Form_Load()
gHW = Me.hwnd '取得本窗体指针
'下一句调用钩子函数,将自制消息处理函数钩入Windows的消息循环
hook
End Sub
Public Sub hook()
'利用AddressOf取得消息处理函数WindowProc的指针,并将其传给SetWindowLong
'lpPrevWndProc用来存储原窗口的指针
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOfWindowProc)
End Sub
Public Sub Unhook()
'本子程序用原窗口的指针替换WindowProc函数的指针,即关闭子类、退出消息循环
Dim temp As Long
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub
Private Sub mnuhideForm_Click() '点中弹出菜单的‘隐藏’功能
Form1.Hide '将窗体隐藏
End Sub
Private Sub mnumaxForm_Click() '点中弹出菜单的‘最大化’功能
formstatus (2) '窗体最大化
End Sub
Private Sub mnuminForm_Click() '点中弹出菜单的‘最小化’功能
formstatus (1) '窗体最小化
End Sub
Private Sub mnunorForm_Click() '点中弹出菜单的‘正常’功能
formstatus (0) '窗体还原到正常
End Sub
Public Sub quit() '退出
If trayflag = True Then Command1_Click '托盘图标仍在,模拟按下‘删除’按钮
Unhook '退出消息循环
Unload Me '卸载窗体
End Sub
Private Sub mnuQuit_Click() '点中弹出菜单的‘退出’功能
quit
End Sub
Public Sub formstatus(ByVal wstates) '根据传递的参数变化窗体的状态
Form1.WindowState = wstates '设置窗体的状态
Form1.Show '显示窗体
End Sub