Click here to Skip to main content
15,884,661 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I've created a balloon tool tip using the following class

C#
using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Drawing;


namespace BeliezeTraderConsole.BaloonToolTip
{
    public class BalloonToolTip
    {
        public event EventHandler timerElapsed;
        private System.Timers.Timer timer;
        private int mvarmaxWidth;
        private int mvarBackColor;
        private int mvarForeColor;
        private int mvarVisibleTime;
        private int mvarDelayTime;
        private string mvarTitle;
        private string mvarTipText;
        private TooltipIcon mvarIcon;
        private Styles mvarStyle;
        private BalloonAlignment mvarAlign;
        private bool mvarPopupOnDemand;
        private bool mvarCentered;
        private bool mvarABSPosn;
        private System.Drawing.Font mvarFont;
        private POINTAPI mvarPos;
        private int m_lTTHwnd;
        private int m_lParentHwnd;
        private TOOLINFO ti;

        public int MaxWidth
        {
            get
            {
                return mvarmaxWidth;
            }
            set
            {
                mvarmaxWidth = value;
                if (m_lTTHwnd != 0)
                {
                    SendMessage(m_lTTHwnd, TTM_SETMAXTIPWIDTH, 0, mvarmaxWidth);
                }
            }


        }

        public Color BackColor
        {
            get
            {
                return Color.FromArgb(mvarBackColor);
            }
            set
            {
                mvarBackColor = value.ToArgb();
                mvarBackColor &= 0x00FFFFFF; //We remove the alfa 
                if (m_lTTHwnd != 0)
                {
                    SendMessage(m_lTTHwnd, TTM_SETTIPBKCOLOR, mvarBackColor, IntPtr.Zero);
                }
            }
        }

        public Color ForeColor
        {
            get
            {
                return Color.FromArgb(mvarForeColor);
            }
            set
            {
                mvarForeColor = value.ToArgb();
                mvarForeColor &= 0x00FFFFFF; //We remove the alfa 
                if (m_lTTHwnd != 0)
                {
                    SendMessage(m_lTTHwnd, TTM_SETTIPTEXTCOLOR, mvarForeColor, IntPtr.Zero);
                }
            }
        }

        public int VisibleTime
        {
            get
            {
                return mvarVisibleTime;
            }
            set
            {
                mvarVisibleTime = value;
                if (mvarVisibleTime > 0)
                    timer.Interval = value;
            }
        }

        public int DelayTime
        {
            get
            {
                return mvarDelayTime;
            }
            set
            {
                mvarDelayTime = value;
            }
        }

        public string Title
        {
            get
            {
                return mvarTitle;
            }
            set
            {
                mvarTitle = value;
                if (m_lTTHwnd != 0 && mvarTitle != string.Empty)
                {
                    SendMessage(m_lTTHwnd, TTM_SETTITLE, (int)mvarIcon, Marshal.StringToHGlobalAuto(mvarTitle));
                }
            }
        }

        public string TipText
        {
            get
            {
                return mvarTipText;
            }
            set
            {
                mvarTipText = value;
                ti.lpStr = value;
                if (m_lTTHwnd != 0)
                {
                    IntPtr ptrStruct = Marshal.AllocHGlobal(Marshal.SizeOf(ti));
                    Marshal.StructureToPtr(ti, ptrStruct, true);
                    SendMessage(m_lTTHwnd, TTM_UPDATETIPTEXTA, 0, ptrStruct);
                    Marshal.FreeHGlobal(ptrStruct);
                }
            }
        }

        public TooltipIcon Icon
        {
            get
            {
                return mvarIcon;
            }
            set
            {
                mvarIcon = value;
                if (m_lTTHwnd != 0 && mvarTitle != string.Empty)
                {
                    SendMessage(m_lTTHwnd, TTM_SETTITLE, (int)mvarIcon, Marshal.StringToHGlobalAuto(mvarTitle));
                }
            }
        }

        public Styles Style
        {
            get
            {
                return mvarStyle;
            }
            set
            {
                mvarStyle = value;
            }
        }

