Results 1 to 11 of 11

Thread: Balloon Tip C# to .NET

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    Balloon Tip C# to .NET

    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
    Code:
    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);
    }
    Attached Files Attached Files

  2. #2
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    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!
    Wind and waves resolves all problems.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    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.

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    But, Will it work when I have not installed C# on my machine?

    Anyways, I will it a try and post back

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is the VB Version:
    VB Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class NotifyIconEx
    4.  
    5.     <StructLayout(LayoutKind.Sequential)> _
    6.     Public Structure NotifyIconData
    7.         Public cbSize As UInt32  'DWORD
    8.         Public hWnd As IntPtr  'HWND
    9.         Public uID As UInt32  'UINT
    10.         Public uFlags As NotifyFlags 'UINT
    11.         Public uCallbackMessage As UInt32  'UINT
    12.         Public hIcon As IntPtr  'HICON
    13.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
    14.         Public szTip As String  'char[128]
    15.         Public dwState As UInt32  'DWORD
    16.         Public dwStateMask As UInt32 'DWORD
    17.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
    18.         Public szInfo As String  'char[256]
    19.         Public uTimeoutOrVersion As UInt32  'UINT
    20.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=64)> _
    21.         Public szInfoTitle As String  'char[64]
    22.         Public dwInfoFlags As UInt32  'DWORD
    23.         'GUID guidItem; > IE 6
    24.     End Structure
    25.  
    26.     Public Enum NotifyCommand
    27.         Add = 0
    28.         Modify = 1
    29.         Delete = 2
    30.         SetFocus = 3
    31.         SetVersion = 4
    32.     End Enum
    33.  
    34.  
    35.     Public Enum NotifyFlags
    36.         Message = 1
    37.         Icon = 2
    38.         Tip = 4
    39.         State = 8
    40.         Info = 16
    41.         Guid = 32
    42.     End Enum
    43.  
    44.     <DllImport("shell32.Dll")> _
    45.     Public Shared Function Shell_NotifyIcon(ByVal cmd As NotifyCommand, ByRef data As NotifyIconData) As Int32
    46.     End Function
    47.  
    48.     <DllImport("Kernel32.Dll")> _
    49.     Public Shared Function GetCurrentThreadId() As UInt32
    50.     End Function
    51.  
    52.     Public Delegate Function EnumThreadWndProc(ByVal hWnd As IntPtr, ByVal lParam As UInt32) As Int32
    53.  
    54.     <DllImport("user32.dll")> _
    55.     Public Shared Function EnumThreadWindows(ByVal threadId As UInt32, ByVal callback As EnumThreadWndProc, ByVal param As UInt32) As Int32
    56.     End Function
    57.  
    58.     <DllImport("user32.dll")> _
    59.     Public Shared Function GetClassName(ByVal hWnd As IntPtr, ByVal className As System.Text.StringBuilder, ByVal maxCount As Int32) As Int32
    60.     End Function
    61.  
    62.     Private m_notifyWindow As IntPtr
    63.     Private m_foundNotifyWindow As Boolean
    64.  
    65.     'Win32 Callback Function
    66.     Private Function FindNotifyWindowCallback(ByVal hWnd As IntPtr, ByVal lParam As UInt32) As Int32
    67.         Dim Buffer As New System.Text.StringBuilder(256)
    68.         GetClassName(hWnd, Buffer, Buffer.Capacity)
    69.  
    70.         'but what if this changes?  - anybody got a better idea?
    71.         'the C# code specified "WindowsForms10.Window.0.app1"
    72.         'but it seems in vb its "WindowsForms10.Window.0.app61"
    73.         'so just in case I just test for "WindowsForms10.Window.0.app"
    74.         If Buffer.ToString.StartsWith("WindowsForms10.Window.0.app") Then
    75.             m_notifyWindow = hWnd
    76.             m_foundNotifyWindow = True
    77.             Return 0 'stop searching
    78.         End If
    79.         Return 1
    80.     End Function
    81.  
    82.     Public Sub ShowBalloon(ByVal iconId As UInt32, ByVal title As String, ByVal text As String, ByVal timeout As UInt32)
    83.         'find notify window
    84.         Dim threadId As UInt32 = GetCurrentThreadId()
    85.         Dim cb As New EnumThreadWndProc(AddressOf FindNotifyWindowCallback)
    86.         m_foundNotifyWindow = False
    87.         EnumThreadWindows(threadId, cb, Convert.ToUInt32(0))
    88.         If m_foundNotifyWindow Then
    89.             'show the balloon
    90.             Dim data As New NotifyIconData()
    91.             data.cbSize = Convert.ToUInt32(System.Runtime.InteropServices.Marshal.SizeOf(GetType(NotifyIconData)))
    92.             data.hWnd = m_notifyWindow
    93.             data.uID = iconId
    94.             data.uFlags = NotifyFlags.Info
    95.             data.uTimeoutOrVersion = Convert.ToUInt32(15000)
    96.             data.szInfo = text
    97.             data.szInfoTitle = title
    98.             Shell_NotifyIcon(NotifyCommand.Modify, data)
    99.         End If
    100.  
    101.     End Sub
    102.  
    103.     'overload to handle int to uint32 conversion
    104.     Public Sub ShowBalloon(ByVal iconId As Integer, ByVal title As String, ByVal text As String, ByVal timeout As Integer)
    105.         ShowBalloon(Convert.ToUInt32(iconId), title, text, Convert.ToUInt32(timeout))
    106.     End Sub
    107.  
    108. End Class

    And here is a sample project.

  7. #7
    New Member
    Join Date
    Dec 2002
    Posts
    3

    Modification for an existing notifyicon

    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

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  9. #9
    New Member
    Join Date
    Dec 2002
    Posts
    3

    Not able to properly integrate this into my code

    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?

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Dont gain the world and lose your soul

  11. #11
    New Member
    Join Date
    Dec 2002
    Posts
    3

    Events on closing/clicking on balloon

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width