vb6.0实现系统托盘 源代码
时间: 2023-08-21 12:00:45 浏览: 193
在VB6.0中,要实现系统托盘的功能,可以使用Shell_NotifyIcon API来操作系统托盘。以下是一个基本的示例代码:
首先,在窗体的General Declarations部分添加如下代码:
Private Const NIM_ADD As Long = &H0
Private Const NIM_DELETE As Long = &H2
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_ICON As Long = &H2
Private Const NIF_TIP As Long = &H4
Private Const WM_MOUSEMOVE As Long = &H200
Private Const WM_LBUTTONUP As Long = &H202
Private 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
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Private Declare Function LoadIcon Lib "user32" Alias "LoadIconA" (ByVal hInstance As Long, ByVal lpIconName As Long) As Long
接下来,在窗体的Load事件中添加如下代码:
Private Sub Form_Load()
Dim nid As NOTIFYICONDATA
nid.cbSize = Len(nid)
nid.hwnd = Me.hwnd
nid.uID = vbNull
nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
nid.uCallbackMessage = WM_MOUSEMOVE
nid.hIcon = LoadIcon(0, ByVal IDI_APPLICATION)
nid.szTip = "系统托盘"
Shell_NotifyIcon NIM_ADD, nid
End Sub
然后,在窗体的QueryUnload事件中添加如下代码,实现点击关闭按钮时隐藏窗体到系统托盘:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
Cancel = 1
Me.Hide
End If
End Sub
最后,在窗体的MouseDown事件中添加如下代码,实现双击系统托盘图标显示窗体:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
Me.Show
Me.WindowState = vbNormal
End If
End Sub
通过以上代码,我们就可以实现在VB6.0中添加系统托盘的功能了。当程序运行时,窗体会自动隐藏到系统托盘图标中,双击图标即可显示窗体。
阅读全文