【问题标题】:Displaying tooltip on mouse hover of a text在文本的鼠标悬停时显示工具提示
【发布时间】:2010-10-26 18:26:40
【问题描述】:

当鼠标悬停在我的自定义富编辑控件中的链接上时,我想显示一个工具提示。考虑以下文本:

我们都在晚上睡觉

在我的例子中,sleep这个词是一个链接。

当用户在链接下移动鼠标时,在本例中为“睡眠”,我想显示链接的工具提示。

我想到了以下内容,但它们不起作用

1) 捕获 OnMouseHover

if(this.Cursor == Cursors.Hand)
   tooltip.Show(textbox,"My tooltip");
else
   tooltip.Hide(textbox);

但这行不通。

更新

提到的链接是不是 URL,即这些是自定义链接,因此 Regex 在这里不会有太大帮助,它可以是任何文本。用户可以选择创建一个链接。

虽然我没有尝试过GetPosition的方法,但我认为它在设计和维护方面不会那么优雅。

让我说我在我的 Richedit 框中有以下行

我们在晚上睡觉。但是蝙蝠保持清醒。蟑螂在晚上变得活跃

在上面的句子中,当鼠标悬停在它们上方时,我想要三个不同的工具提示。

sleep -> Human beings
awake -> Nightwatchman here
active -> My day begins

我将OnMouseMove困如下:

使用消息框

OnMouseMove( )
{

   // check to see if the cursor is over a link
   // though this is not the correct approach, I am worried why does not a tooltip show up
   if(this.Cursor.current == Cursors.hand )
   {
     Messagebox.show("you are under a link");
   }
}

不工作 - 使用工具提示 - 工具提示不显示

OnMouseMove( MouseventArgs e )
{

   if(cursor.current == cursors.hand )
   {
     tooltip.show(richeditbox,e.x,e.y,1000);
   }
}

【问题讨论】:

  • 这成功了.. tooltip.Active = true
  • 哦,我没有意识到你的问题是工具提示本身......无论如何,使用 System.Windows.Forms.Cursor.Current 静态获取当前鼠标光标,就像我上一个答案一样。

标签: c# .net winforms tooltip richtextbox


【解决方案1】:

这并不优雅,但是您也许可以使用 RichTextBox.GetCharIndexFromPosition 方法将鼠标当前所在字符的索引返回给您,然后使用该索引来确定它是否在链接上,热点,或任何其他特殊区域。如果是,请显示您的工具提示(并且您可能希望将鼠标坐标传递给工具提示的 Show 方法,而不是仅传递文本框,以便工具提示可以定位在链接旁边)。

这里的例子: http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.getcharindexfromposition(VS.80).aspx

