在托盤上顯示Balloon Tooltip

40Star發表於2020-04-04

 

執行這個示例的前提是系統需要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除錯通過

相關文章