Förstår jag dig rätt att ditt problem är att det inte fungerar när du vill använda AddPacket(). Isf var kallar du på denna? För att kolla flödet kan du prova att skriva ut till konsollen.
Hej jag har en class med balloon-tip för VB.NET!
Den funkar alldeles utmärkt att köra när jag t.ex. klickat på en knapp:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ShowBallon(Me.Text, "hej")
End Sub
Dock skall jag använda den här funkar det inte (exakt samma):
Private Sub AddPacket(ByVal pPacket As StructPackets)
ShowBallon(Me.Text, "hej")
End Sub
Här är klassen för balloon-tipen:
Imports System.Runtime.InteropServices
Public Class NotifyIconEx
<StructLayout(LayoutKind.Sequential)> Public Structure NotifyIconData
Public cbSize As UInt32
Public hWnd As IntPtr
Public uID As UInt32
Public uFlags As NotifyFlags
Public uCallbackMessage As UInt32
Public hIcon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> Public szTip As String
Public dwState As UInt32
Public dwStateMask As UInt32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> Public szInfo As String
Public uTimeoutOrVersion As UInt32
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> Public szInfoTitle As String
Public dwInfoFlags As UInt32
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
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)
If Buffer.ToString.StartsWith("WindowsForms10.Window.0.app") Then
m_notifyWindow = hWnd
m_foundNotifyWindow = True
Return 0
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)
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
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
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
Notera att jag skapat en egen funktion för ShowBallon som ser ut såhär:
Function ShowBallon(ByVal strTitle As String, ByVal strText As String, Optional ByVal intTimeOut As Integer = 5000)
Dim trayIcon As New NotifyIconEx
trayIcon.ShowBalloon(1, strText, strTitle, intTimeOut)
End Function
Men jag vet inte om det inte funkar eftersom det går för snabbt när allt skickas men det tror jag inte för jag har testat med threading.thread.sleep mm. Har det nått med att ingen system.object eller eventargs finns? notera att det måste vara Visual Basic.NET