        public BalloonAlignment Alignment
        {
            get
            {
                return mvarAlign;
            }
            set
            {
                if (ti.hwnd != 0)
                {
                    Point pt = new Point();
                    GetClientRect(ti.hwnd, ref ti.lprect);
                    ClientToScreen(ti.hwnd, ref ti.lprect);
                    switch (value)
                    {
                        default:
                        case BalloonAlignment.TopLeft:
                            pt.X = ti.lprect.left - 3;
                            pt.Y = ti.lprect.top;
                            break;
                        case BalloonAlignment.TopMiddle:
                            pt.X = ti.lprect.left + (ti.lprect.right / 2);
                            pt.Y = ti.lprect.top;
                            break;
                        case BalloonAlignment.TopRight:
                            pt.X = ti.lprect.left + ti.lprect.right + 3;
                            pt.Y = ti.lprect.top;
                            break;
                        case BalloonAlignment.LeftMiddle:
                            pt.X = ti.lprect.left - 1;
                            pt.Y = ti.lprect.top + (ti.lprect.bottom / 2);
                            break;
                        case BalloonAlignment.RightMiddle:
                            pt.X = ti.lprect.left + ti.lprect.right + 3;
                            pt.Y = ti.lprect.top + (ti.lprect.bottom / 2);
                            break;
                        case BalloonAlignment.BottomLeft:
                            pt.X = ti.lprect.left;
                            pt.Y = ti.lprect.top + ti.lprect.bottom - 3;
                            break;
                        case BalloonAlignment.BottomMiddle:
                            pt.X = ti.lprect.left + (ti.lprect.right / 2);
                            pt.Y = ti.lprect.top + ti.lprect.bottom;
                            break;
                        case BalloonAlignment.BottomRight:
                            pt.X = ti.lprect.left + ti.lprect.right + 3;
                            pt.Y = ti.lprect.top + ti.lprect.bottom;
                            break;
                    }
                    Position = pt;
                    mvarAlign = value; //Do not move this from here.		
                }
            }
        }

        public bool PopupOnDemand
        {
            get
            {
                return mvarPopupOnDemand;
            }
            set
            {
                mvarPopupOnDemand = value;
            }
        }

        public bool Centered
        {
            get
            {
                return mvarCentered;
            }
            set
            {
                mvarCentered = value;
            }
        }

        public bool AbsolutePositioning
        {
            get
            {
                return mvarABSPosn;
            }
            set
            {
                mvarABSPosn = value;
                if (mvarABSPosn)
                    ti.lFlags |= TTF_ABSOLUTE;
                else
                    ti.lFlags ^= TTF_ABSOLUTE;


            }
        }

        public System.Drawing.Font TipFont
        {
            get
            {
                return mvarFont;
            }
            set
            {
                mvarFont = value;
                if (m_lTTHwnd != 0)
                {
                    SendMessage(m_lTTHwnd, WM_SETFONT, mvarFont.ToHfont().ToInt32(), 1);
                }
            }
        }

        public Point Position
        {
            get
            {
                return new Point(mvarPos.X, mvarPos.Y);
            }
            set
            {
                mvarPos = new POINTAPI(value.X, value.Y);
                mvarAlign = BalloonAlignment.Custom;
                //TTM_TRACKPOSITION Message needs the coordinates like this
                SendMessage(m_lTTHwnd, TTM_TRACKPOSITION, 0, MAKELONG(mvarPos.X, mvarPos.Y));
            }
        }

        internal const int WM_USER = 0x0400;
        internal const long CW_USEDEFAULT = 0x80000000;
        internal const int WM_SETFONT = 0x30;
        internal const int SWP_NOSIZE = 0x0001;
        internal const int SWP_NOMOVE = 0x0002;
        internal const int SWP_NOACTIVATE = 0x0010;
        internal const int SWP_NOZORDER = 0x0004;
        internal const int TTS_ALWAYSTIP = 0x01;
        internal const int TTS_NOPREFIX = 0x02;
        internal const int TTS_BALLOON = 0x40;
        internal const int TTS_CLOSE = 0x80;
        internal const int TTM_SETMAXTIPWIDTH = WM_USER + 24;
        internal const int TTM_ADDTOOL = WM_USER + 50;
        internal const int TTM_SETTITLE = WM_USER + 33;
        internal const int TTM_ACTIVATE = WM_USER + 1;
        internal const int TTM_UPDATETIPTEXTA = WM_USER + 12;
        internal const int TTM_SETDELAYTIME = WM_USER + 3;
        internal const int TTM_SETTOOLINFO = WM_USER + 54; //54
        internal const int TTM_TRACKACTIVATE = WM_USER + 17;
        internal const int TTM_TRACKPOSITION = WM_USER + 18;
        internal const int TTM_SETTIPBKCOLOR = WM_USER + 19;
        internal const int TTM_SETTIPTEXTCOLOR = WM_USER + 20;
        internal const int TTM_UPDATE = WM_USER + 29;
        internal const int TTF_IDISHWND = 0x0001;
        internal const int TTF_SUBCLASS = 0x0010;
        internal const int TTF_TRACK = 0x0020;
        internal const int TTF_ABSOLUTE = 0x0080;
        internal const int TTF_TRANSPARENT = 0x0100;
        internal const int TTF_CENTERTIP = 0x0002;
        internal const int TTF_PARSELINKS = 0x1000;
        internal const int TTF_RTLREADING = 0x04;
        internal const int TTDT_AUTOMATIC = 0;
        internal const int TTDT_RESHOW = 1;
        internal const int TTDT_AUTOPOP = 2;
        internal const int TTDT_INITIAL = 3;
        internal const long WS_POPUP = 0x80000000;
        internal const string TOOLTIPS_CLASSA = "tooltips_class32";
        public enum TooltipIcon
        {
            None,
            Info,
            Warning,
            Error
        }

