PDA

Click to See Complete Forum and Search --> : Balloon Tip C# to .NET


veryjonny
Aug 27th, 2002, 06:50 AM
After much searching I have found the code to display balloon tips (XP style) But unforunately its in c# ...would some one convert it into VB.NET...Thanks
I have attached it as well

public class NotifyIcon
{
[StructLayout(LayoutKind.Sequential)]
public struct NotifyIconData
{
public System.UInt32 cbSize; // DWORD
public System.IntPtr hWnd; // HWND
public System.UInt32 uID; // UINT
public NotifyFlags uFlags; // UINT
public System.UInt32 uCallbackMessage; // UINT
public System.IntPtr hIcon; // HICON
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public System.String szTip; // char[128]
public System.UInt32 dwState; // DWORD
public System.UInt32 dwStateMask; // DWORD
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
public System.String szInfo; // char[256]
public System.UInt32 uTimeoutOrVersion; // UINT
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public System.String szInfoTitle; // char[64]
public System.UInt32 dwInfoFlags; // DWORD
//GUID guidItem; > IE 6
}

public enum NotifyCommand {Add = 0, Modify = 1, Delete = 2, SetFocus = 3,
SetVersion = 4}
public enum NotifyFlags {Message = 1, Icon = 2, Tip = 4, State = 8, Info = 16,
Guid = 32}

[DllImport("shell32.Dll")]
public static extern System.Int32 Shell_NotifyIcon(NotifyCommand cmd,
ref NotifyIconData data);

[DllImport("Kernel32.Dll")]
public static extern System.UInt32 GetCurrentThreadId();

public delegate System.Int32 EnumThreadWndProc(System.IntPtr hWnd,
System.UInt32 lParam);

[DllImport("user32.Dll")]
public static extern System.Int32 EnumThreadWindows(System.UInt32 threadId,
EnumThreadWndProc callback,
System.UInt32 param);

[DllImport("user32.Dll")]
public static extern System.Int32 GetClassName(System.IntPtr hWnd,
System.Text.StringBuilder className,
System.Int32 maxCount);

private System.IntPtr m_notifyWindow;
private bool m_foundNotifyWindow;

// Win32 Callback Function
private System.Int32 FindNotifyWindowCallback(System.IntPtr hWnd,
System.UInt32 lParam)
{
System.Text.StringBuilder buffer = new System.Text.StringBuilder(256);
GetClassName(hWnd, buffer, buffer.Capacity);

// but what if this changes? - anybody got a better idea?
if(buffer.ToString() == "WindowsForms10.Window.0.app1")
{
m_notifyWindow = hWnd;
m_foundNotifyWindow = true;
return 0; // stop searching
}
return 1;
}

public void ShowBalloon(uint iconId, string title, string text, uint timeout)
{
// find notify window
uint threadId = GetCurrentThreadId();
EnumThreadWndProc cb = new EnumThreadWndProc(FindNotifyWindowCallback);
m_foundNotifyWindow = false;
EnumThreadWindows(threadId, cb, 0);
if(m_foundNotifyWindow)
{
// show the balloon
NotifyIconData data = new NotifyIconData();
data.cbSize = (System.UInt32)
System.Runtime.InteropServices.Marshal.SizeOf(
typeof(NotifyIconData));
data.hWnd = m_notifyWindow;
data.uID = iconId;
data.uFlags = NotifyFlags.Info;
data.uTimeoutOrVersion = 15000;
data.szInfo = text;
data.szInfoTitle = title;
Shell_NotifyIcon(NotifyCommand.Modify, ref data);
}
}
}
Here is the code that shows the usage of the class.

private void OnShowBalloon(object sender, System.EventArgs e)
{
NotifyBalloonDemo.NotifyIcon notifyIcon = new NotifyBalloonDemo.NotifyIcon();
notifyIcon.ShowBalloon(1, "My Title", "My Text", 15000);
}

cim3
Aug 28th, 2002, 04:31 AM
Think I found something more simple along these lines. If you add Imports Microsoft.Office.Core
Then there is balloon object which might be of help. DOn't know how it works though!

veryjonny
Aug 28th, 2002, 03:08 PM
thanks for the post.

I tried with it but cant seem to get it.

Though using Microsoft agent I can show a balloon (ie making the agent speak), but just the balloon, VB says something about interfaces which I yet to understand.

hellswraith
Aug 28th, 2002, 04:41 PM
You don't need to convert it to VB, just use the class as you would any VB.NET class. Make the call to the ShowBallon method, passing in the correct arguments, and it will work.
Just save the class in C#, then add the class to your project. This is the beauty of the language interop in .NET.

veryjonny
Aug 29th, 2002, 06:31 AM
But, Will it work when I have not installed C# on my machine?

Anyways, I will it a try and post back:)

Edneeis
Dec 19th, 2002, 11:38 AM
Here is the VB Version:

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


And here is a sample project.

Cputerace
Dec 28th, 2002, 10:19 AM
I have an app that i am trying to integrate this with.

1) how would i go about making the balloon show from an existing notifyicon

2) What happens if this is called in a windows 98 environment? will it cause an error, or simply not show. What is the best way of determining what OS version the program is running in?

Thanks

-Mike

Edneeis
Dec 29th, 2002, 07:23 PM
1) You wouldn't. You'd change your declarations of the normal NotifyIcon to the new one NotifyIconEx.

2) You'll have to try it on a 98 machine to find out but as for figuringout which OS you are in I believe there is an Environmental variable for that.

Cputerace
Dec 31st, 2002, 05:01 PM
Edneeis:
I have been trying to implement this code in my project, but it doesnt want to work. I think it has something to do with the fact that i am using multiple threads, and the thread that i am calling this from is not the one that contains the form that contains the notifyicon. How do i specify what notifyicon i want the ballook to show up on manually?

DevGrp
Dec 31st, 2002, 06:30 PM
Here check this out. http://www.codeproject.com/useritems/balloonwindow.asp

Cputerace
Jan 1st, 2003, 11:56 AM
I fixed the threadig problem by simply invoking the method on the form that has the notuifyicon.

I now have another question in reference to the vb version edneeis posted...

In windows xp/2k, when the system uses the balloons, it knows when you click on the balloon, because it will allow an action to occur when you click on it (i.e. adding a new hardware if it fails, it says click this balloon to see hardware). I would like to integrate this into my program, both capturing a click on the close button and a click on the actual window.

Thanks

-Mike