Private bIsQuit As Boolean
Private Sub Form_Load()
bIsQuit = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not bIsQuit Then
Dim rc As Long
Dim OwnerhWnd As Long
' 让窗体不可见
Me.Visible = False
' 从任务管理器列表中移除
OwnerhWnd = GetWindow(Me.HWnd, GW_OWNER)
rc = ShowWindow(OwnerhWnd, SW_HIDE)
Call Icon_Add(Form1, Me.topmenu, Me.Timer1, Me.HWnd, "ICON Test", Me.Icon, 0)
Cancel = 1
Else
Cancel = 0
End If
End Sub
Private Sub mnuQuit_Click()
bIsQuit = True
Call Icon_Del(Me.HWnd, 0) '删除通知栏图标
Unload Me
End Sub
Private Sub mnuShow_Click()
Call Icon_Del(Me.HWnd, 0) '删除通知栏图标
Me.Show '调出窗口
Me.WindowState = 0
End Sub
Private Sub Timer1_Timer()
Call Common.procesSD
End Sub
Common模块代码
'Window消息处理函数,目的是: 窗口最小化到托盘时,程序自己处理消息
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
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal HWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
'点击X时,最小化到托盘
Public Const SW_HIDE = 0
Public Const GW_OWNER = 4
Declare Function GetWindow Lib "user32" (ByVal HWnd As Long, ByVal wCmd As Long) As Long
Declare Function ShowWindow Lib "user32" (ByVal HWnd As Long, ByVal nCmdShow As Long) As Long
'添加、删除、更改托盘图标 Windows API
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Const DefaultIconIndex = 1 '图标缺省索引
Public Const WM_LBUTTONDOWN = &H201 '按鼠标左键
Public Const WM_RBUTTONDOWN = &H204 '按鼠标右键
Public Const WM_LBUTTONDBLCLK = &H203 '双击左键
Public Const WM_USER = &H400
Public Const TRAY_CALLBACK = (WM_USER + 1001&)
Public Const GWL_WNDPROC = (-4)
Public Const NIM_ADD = 0 '添加图标
Public Const NIM_MODIFY = 1 '修改图标
Public Const NIM_DELETE = 2 '删除图标
Public Const NIF_MESSAGE = 1 'message 有效
Public Const NIF_ICON = 2 '图标 *** 作(添加、修改、删除)有效
Public Const NIF_TIP = 4 'ToolTip(提示)有效
Type NOTIFYICONDATA
cbSize As Long '需填入NOTIFYICONDATA数据结构的长度
HWnd As Long '设置成窗口的句柄
Uid As Long '为图标所设置的ID值
UFlags As Long '设置uCallbackMessage,hIcon,szTip是否有效
UCallbackMessage As Long '消息编号
hIcon As Long '显示在状态栏上的图标
SzTip As String * 64 '提示信息
End Type
'声明上述类型的对象
Private IconVa As NOTIFYICONDATA
'声明获取处理Windows 消息前的window句柄
Public OldWindowProc As Long
'判断单击,双击的两个标志
Private bIsSingle As Boolean
Private bIsDouble As Boolean
'声明主窗体,右键菜单和时钟,这里的时钟是为了区分单双击左键,显得比较笨拙
Private frmMain As Form
Private topmenu As Menu
Private timMain As Timer
'函数定义
'添加图标至通知栏
Public Function Icon_Add(frmParent As Form, mnuParent As Menu, timParent As Timer, iHwnd As Long, sTips As String, hIcon As Long, IconID As Long) As Long
'参数说明:iHwnd:窗口句柄,sTips:当鼠标移到通知栏图标上时显示的提示内容
'hIcon:图标句柄,IconID:图标Id号
'初始化控件成员
Set frmMain = frmParent
Set topmenu = mnuParent
Set timMain = timParent
With IconVa
.HWnd = iHwnd
.SzTip = sTips + Chr$(0)
.hIcon = hIcon
.Uid = IconID
.UCallbackMessage = TRAY_CALLBACK
.cbSize = Len(IconVa)
.UFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
End With
'获得让程序处理事件的权利
OldWindowProc = SetWindowLong(iHwnd, GWL_WNDPROC, AddressOf NewWindowProc)
'初始化单双击的判别标志
bIsSingle = False
bIsDouble = False
'初始化时钟
timMain.Enabled = True
timMain.Interval = 500
Icon_Add = Shell_NotifyIcon(NIM_ADD, IconVa)
End Function
'删除通知栏图标(参数说明同Icon_Add)
Function Icon_Del(iHwnd As Long, lIndex As Long) As Long
Dim IconVa As NOTIFYICONDATA
Dim L As Long
With IconVa
.HWnd = iHwnd
.Uid = lIndex
.cbSize = Len(IconVa)
End With
'当图标从托盘删除后,关闭时钟,不在监视单双击事件
timMain.Enabled = True
'让 *** 作系统处理消息
SetWindowLong iHwnd, GWL_WNDPROC, OldWindowProc
Icon_Del = Shell_NotifyIcon(NIM_DELETE, IconVa)
End Function
'修改通知栏图标(参数说明同Icon_Add)
Public Function Icon_Modify(iHwnd As Long, sTips As String, hIcon As Long, IconID As Long) As Long
Dim IconVa As NOTIFYICONDATA
With IconVa
.HWnd = iHwnd
.SzTip = sTips + Chr$(0)
.hIcon = hIcon
.Uid = IconID
.UCallbackMessage = TRAY_CALLBACK
.cbSize = Len(IconVa)
.UFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
End With
Icon_Modify = Shell_NotifyIcon(NIM_MODIFY, IconVa)
End Function
'程序处理Windows传递的消息
Public Static Function NewWindowProc(ByVal HWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'如果用户点击了托盘中的图标,则进行判断是点击了左键单双击,还是右键
If Msg = TRAY_CALLBACK Then
'如果点击了右键
If lParam = WM_RBUTTONDOWN Then
'则d出右键菜单
frmMain.PopupMenu topmenu
Exit Function
End If
'如果双击左键
If lParam = WM_LBUTTONDBLCLK Then
bIsDouble = True
bIsSingle = False
Exit Function
End If
'如果点击了左键
If lParam = WM_LBUTTONDOWN Then
bIsSingle = True
bIsDouble = False
Exit Function
End If
End If
'如果是其他类型的消息则传递给原有默认的窗口函数
NewWindowProc = CallWindowProc(OldWindowProc, HWnd, Msg, wParam, lParam)
End Function
'被时钟调用,来区分单击,还是双击,以及事件处理内容
Public Sub procesSD()
'如果是双击,就恢复窗口,并把图标从托盘删除
If bIsDouble Then
Call Icon_Del(frmMain.HWnd, 0) '删除通知栏图标
frmMain.Show '调出窗口
frmMain.WindowState = 0
frmMain.SetFocus
ElseIf bIsSingle Then
'如果是单击,就d出左键菜单
frmMain.PopupMenu topmenu
End If
'恢复单双击判断的标志
bIsSingle = False
bIsDouble = False
End Sub
您好: vb中有一个控件是可以实现你要的功能的:需要找到VB的安装盘(不是安装以后的目录)的COMMON\TOOLS\VB\UNSUPPRT\SYSTRAY目录,将Systray目录拷到硬盘上面并编译为ocx控件(编译前记得要先把只读属性修改掉,有时候会提示要先保存,直接保存。),
然后在自己的VB程序中添加改控件(工程-部件-浏览)。
最好把这个ocx放到C:\WINDOWS\system32。
改控件的属性InTray属性用来设置是否显示在托盘中,True为显示在托盘,False为不显示。TrayIcon属性是在托盘中显示的图标式样。TrayTip属性是鼠标移到改控件上面显示的提示文字。如果要使程序最小化时显示到托盘,如下:
Private Sub Form_Resize()
If Me.WindowState = vbMinimized Then
cSysTray1.InTray = True
Me.Visible = FalseEnd If
End Sub
点击托盘图标后让程序显示出来,如下:
Private Sub cSysTray1_MouseDown(Button As Integer, Id As Long)
Me.WindowState = vbNormal
Me.Visible = True
cSysTray1.InTray = False
Me.SetFocus
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)