Tray Icon NotiFy Balloon Problem

A

Andrea V.F.

I use the code below (VB.NET) to display a Popup balloon in the Tray
Icon of my application.

The balloon is displayed, but the timeout never happens and the balloon
is always visible even if I set it. Where is the error?? Please Help
me. Thanks.

Code:
Imports System.Runtime.InteropServices
Public Class ClsNotifyBalloon


<StructLayout(LayoutKind.Sequential)> _
Public Structure NOTIFYICONDATA
Public cbSize As Integer
Public hwnd As IntPtr
Public uID As Integer
Public uFlags As Integer
Public uCallbackMessage As Integer
Public hIcon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
Public szTip As String
Public dwState As Integer
Public dwStateMask As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
Public szInfo As String
Public uTimeout As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> _
Public szInfoTitle As String
Public dwInfoFlags As Integer
End Structure

<DllImport("Shell32")> _
Private Shared Function Shell_NotifyIconA(ByVal Msg As Integer,
ByRef Nd As NOTIFYICONDATA) As IntPtr
End Function

Public Enum ToolTipIcon
TTI_INFO = 1
TTI_WARNING = 2
TTI_ERROR = 3
End Enum

Public Sub DisplayBalloon(ByVal Caption As String, ByVal strText As
String, ByVal Ni As NotifyIcon, ByVal ico As ToolTipIcon, Optional
ByVal timeout As Integer = 1000)
Dim notifystruct As NOTIFYICONDATA
Dim nWindow As NativeWindow =
DirectCast(Ni.GetType.GetField("window",
Reflection.BindingFlags.NonPublic Or
Reflection.BindingFlags.Instance).GetValue(Ni), NativeWindow)

With notifystruct
.cbSize = 0
.dwInfoFlags = 0
.dwState = 0
.dwStateMask = 0
.hIcon = New IntPtr(0)
.szTip = String.Empty
.uCallbackMessage = &H200
.szInfoTitle = Caption
.uTimeout = timeout
.hwnd = nWindow.Handle
.uID = Convert.ToInt32(Ni.GetType.GetField("id",
Reflection.BindingFlags.NonPublic Or
Reflection.BindingFlags.Instance).GetValue(Ni))
.dwInfoFlags = Convert.ToInt32(ico)
.uTimeout = Environment.OSVersion.Version.Major
.szInfo = strText
.uFlags = &H10
.cbSize = Marshal.SizeOf(notifystruct)
Shell_NotifyIconA(&H1, notifystruct)
End With

End Sub

End Class
 
M

m.posseth

well i use this one


Option Explicit On

Option Strict On

Imports System.Runtime.InteropServices

Public Class Balloon

<StructLayout(LayoutKind.Sequential)> _

Public Structure NOTIFYICONDATA

Public cbSize As Int32

Public hwnd As IntPtr

Public uID As Int32

Public uFlags As Int32

Public uCallbackMessage As IntPtr

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 uTimeout As Int32

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> _

Public szInfoTitle As String

Public dwInfoFlags As Int32

End Structure

Public Const NIF_MESSAGE As Int32 = &H1

Public Const NIF_ICON As Int32 = &H2

Public Const NIF_STATE As Int32 = &H8

Public Const NIF_INFO As Int32 = &H10

Public Const NIF_TIP As Int32 = &H4

Public Const NIM_ADD As Int32 = &H0

Public Const NIM_MODIFY As Int32 = &H1

Public Const NIM_DELETE As Int32 = &H2

Public Const NIM_SETVERSION As Int32 = &H4

Public Const NOTIFYICON_VERSION As Int32 = &H5

Public Const NIIF_ERROR As Int32 = &H3

Public Const NIIF_INFO As Int32 = &H1

Public Const NIIF_NONE As Int32 = &H0

Public Const NIM_SETFOCUS As Int32 = &H3

Public Enum BalloonMessageType

None = &H0

Info = &H1

[Error] = &H3

End Enum

Shared Sub NotifyBalloon(ByRef ntfyIcon As NotifyIcon, ByVal Title As
String, ByVal Info As String, ByVal Type As BalloonMessageType, ByVal
Timeout As Integer)

