开发者

How to make tooltip move with mouse (winforms)

开发者 https://www.devze.com 2022-12-26 23:18 出处:网络
I want it to move when the mouse moves, and disappear when the pointer isn\'t over the label. This doesn\'t work:

I want it to move when the mouse moves, and disappear when the pointer isn't over the label.

This doesn't work:

private void lblRevisionQuestion_MouseMove(object sender, MouseEventArgs e)
{
    toolTip1.Show("test", this, PointToClient(MousePosition), Int32.MaxValue);
}

private void lblRevisionQuestion_MouseLeave(object sender, EventArgs e)
{
    toolTip1.Hide(this);
}

As soon as the tooltip appears, it steals focus away from the form, evoking MouseLeave. Then the tooltip hides, and the pointer is once again over the label, invoking Mou开发者_开发百科seMove. This results in a choppy, flashing tooltip.

Is there any way to do this?


toolTip1.Show(_toolTipText, this, new Point(lblRevisionQuestion.Left + e.X + 1, lblRevisionQuestion.Top + e.Y + 1), int.MaxValue);

Oddly enough, when I tried displaying it to some arbitrary coordinates eariler, it had the same problem as above. I don't know why this works and that didn't.


Since your are working with a list view, I would like to bring to your notice that the listview items have some tooltip specific properties like ToolTipText. This will make it easier to display the data when you hover over a item as shown below

toolTip1.ToolTipTitle = string.Format("Item: {0}",e.Item.Text);
toolTip1.Show(e.Item.ToolTipText,listView1);
toolTip1.ShowAlways = true;


This is how I did:

MyToolTip.cs :

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

public class MyToolTip : ToolTip
{
    public MyToolTip()
    {
        OwnerDraw = true;
        Draw += MyToolTip_Draw;
    }

    private void MyToolTip_Draw(object sender, DrawToolTipEventArgs e)
    {
        using (StringFormat sf = new StringFormat())
        {
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
            sf.FormatFlags = StringFormatFlags.NoWrap;
            using (Font f = new Font("Arial", 7.5F))
            {
                SizeF s = new SizeF();
                s = e.Graphics.MeasureString(e.ToolTipText, f);
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(225, 225, 245)), e.Bounds);
                e.DrawBorder();
                e.Graphics.DrawString(e.ToolTipText, f, SystemBrushes.ActiveCaptionText, e.Bounds, sf);
            }
        }
    }

}

Using it somewhere in a class:

private MyToolTip ttp;
private int LastX;
private int LastY;
public string Caption { get; set; }

private void MyObjectWhichNeedsMovingToolTip_MouseLeave(object sender, EventArgs e)
{
    ttp.Hide(this);
}

private void MyObjectWhichNeedsMovingToolTip_MouseMove(object sender, MouseEventArgs e)
{
    // This is for stop flickering tooltip
    if (e.X != this.lastX || e.Y != this.lastY)
    {
        // depending on parent of the object you must add or substract Left and Top information in next line
        ttp.Show(Caption, this, new Point(MyObjectWhichNeedsMovingToolTip.Left + e.X + 10, MyObjectWhichNeedsMovingToolTip.Top + e.Y + 20), int.MaxValue); 

        this.lastX = e.X;
        this.lastY = e.Y;
    }
}

And the result is:

How to make tooltip move with mouse (winforms)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号