最小化到系统托盘 - 翼展电脑服务中心


最小化到系统托盘图标

2022-9-20 乱云飞 评论(0) 浏览(169) 标签: VB源码 缩小到系统托盘 最小化到系统托盘

'添加一标准模块,加入以下代码:
Option Explicit
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Const MAX_TOOLTIP As Integer = 64
Const NIF_ICON = &H2
Const NIF_MESSAGE = &H1
Const NIF_TIP = &H4
Const NIM_ADD = &H0
Const NIM_DELETE = &H2
Const WM_MOUSEMOVE = &H200
Const WM_LBUTTONUP = &H202
Const WM_RBUTTONUP = &H205

Private Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * MAX_TOOLTIP
End Type
Private nfIconData As NOTIFYICONDATA

Public Sub TheTray(Fem As Form) '最小化到托盘
nfIconData.hwnd = Fem.hwnd
nfIconData.uID = Fem.Icon '这里确定使用哪个图标
nfIconData.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
nfIconData.uCallbackMessage = WM_MOUSEMOVE
nfIconData.hIcon = Fem.Icon.Handle
nfIconData.szTip = Fem.Caption & vbNullChar '这里是将鼠标移到图标上时,将显示的文字
nfIconData.cbSize = Len(nfIconData)
Call Shell_NotifyIcon(NIM_ADD, nfIconData)
Fem.Hide
End Sub

Public Sub IconResponse(Fem As Form, X As Single)
Dim lMsg As Single
lMsg = X / Screen.TwipsPerPixelX
Select Case lMsg
Case WM_RBUTTONUP '鼠标右键按下
'此处添加弹出菜单语句
Case WM_LBUTTONUP '鼠标左键按下
Fem.WindowState = 0
Fem.Show
Call UnloadIcon
End Select
End Sub

'将图标从托盘中消除
Public Sub UnloadIcon()
Shell_NotifyIcon NIM_DELETE, nfIconData
End Sub

'--------------------------------------------------------------------------------------
'窗体调用,代码如下:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call IconResponse(Me, X)
End Sub

Private Sub Form_Resize()
If Me.WindowState = 1 Then
Call TheTray(Me)
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call UnloadIcon
End Sub
'--------------------------------------------------------------------------------------

预ICP备10086-001号 © 翼展网/80C.CC 技术支持/洛阳翼展科技
TEL / 13213610060 QQ / 345794501
Powered by emlog