【问题标题】:Tooltip only shows after moving the mouse工具提示仅在移动鼠标后显示
【发布时间】:2014-10-11 20:13:05
【问题描述】:

我在使用 mousemove 事件显示工具提示时遇到了一些麻烦。基本上,当我的鼠标指针位于图片框的某些区域上时,我想显示一个工具提示。我正在尝试使用 mousemove 事件来完成此操作,确定指针是否位于最佳位置并(如果是)使用 settooltip 设置工具提示。

这是我的 mousemove 事件(因为我注意到在显示工具提示时会连续触发 mousemove,我检查位置是否真的改变了)

private void pbFaseFlow_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Location != OldPosition)
    {
        OldPosition = e.Location;

        // determine text for tooltip (gets loaded into a global string)
        DetermineText(Position);

        // show the coords and tooltip text for debugging in some textbox
        tbInfo.Text = e.X.ToString() + " " + e.Y.ToString() + " " + ToolTipText;

        // show tooltip
        if (ToolTipText != string.Empty)
        {
            toolTip.SetToolTip(pbFaseFlow, ToolTipText);
            toolTip.Active = true;
        }
        else
        {
            toolTip.Active = false;
        }
    }
}

这一切正常,除了当我将鼠标移动到第一个像素的最佳位置时。在我的文本框中,我可以看到正在确定文本(比如“测试”fe),但工具提示没有显示。只有在我将鼠标再移动 1 个像素后,才会显示工具提示。这是一个问题,因为在甜蜜点上可能只有 1 个像素宽,所以当将鼠标从左向右移动时,工具提示不会显示(它会在向上/向下移动时显示......)

即使我没有检查真正改变的位置(我省略了 e.location 检查),在我将鼠标再移动 1 个像素之前,工具提示也不会显示。

我注意到,如果我从不让工具提示处于非活动状态,它确实有效,但我不希望在甜蜜点之外显示任何内容..

这里发生了什么?

---------编辑--------------

更多信息:

当我将代码更改为此(基本上总是显示工具提示,除了现在没有信息时只有一个空格),工具提示会立即在最佳位置更新。缺点是我现在有一个空的工具提示,当没有数据要显示时,总是显示,很烦人。

            // show tooltip
            if (ToolTipText != string.Empty)
            {
                toolTip.Active = true;
                toolTip.SetToolTip(pbFaseFlow, ToolTipText);
            }
            else
            {
                toolTip.Active = true;
                ToolTipText = " ";
                toolTip.SetToolTip(pbFaseFlow, " ");
            }

【问题讨论】:

  • 那么如果你在这两行中切换顺序会发生什么toolTip.SetToolTip(pbFaseFlow, ToolTipText); toolTip.Active = true;
  • 我在您的帖子中添加了winforms 标签。如果有误,请edit发帖。
  • 没有变化,我已经试过了:-)
  • 基本上,我感觉我的事件现在正在设置新的工具提示,但我需要做一些事情来触发工具提示显示(在这种情况下,将鼠标移动另一个像素)。有点像用 .invalidate 强制重绘画框,但现在是工具提示...我还尝试向 tooltip_popup 事件添加一些调试代码。当我在第一个像素中调用 settooltip 时,永远不会触发弹出事件。

标签: c# winforms tooltip


【解决方案1】:

我有一个非常相似的问题。我使用TrackMouseEvent() 重新启用MouseHover,因此我不必将鼠标移出控件并重新移入以获取另一个悬停事件来触发工具提示。

第一次正常工作,但随后的尝试显示工具提示直到鼠标移动 1 个像素后才会发生。

我的解决方法是拨打Show() 两次。我还需要从鼠标位置稍微偏移工具提示。

toolTip1.Show("Hi There", Parent, e.X + 5, e.Y + 5, 2000);
toolTip1.Show("Hi there", Parent, e.X + 5, e.Y + 5, 2000);

【讨论】:

    【解决方案2】:

    看起来您缺少 toolTip.Show() 方法。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private Point OldPosition = new Point(0, 0);
            private Point ptSweetSpot = new Point(30, 30);
            private List<Point> sweetPointArea = new List<Point>() 
                { new Point(60, 60), new Point(60, 61), 
                  new Point(61, 60), new Point(61, 61)
                };
    
            public Form1()
            {
                InitializeComponent();
    
                pbFaseFlow.MouseMove += pbFaseFlow_MouseMove;
    
                //just to see it on pictureBox
                Bitmap myBitmap = new Bitmap(pbFaseFlow.Height, pbFaseFlow.Width);
                myBitmap.SetPixel(ptSweetSpot.X, ptSweetSpot.Y, Color.Red);
    
                foreach (Point p in sweetPointArea)
                {
                   myBitmap.SetPixel(p.X, p.Y, Color.Green);
                }
    
                pbFaseFlow.Image = myBitmap;
            }
            private void pbFaseFlow_MouseMove(object sender, MouseEventArgs e)
            {
                if (!e.Location.Equals(OldPosition))
                {
                    OldPosition = e.Location;
    
                    // show the coords and tooltip text for debugging in some textbox
                    tbInfo.Text = e.X.ToString() + " " + e.Y.ToString();
    
                    //are we inside sweet area?
                    if (sweetPointArea.Contains(e.Location))
                    {
                        toolTip.Active = true;
                        toolTip.SetToolTip(pbFaseFlow, "hello from sweet area!");
                        toolTip.Show(toolTip.GetToolTip(pbFaseFlow), pbFaseFlow, pbFaseFlow.Width / 2, pbFaseFlow.Height / 2);
                    }
                    //no? so maybe we're over sweet spot?
                    else if (e.Location.Equals(ptSweetSpot)) 
                    {
                        toolTip.Active = true;
                        toolTip.SetToolTip(pbFaseFlow, "hello from sweet point!");
                        toolTip.Show(toolTip.GetToolTip(pbFaseFlow), pbFaseFlow, pbFaseFlow.Width / 2, pbFaseFlow.Height / 2);                }
                    //no? ok, so disable tooltip
                    else
                    {
                        toolTip.Active = false;
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 现在可以了!我之前尝试添加 Tooltip.Show,但看起来我错过了工具提示的位置。现在我只需要找出工具提示显示的正确位置!像 e.X, e.Y + cursor.size.height/2 之类的东西,但不知何故,它偏离了 2 个像素..
    • 任何人都可以告诉我,无论你是否给予职位,这有什么不同?还有工具提示的“标准”位置是什么,所以我可以确保我的工具提示处于正常位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多