C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-09-2009, 01:42 PM   #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 ?
khdani is offline   Reply With Quote
Old 09-10-2009, 10:33 AM   #2
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
Use a standard ToolTip but override the painting so it can appear as you want it, while still behaving like a normal tool tip.
theoobe is offline   Reply With Quote
Old 09-10-2009, 10:57 AM   #3
Registered User
 
Join Date: Mar 2009
Location: england
Posts: 76
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 11:06 AM.
theoobe is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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