Imports System.Runtime.InteropServices
Public Class NotifyIconEx
<StructLayout(LayoutKind.Sequential)> _
Public Structure NotifyIconData
Public cbSize As UInt32 'DWORD
Public hWnd As IntPtr 'HWND
Public uID As UInt32 'UINT
Public uFlags As NotifyFlags 'UINT
Public uCallbackMessage As UInt32 'UINT
Public hIcon As IntPtr 'HICON
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
Public szTip As String 'char[128]
Public dwState As UInt32 'DWORD
Public dwStateMask As UInt32 'DWORD
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
Public szInfo As String 'char[256]
Public uTimeoutOrVersion As UInt32 'UINT
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> _
Public szInfoTitle As String 'char[64]
Public dwInfoFlags As UInt32 'DWORD
'GUID guidItem; > IE 6
End Structure
Public Enum NotifyCommand
Add = 0
Modify = 1
Delete = 2
SetFocus = 3
SetVersion = 4
End Enum
Public Enum NotifyFlags
Message = 1
Icon = 2
Tip = 4
State = 8
Info = 16
Guid = 32
End Enum
<DllImport("shell32.Dll")> _
Public Shared Function Shell_NotifyIcon(ByVal cmd As NotifyCommand, ByRef data As NotifyIconData) As Int32
End Function
<DllImport("Kernel32.Dll")> _
Public Shared Function GetCurrentThreadId() As UInt32
End Function
Public Delegate Function EnumThreadWndProc(ByVal hWnd As IntPtr, ByVal lParam As UInt32) As Int32
<DllImport("user32.dll")> _
Public Shared Function EnumThreadWindows(ByVal threadId As UInt32, ByVal callback As EnumThreadWndProc, ByVal param As UInt32) As Int32
End Function
<DllImport("user32.dll")> _
Public Shared Function GetClassName(ByVal hWnd As IntPtr, ByVal className As System.Text.StringBuilder, ByVal maxCount As Int32) As Int32
End Function
Private m_notifyWindow As IntPtr
Private m_foundNotifyWindow As Boolean
'Win32 Callback Function
Private Function FindNotifyWindowCallback(ByVal hWnd As IntPtr, ByVal lParam As UInt32) As Int32
Dim Buffer As New System.Text.StringBuilder(256)
GetClassName(hWnd, Buffer, Buffer.Capacity)
'but what if this changes? - anybody got a better idea?
'the C# code specified "WindowsForms10.Window.0.app1"
'but it seems in vb its "WindowsForms10.Window.0.app61"
'so just in case I just test for "WindowsForms10.Window.0.app"
If Buffer.ToString.StartsWith("WindowsForms10.Window.0.app") Then
m_notifyWindow = hWnd
m_foundNotifyWindow = True
Return 0 'stop searching
End If
Return 1
End Function
Public Sub ShowBalloon(ByVal iconId As UInt32, ByVal title As String, ByVal text As String, ByVal timeout As UInt32)
'find notify window
Dim threadId As UInt32 = GetCurrentThreadId()
Dim cb As New EnumThreadWndProc(AddressOf FindNotifyWindowCallback)
m_foundNotifyWindow = False
EnumThreadWindows(threadId, cb, Convert.ToUInt32(0))
If m_foundNotifyWindow Then
'show the balloon
Dim data As New NotifyIconData()
data.cbSize = Convert.ToUInt32(System.Runtime.InteropServices.Marshal.SizeOf(GetType(NotifyIconData)))
data.hWnd = m_notifyWindow
data.uID = iconId
data.uFlags = NotifyFlags.Info
data.uTimeoutOrVersion = Convert.ToUInt32(15000)
data.szInfo = text
data.szInfoTitle = title
Shell_NotifyIcon(NotifyCommand.Modify, data)
End If
End Sub
'overload to handle int to uint32 conversion
Public Sub ShowBalloon(ByVal iconId As Integer, ByVal title As String, ByVal text As String, ByVal timeout As Integer)
ShowBalloon(Convert.ToUInt32(iconId), title, text, Convert.ToUInt32(timeout))
End Sub
End Class