Dim t As Type = GetType(NotifyIcon)

Dim window As IntPtr = (CType(t.GetField("window",
System.Reflection.BindingFlags.Instance Or
System.Reflection.BindingFlags.NonPublic).GetValue(ntfyIcon),
NativeWindow)).Handle

Dim id As Int32 = CType(t.GetField("id",
System.Reflection.BindingFlags.Instance Or
System.Reflection.BindingFlags.NonPublic).GetValue(ntfyIcon), Integer)

Dim uNIF As NOTIFYICONDATA

uNIF.cbSize = 0

uNIF.dwInfoFlags = 0

uNIF.dwState = 0

uNIF.dwStateMask = 0

uNIF.hIcon = IntPtr.Zero

uNIF.szTip = ""

uNIF.uCallbackMessage = New IntPtr(&H200)

uNIF.szInfoTitle = Title

uNIF.uTimeout = Timeout

uNIF.hwnd = window

uNIF.uID = id

uNIF.dwInfoFlags = CType(Type, Int32)

uNIF.uTimeout = NOTIFYICON_VERSION

uNIF.szInfo = Info

uNIF.uFlags = NIF_INFO

uNIF.cbSize = Marshal.SizeOf(uNIF)

Dim result As Int32 = Shell_NotifyIconA(NIM_MODIFY, uNIF)

End Sub

Private Declare Function Shell_NotifyIconA Lib "shell32" (ByVal dwMessage As
Int32, ByRef pnid As NOTIFYICONDATA) As Int32

End Class



and on windows 2003 i didn`t see anny strange behavior

regards



M. posseth [MCP]



Andrea V.F. said:
I use the code below (VB.NET) to display a Popup balloon in the Tray
Icon of my application.

The balloon is displayed, but the timeout never happens and the balloon
is always visible even if I set it. Where is the error?? Please Help
me. Thanks.

Code:
Imports System.Runtime.InteropServices
Public Class ClsNotifyBalloon


<StructLayout(LayoutKind.Sequential)> _
Public Structure NOTIFYICONDATA
Public cbSize As Integer
Public hwnd As IntPtr
Public uID As Integer
Public uFlags As Integer
Public uCallbackMessage As Integer
Public hIcon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
Public szTip As String
Public dwState As Integer
Public dwStateMask As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
Public szInfo As String
Public uTimeout As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> _
Public szInfoTitle As String
Public dwInfoFlags As Integer
End Structure

<DllImport("Shell32")> _
Private Shared Function Shell_NotifyIconA(ByVal Msg As Integer,
ByRef Nd As NOTIFYICONDATA) As IntPtr
End Function

Public Enum ToolTipIcon
TTI_INFO = 1
TTI_WARNING = 2
TTI_ERROR = 3
End Enum

Public Sub DisplayBalloon(ByVal Caption As String, ByVal strText As
String, ByVal Ni As NotifyIcon, ByVal ico As ToolTipIcon, Optional
ByVal timeout As Integer = 1000)
Dim notifystruct As NOTIFYICONDATA
Dim nWindow As NativeWindow =
DirectCast(Ni.GetType.GetField("window",
Reflection.BindingFlags.NonPublic Or
Reflection.BindingFlags.Instance).GetValue(Ni), NativeWindow)

With notifystruct
.cbSize = 0
.dwInfoFlags = 0
.dwState = 0
.dwStateMask = 0
.hIcon = New IntPtr(0)
.szTip = String.Empty
.uCallbackMessage = &H200
.szInfoTitle = Caption
.uTimeout = timeout
.hwnd = nWindow.Handle
.uID = Convert.ToInt32(Ni.GetType.GetField("id",
Reflection.BindingFlags.NonPublic Or
Reflection.BindingFlags.Instance).GetValue(Ni))
.dwInfoFlags = Convert.ToInt32(ico)
.uTimeout = Environment.OSVersion.Version.Major
.szInfo = strText
.uFlags = &H10
.cbSize = Marshal.SizeOf(notifystruct)
Shell_NotifyIconA(&H1, notifystruct)
End With

End Sub

End Class
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top