        public enum Styles
        {
            TTStandard, //Shows a square window
            TTBalloon,  //Shows the balloon as you know it.
        }

        public enum BalloonAlignment
        {
            TopLeft,
            TopMiddle,
            TopRight,
            LeftMiddle,
            RightMiddle,
            BottomLeft,
            BottomMiddle,
            BottomRight,
            Custom
        }

        [DllImport("User32", SetLastError = true)]
        internal static extern int GetClientRect(
            int hWnd,
            ref RECT lpRect);

        [DllImport("User32", SetLastError = true)]
        internal static extern int ClientToScreen(
            int hWnd,
            ref POINTAPI lpPoint);

        [DllImport("User32", SetLastError = true)]
        private static extern int ClientToScreen(
            int hWnd,
            ref RECT lpRect);

        [DllImport("User32", SetLastError = true)]
        internal static extern int SendMessage(
            int hWnd,
            int Msg,
            int wParam,
            IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        internal static extern int SendMessage(
            int hWnd,
            int Msg,
            int wParam,
            int lParam);

        [DllImport("user32.dll", SetLastError = true)]
        internal static extern int SendMessage(
            int hWnd,
            int Msg,
            int wParam,
            TOOLINFO lParam);

        [DllImport("User32", SetLastError = true)]
        internal static extern int CreateWindowEx(
            int dwExStyle,
            string lpClassName,
            string lpWindowName,
            int dwStyle,
            int X,
            int Y,
            int nWidth,
            int nHeight,
            int hWndParent,
            int hMenu,
            int hInstance,
            IntPtr lpParam);


        [DllImport("User32", SetLastError = true)]
        internal static extern int DestroyWindow(int hwnd);

        [DllImport("User32", SetLastError = true)]
        static extern int MoveWindow(
            int hwnd,
            int x,
            int y,
            int nWidth,
            int nHeight,
            int bRepaint);

        [StructLayout(LayoutKind.Sequential)]
        public struct TOOLINFO
        {
            public int lSize;
            public int lFlags;
            public int hwnd;
            public int lId;
            public RECT lprect;
            public int hInstance;
            public string lpStr;
            public int lParam;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct POINTAPI
        {
            public int X;
            public int Y;

            public POINTAPI(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }

        public BalloonToolTip()
        {
            timer = new System.Timers.Timer();
            timer.AutoReset = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            mvarPopupOnDemand = true;
            mvarStyle = BalloonToolTip.Styles.TTBalloon;
        }


        ~BalloonToolTip()
        {
            Destroy();
        }

        public void CreateToolTip(int ParentHwnd)
        {
            int ret;
            int lWinStyle = 0;
            if (m_lTTHwnd != 0)
                DestroyWindow(m_lTTHwnd);

            m_lParentHwnd = ParentHwnd;
            if (mvarStyle == Styles.TTBalloon)
                lWinStyle |= TTS_BALLOON | TTS_CLOSE;

            //creates the balloon
            m_lTTHwnd = CreateWindowEx(0, TOOLTIPS_CLASSA, string.Empty,
                lWinStyle, 0, 0, 0, 0, m_lParentHwnd, 0, 0, IntPtr.Zero);

            //now set our tooltip info structure
            if (mvarCentered)
            {
                if (!mvarPopupOnDemand)
                    ti.lFlags = TTF_IDISHWND | TTF_CENTERTIP | TTF_SUBCLASS;
                else
                    ti.lFlags = TTF_IDISHWND | TTF_CENTERTIP | TTF_TRACK;
            }
            else
            {
                if (!mvarPopupOnDemand)
                    ti.lFlags = TTF_IDISHWND | TTF_SUBCLASS;
                else
                    ti.lFlags = TTF_IDISHWND | TTF_TRACK | TTF_TRANSPARENT;
            }

            if (mvarABSPosn)
            {
                ti.lFlags |= (TTF_ABSOLUTE);
            }

            //set the hwnd prop to our parent control's hwnd
            ti.hwnd = m_lParentHwnd;
            ti.lId = m_lParentHwnd; //0
            ti.hInstance = 0;
            //ti.lpstr = ALREADY SET;
            //ti.lpRect = lpRect;
            ti.lSize = Marshal.SizeOf(ti);

            IntPtr ptrStruct = Marshal.AllocHGlobal(Marshal.SizeOf(ti));
            Marshal.StructureToPtr(ti, ptrStruct, true);
            ret = SendMessage(m_lTTHwnd, TTM_ADDTOOL, 0, ptrStruct);

            Marshal.FreeHGlobal(ptrStruct);
            Title = mvarTitle;
            TipText = mvarTipText;
            if (mvarmaxWidth != 0) MaxWidth = mvarmaxWidth;
            if (mvarBackColor != 0) BackColor = Color.FromArgb(mvarBackColor);
            if (mvarForeColor != 0) ForeColor = Color.FromArgb(mvarForeColor);
            if (mvarVisibleTime != 0) VisibleTime = mvarVisibleTime;
            if (mvarDelayTime != 0) DelayTime = mvarDelayTime;
            if (mvarFont != null) TipFont = mvarFont;
        }

        public void Show(BalloonAlignment align)
        {
            Alignment = align;
            Show();
        }

        public void Show()
        {            
            System.Threading.Thread.Sleep(mvarDelayTime);
            IntPtr ptrStruct = Marshal.AllocHGlobal(Marshal.SizeOf(ti));
            Marshal.StructureToPtr(ti, ptrStruct, true);
            SendMessage(m_lTTHwnd, TTM_TRACKACTIVATE, 1, ptrStruct);
            Marshal.FreeHGlobal(ptrStruct);
            if (mvarVisibleTime > 0 && !timer.Enabled)
            {//If this method is called several times during a small period of time, 
                //this action will interpret it and reset the timer and start the count again.
                timer.Interval += 1;
                timer.Interval -= 1;
                timer.Start();
            }
        }

        public void Show(int x, int y)
        {
            mvarAlign = BalloonAlignment.Custom;
            Position = new Point(x, y);
            Show();
        }

        public void Move(int x, int y)
        {
            RECT size = new RECT();
            mvarAlign = BalloonAlignment.Custom;
            GetClientRect(this.m_lTTHwnd, ref size);
            MoveWindow(this.m_lTTHwnd, x, y, size.right, size.bottom, 1);
        }

        public void Hide()
        {
            IntPtr ptrStruct = Marshal.AllocHGlobal(Marshal.SizeOf(ti));
            Marshal.StructureToPtr(ti, ptrStruct, true);
            SendMessage(m_lTTHwnd, TTM_TRACKACTIVATE, 0, ptrStruct);
            Marshal.FreeHGlobal(ptrStruct);
        }

        public void Refresh()
        {
            SendMessage(m_lTTHwnd, TTM_UPDATE, 0, 0);
        }

        public void Destroy()
        {
            if (m_lTTHwnd != 0)
                DestroyWindow(m_lTTHwnd);
        }


        private int MAKELONG(int loWord, int hiWord)
        { //USED to set the position in the screen (TTM_TRACKPOSITION Message)
            return (hiWord << 16) | (loWord & 0xffff);
        }


        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (timerElapsed != null)
            {
                timerElapsed(this, EventArgs.Empty);
            }
        }
    }
}

my problem is, This Balloon tool tip is not showing in windows 7 and windows vista
Posted
Updated 9-Dec-11 5:33am
v3
Comments
Orcun Iyigun 9-Dec-11 11:34am    
Tags are added.

1 solution

Is it possible that you have the EnableBalloonTips registry key set to 0?

See http://www.howtogeek.com/howto/windows-vista/disable-all-notification-balloons-in-windows-vista/ for details on disabling balloon tooltips.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900