【讨论】:

  • 我做了以下;仅用于在 Richedit 框的 MouseMove 事件中进行测试。 if(Cursor.Current == Cursors.Hand) Messagebox.Show("My Tooltip); 但是我的工具提示没有显示,一旦我用 tooltip.show() 替换 Messagebox 如下 if(Cursor.Current == Cursors .Hand) this.ttpLink.Show("Hover",txtBox,eX,eY,1000); 我错过了什么吗?
  • Jean,即使我得到文本我怎么知道 atring ia 链接,
【解决方案2】:

好吧,看看,这行得通,如果你有问题请告诉我:

using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }

        ToolTip tip = new ToolTip();
        void richTextBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!timer1.Enabled)
            {
                string link = GetWord(richTextBox1.Text, richTextBox1.GetCharIndexFromPosition(e.Location));
                //Checks whether the current word i a URL, change the regex to whatever you want, I found it on www.regexlib.com.
//you could also check if current word is bold, underlined etc. but I didn't dig into it.
                if (System.Text.RegularExpressions.Regex.IsMatch(link, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$"))
                {
                    tip.ToolTipTitle = link;
                    Point p = richTextBox1.Location;
                    tip.Show(link, this, p.X + e.X,
                        p.Y + e.Y + 32, //You can change it (the 35) to the tooltip's height - controls the tooltips position.
                        1000);
                    timer1.Enabled = true;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e) //The timer is to control the tooltip, it shouldn't redraw on each mouse move.
        {
            timer1.Enabled = false;
        }

        public static string GetWord(string input, int position) //Extracts the whole word the mouse is currently focused on.
        {
            char s = input[position];
            int sp1 = 0, sp2 = input.Length;
            for (int i = position; i > 0; i--)
            {
                char ch = input[i];
                if (ch == ' ' || ch == '\n')
                {
                    sp1 = i;
                    break;
                }
            }

            for (int i = position; i < input.Length; i++)
            {
                char ch = input[i];
                if (ch == ' ' || ch == '\n')
                {
                    sp2 = i;
                    break;
                }
            }

            return input.Substring(sp1, sp2 - sp1).Replace("\n", "");
        }
    }
}

【讨论】:

    【解决方案3】:

    用途:

    ToolTip tip = new ToolTip();
    private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
    {
        Cursor a = System.Windows.Forms.Cursor.Current;
        if (a == Cursors.Hand)
        {
            Point p = richTextBox1.Location;
            tip.Show(
                GetWord(richTextBox1.Text,
                    richTextBox1.GetCharIndexFromPosition(e.Location)),
                this,
                p.X + e.X,
                p.Y + e.Y + 32,
                1000);
        }
    }
    

    使用我其他答案中的 GetWord 函数来获取悬停的单词。 使用计时器逻辑禁用重新显示工具提示,如上一页所示。例子。

    在上面的示例中,工具提示通过检查鼠标指针来显示悬停的单词。

    如果这个答案仍然不是您想要的,请指定您想要使用工具提示的单词的特征条件。 如果你想要粗体字,请告诉我。

    【讨论】:

      【解决方案4】:

      您不应该使用控件私有工具提示,而应使用表单。这个例子效果很好:

      public partial class Form1 : Form
      {
          private System.Windows.Forms.ToolTip toolTip1;
      
          public Form1()
          {
              InitializeComponent();
              this.components = new System.ComponentModel.Container();
              this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
      
              MyRitchTextBox myRTB = new MyRitchTextBox();
              this.Controls.Add(myRTB);
      
              myRTB.Location = new Point(10, 10);
              myRTB.MouseEnter += new EventHandler(myRTB_MouseEnter);
              myRTB.MouseLeave += new EventHandler(myRTB_MouseLeave);
          }
      
      
          void myRTB_MouseEnter(object sender, EventArgs e)
          {
              MyRitchTextBox rtb = (sender as MyRitchTextBox);
              if (rtb != null)
              {
                  this.toolTip1.Show("Hello!!!", rtb);
              }
          }
      
          void myRTB_MouseLeave(object sender, EventArgs e)
          {
              MyRitchTextBox rtb = (sender as MyRitchTextBox);
              if (rtb != null)
              {
                  this.toolTip1.Hide(rtb);
              }
          }
      
      
          public class MyRitchTextBox : RichTextBox
          {
          }
      
      }
      

      【讨论】:

        【解决方案5】:

        我还想在此处添加一些内容,如果您在程序运行之前加载包含工具提示控件的所需表单,则该表单上的工具提示控件将无法按如下所述工作...

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                objfrmmain = new Frm_Main();
                Showtop();//this is procedure in program.cs to load an other form, so if that contain's tool tip control then it will not work
                Application.Run(objfrmmain);
        
        
            }
        

        所以我通过将以下代码放入这样的 Fram_main_load 事件过程中解决了这个问题

            private void Frm_Main_Load(object sender, EventArgs e)
            {
                Program.Showtop();
            }
        

        【讨论】:

          【解决方案6】:

          只需将 ToolTip 工具从工具箱添加到表单中,然后将此代码添加到任何控件的 mousemove 事件中,以使工具提示在其 mousemove 时开始

          private void textBox3_MouseMove(object sender, MouseEventArgs e)
              {
                toolTip1.SetToolTip(textBox3,"Tooltip text"); // you can change the first parameter (textbox3) on any control you wanna focus
              }
          

          希望对你有帮助

          和平

          【讨论】:

          • 设置set toolTip1.SetToolTip(textBox3,"Tooltip text");只有一次,而不是每次引发 mousemove 事件。
          【解决方案7】:

          由于这个问题(但它的年龄)中没有任何内容需要 Windows.Forms 中的解决方案,因此这是在代码隐藏中的 WPF 中执行此操作的一种方法。

          TextBlock tb = new TextBlock();
          tb.Inlines.Add(new Run("Background indicates packet repeat status:"));
          tb.Inlines.Add(new LineBreak());
          tb.Inlines.Add(new LineBreak());
          Run r = new Run("White");
          r.Background = Brushes.White;
          r.ToolTip = "This word has a White background";
          tb.Inlines.Add(r);
          tb.Inlines.Add(new Run("\t= Identical Packet received at this time."));
          tb.Inlines.Add(new LineBreak());
          r = new Run("SkyBlue");
          r.ToolTip = "This word has a SkyBlue background";
          r.Background = new SolidColorBrush(Colors.SkyBlue);
          tb.Inlines.Add(r);
          tb.Inlines.Add(new Run("\t= Original Packet received at this time."));
          
          myControl.Content = tb;
          

          【讨论】:

            【解决方案8】:

            如果您使用的是 RichTextBox 控件。您可以简单地定义 ToolTip 对象,并通过在 RichTextBox 控件内移动鼠标来在选择文本时显示工具提示。

                ToolTip m_ttInput = new ToolTip(); // define as member variable
            
                private void rtbInput_SelectionChanged(object sender, EventArgs e)
                {
                    if (rtbInput.SelectedText.Length > 0) 
                    {
                        m_ttInput.Show(rtbInput.SelectedText.Length.ToString(), rtbInput, 1000);
                    }
                }
            

            【讨论】:

              【解决方案9】:

              为了便于使用和理解。

              您可以简单地将工具提示放在表单上的任何位置(来自工具箱)。然后,您将在表单中其他所有内容的属性中获得一个选项,以确定该工具提示中显示的内容(其内容类似于“ToolTip on toolTip1”)。每当您将鼠标悬停在对象上时,该属性中的文本都会显示为工具提示。

              涵盖像原始问题要求的自定义即时工具提示。但我把这个留给其他不需要的人

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2018-06-02
                • 2011-04-22
                • 2011-09-30
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多