Custom ToolTip

This is a discussion on Custom ToolTip within the C# Programming forums, part of the General Programming Boards category; Hello, I've created a form to be used as a more informative tooltip, whenever the mouse hovers over a ToolBar ...

  1. #1
    Registered User khdani's Avatar
    Join Date
    Oct 2007
    Posts
    42

    Custom ToolTip

    Hello,
    I've created a form to be used as a more informative tooltip, whenever
    the mouse hovers over a ToolBar Item it should be shown.
    The problem is, as soon as the tooltip form is shown, the toolbar is closed immediately,
    and hence the tooltip.
    how can i prevent from the toolbar to be closed ?

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Use a standard ToolTip but override the painting so it can appear as you want it, while still behaving like a normal tool tip.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    I've been having a play with the idea and come up with a little demo to explain what I mean. OK first I made this class which is based on a standard inherited ToolTip:

    Code:
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        class CustomToolTip : ToolTip
        {
            public CustomToolTip()
            {
                this.OwnerDraw = true;
                this.Popup += new PopupEventHandler(this.OnPopup);
                this.Draw += new DrawToolTipEventHandler(this.OnDraw);
            }
    
            private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip
            {
                e.ToolTipSize = new Size(200, 100);
            }
    
            private void OnDraw(object sender, DrawToolTipEventArgs e) // use this event to customise the tool tip
            {
                Graphics g = e.Graphics;
    
                LinearGradientBrush b = new LinearGradientBrush(e.Bounds,
                    Color.GreenYellow, Color.MintCream, 45f);
    
                g.FillRectangle(b, e.Bounds);
    
                g.DrawRectangle(new Pen(Brushes.Red, 1), new Rectangle(e.Bounds.X, e.Bounds.Y,
                    e.Bounds.Width - 1, e.Bounds.Height - 1));
    
                g.DrawString(e.ToolTipText, new Font(e.Font, FontStyle.Bold), Brushes.Silver,
                    new PointF(e.Bounds.X + 6, e.Bounds.Y + 6)); // shadow layer
                g.DrawString(e.ToolTipText, new Font(e.Font, FontStyle.Bold), Brushes.Black,
                    new PointF(e.Bounds.X + 5, e.Bounds.Y + 5)); // top layer
    
                b.Dispose();
            }
        }
    }
    In the Draw event, you can see I've added a gradient background, a red border, and some double layered text. You can change all that to whatever you want.

    Then for demonstration, create a button on your form and add a Hover event:

    Code:
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private CustomToolTip tip;
    
            public Form1()
            {
                InitializeComponent();
                
                this.tip = new CustomToolTip();
            }
    
            private void button1_MouseHover(object sender, EventArgs e)
            {
                this.tip.SetToolTip(this.button1, "this is a test");
            }
        }
    }
    Run the program, and let your mouse rest on top of the button. As you can see, it's as simple as using a standard ToolTip.
    Last edited by theoobe; 09-10-2009 at 12:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom Allocation
    By SevenThunders in forum C Programming
    Replies: 17
    Last Post: 04-09-2007, 05:10 PM
  2. FindWindowEx() fails with ToolTip class
    By @nthony in forum Windows Programming
    Replies: 3
    Last Post: 01-16-2007, 03:43 AM
  3. Stl sets and custom classes.
    By cunnus88 in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 12:58 AM
  4. Tooltip won't display over toolbar button
    By minesweeper in forum Windows Programming
    Replies: 5
    Last Post: 06-20-2003, 02:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 10:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21