运行这个示例的前提是系统需要Windows 2000 or XP (Shell version 5 or better)。
大家可能对托盘程序很熟悉了,这里我也不再多讲,这段小代码只是说明一下托盘的新特性:显示Balloon Tooltip。至于什么是Balloon Tooltip,只要把一下代码贴到VB中就会看到新鲜效果,再次提醒注意运行环境!
1.建立标准EXE工程,为Form1换一个PP的Icon,加入一个模块,改名为SysTray
SysTray.bas代码:
Option Explicit
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, _
lpData As NOTIFYICONDATA) As Long
Public Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uId As Long
uFlags As Long
uCallBackMessage As Long
hIcon As Long
szTip As String * 128
dwState As Long
dwStateMask As Long
szInfo As String * 256
uTimeoutOrVersion As Long '由于VB中没有Union类型,只能用Long型代替
szInfoTitle As String * 64
dwInfoFlags As Long
End Type
Public Const NOTIFYICON_VERSION = 3
Public Const NOTIFYICON_OLDVERSION = 0
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIM_SETFOCUS = &H3
Public Const NIM_SETVERSION = &H4
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const NIF_STATE = &H8
Public Const NIF_INFO = &H10
Public Const NIS_HIDDEN = &H1
Public Const NIS_SHAREDICON = &H2
Public Const NIIF_NONE = &H0
Public Const NIIF_WARNING = &H2
Public Const NIIF_ERROR = &H3
Public Const NIIF_INFO = &H1
Public nfIconData As NOTIFYICONDATA
2.Form1的代码:
Option Explicit
Private Sub Form_Load()
With nfIconData
.cbSize = Len(nfIconData)
.hwnd = Me.hwnd
.uId = vbNull
.uFlags = NIF_INFO Or NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.hIcon = Me.Icon
.szTip = "你的小消息可以放在此..." & vbNullChar
.dwState = 0
.dwStateMask = 0
.szInfo = "这是一个Balloon Style Tool-tip!又一个小技巧可以应用了 " & Chr(13) & ":)" & vbNullChar
.uTimeoutOrVersion = 15000
.szInfoTitle = "哈哈,看到了么" & vbNullChar
.dwInfoFlags = NIIF_INFO
End With
Call Shell_NotifyIcon(NIM_ADD, nfIconData)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
End
End Sub
本程序代码使用Vb6 + Win2000调试通过