代码之家  ›  专栏  ›  技术社区  ›  BlueRaja - Danny Pflughoeft

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

  •  1
  • BlueRaja - Danny Pflughoeft  · 技术社区  · 16 年前

    这不起作用:

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

    工具提示一出现,就会将焦点从窗体上夺走,引发鼠标悬停。然后工具提示隐藏,指针再次位于标签上方,调用MouseMove。这将导致出现一个断断续续、闪烁的工具提示。

    有什么办法吗?

    2 回复  |  直到 16 年前
        1
  •  0
  •   BlueRaja - Danny Pflughoeft    16 年前
    toolTip1.Show(_toolTipText, this, new Point(lblRevisionQuestion.Left + e.X + 1, lblRevisionQuestion.Top + e.Y + 1), int.MaxValue);
    

    奇怪的是,当我尝试显示它的一些任意坐标早期,它有同样的问题如上所述。我不知道为什么这行得通,那不行。

        2
  •  0
  •   Patrick D'Souza ob1    13 年前

    由于您使用的是列表视图,因此我想提醒您,listview项具有一些特定于工具提示的属性,如 工具脚本

    toolTip1.ToolTipTitle = string.Format("Item: {0}",e.Item.Text);
    toolTip1.Show(e.Item.ToolTipText,listView1);
    toolTip1.ShowAlways = true;
    
        3
  •  0
  •   Vali Maties    6 年前

    我就是这样做的:

    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