在托盘上显示Balloon Tooltip(VB.NET)

类别:.NET开发 点击:0 评论:0 推荐:

使用VB编写在托盘上显示Balloon提示的功能就需要调用Win32Api了,但是VB6和VB.NET代码上还是有些不同的,红色部分即为二者区别

Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form


#Region "Win32Api_NotifyIcon"
    Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias _
         "Shell_NotifyIconA" (ByVal dwMessage As Int32, _
    ByRef lpData As NOTIFYICONDATA) As Int32

    <System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> Private Structure NOTIFYICONDATA
        Public cbSize As Int32
        Public hwnd As IntPtr
        Public uId As Int32
        Public uFlags As Int32
        Public uCallBackMessage As Int32
        Public hIcon As IntPtr
        <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=128)> _
                Public szTip As String
        Public dwState As Int32
        Public dwStateMask As Int32
        <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=256)> _
        Public szInfo As String
        Public uTimeoutOrVersion As Int32
        <MarshalAs(UnmanagedType.ByValTStr, sizeconst:=64)> _
        Public szInfoTitle As String
        Public dwInfoFlags As Int32
    End Structure

    Private Const NOTIFYICON_VERSION = 3
    Private Const NOTIFYICON_OLDVERSION = 0

    Private Const NIM_ADD = &H0
    Private Const NIM_MODIFY = &H1
    Private Const NIM_DELETE = &H2

    Private Const NIM_SETFOCUS = &H3
    Private Const NIM_SETVERSION = &H4

    Private Const NIF_MESSAGE = &H1
    Private Const NIF_ICON = &H2
    Private Const NIF_TIP = &H4

    Private Const NIF_STATE = &H8
    Private Const NIF_INFO = &H10

    Private Const NIS_HIDDEN = &H1
    Private Const NIS_SHAREDICON = &H2


    Private Const NIIF_NONE = &H0
    Private Const NIIF_WARNING = &H2
    Private Const NIIF_ERROR = &H3
    Private Const NIIF_INFO = &H1

    Private nfIconData As NOTIFYICONDATA

#End Region


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        nfIconData = New NOTIFYICONDATA()
        With nfIconData
            .cbSize = System.Runtime.InteropServices.Marshal.SizeOf(nfIconData)
            .hwnd = Me.Handle
            .uId = vbNull
            .uFlags = NIF_INFO Or NIF_ICON Or NIF_TIP Or NIF_MESSAGE
            .hIcon = Me.Icon.Handle
            .szTip = "123" '& Microsoft.VisualBasic.vbNullChar
            .dwState = 0
            .dwStateMask = 0
            .szInfo = "Balloon Style Tool-tip" & Chr(13) & "哈哈" '& vbNullChar
            .uTimeoutOrVersion = 15000
            .szInfoTitle = "看到了么?" '& Microsoft.VisualBasic.vbNullChar
            .dwInfoFlags = NIIF_INFO
        End With


        Call Shell_NotifyIcon(NIM_ADD, nfIconData)

    End Sub

    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
    End Sub
End Class

本文地址:http://com.8s8s.com/it/it45419.htm