如何使用鼠标移动工具提示(winforms)

问题描述 投票:1回答:3

我希望它在鼠标移动时移动,而在指针不在标签上时消失。

这不起作用:

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);
}

工具提示一出现,它就会从窗体窃取焦点,从而引起MouseLeave。然后,工具提示将隐藏,并且指针再次位于标签上方,从而调用MouseMove。这将导致不稳定的闪烁工具提示。

有什么办法吗?

c# winforms tooltip mouseevent
3个回答
0
投票
toolTip1.Show(_toolTipText, this, new Point(lblRevisionQuestion.Left + e.X + 1, lblRevisionQuestion.Top + e.Y + 1), int.MaxValue);

奇怪的是,当我尝试将其显示在任意坐标系上时,它具有与上述相同的问题。我不知道为什么这样行不通。


0
投票

由于您使用的是列表视图,所以我想提醒您,列表视图项具有一些工具提示特定的属性,例如ToolTipText。当您将鼠标悬停在一个项目上时,这将使显示数据更加容易,如下所示

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

0
投票

这是我的工作方式:

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);
            }
        }
    }

}

在班级某处使用它:

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;
    }
}

结果是:

FloatingToolTip

© www.soinside.com 2019 - 2024. All rights reserved.