显示工具提示的文本鼠标悬停 [英] Displaying tooltip on mouse hover of a text

查看:316
本文介绍了显示工具提示的文本鼠标悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示工具提示,当鼠标悬停在链接在我的自定义丰富的编辑控制权。考虑下面的文字:

  

我们所有的睡眠在夜间进行。

在我的情况下,这个词的睡眠是链接。

当用户链接下移动鼠标,在这种情况下,休眠,我想显示一个提示的链接。

下面就来我的脑海里,但他们没有工作

1)补漏OnMouseHover

 如果(this.Cursor == Cursors.Hand)
   tooltip.Show(文本框,我的工具提示);
其他
   tooltip.Hide(文本);
 

但是,这并不工作了。

更新

中提到的链接的不可以网址,即这些自定义链接,所以正则表达式将不会有很大帮助在这里,它可以是任何文字。用户可以选择创建它的链接。

虽然我没有试过为getPosition 的方法,我不认为这将是优雅的设计和维护方面。

让我说,我有以下行,在我的richedit中

  

我们的睡眠在夜间进行。但蝙蝠留下的清醒。蟑螂变的有效在夜间进行。

在上面这句话,我想三个不同的工具提示,当鼠标悬停在他们。

 睡眠 - >人类
清醒 - >守夜这里
主动 - >我的一天开始
 

我被困的OnMouseMove 如下:

Working-有消息框

 的OnMouseMove()
{

   //检查是否光标在一个链接
   //虽然这是不正确的做法,我很担心,为什么没有一个工具提示显示出来
   如果(this.Cursor.current == Cursors.hand)
   {
     的MessageBox.show(你是下一个链接);
   }
}
 

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

 的OnMouseMove(MouseventArgs E)
{

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

解决方案

好了,一起来看看,这个工程,如果您有问题,请告诉我:

 使用System.Drawing中;
使用System.Windows.Forms的;
命名空间WindowsFormsApplication1
{
    公共部分类Form1中:形态
    {
        公共Form1中(){的InitializeComponent(); }

        工具提示尖=新的工具提示();
        无效richTextBox1_MouseMove(对象发件人,发送MouseEventArgs E)
        {
            如果(!timer1.Enabled)
            {
                字符串链接=朗朗上口(richTextBox1.Text,richTextBox1.GetCharIndexFromPosition(e.Location));
                //检查当前单词是否保险业监督的URL,更改为任何你想要的正则表达式,我发现它www.regexlib.com。
//你也可以检查,如果当前字是粗体,下划线等,但我并没有深入进去。
                如果(System.Text.RegularEx pressions.Regex.IsMatch(链接, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$"))
                {
                    tip.ToolTipTitle =链接;
                    点P = richTextBox1.Location;
                    tip.Show(链接,这一点,p.X + e.X,
                        PY + EY + 32,//你可以把它(35)更改为提示的高度 - 控制工具提示的位置。
                        1000);
                    timer1.Enabled = TRUE;
                }
            }
        }

        私人无效timer1_Tick(对象发件人,EventArgs五)//定时器是控制工具提示,它不应该在每次鼠标移动重绘。
        {
            timer1.Enabled = FALSE;
        }

        公共静态字符串朗朗上口(字符串输入,INT位置)//提取整个单词鼠标目前主要关注。
        {
            焦炭S =输入[位置]
            INT SP1 = 0,SP2 = input.Length;
            的for(int i =位置; I> 0;我 - )
            {
                焦炭CH =输入[I]
                如果(CH ==''|| CH =='\ N')
                {
                    SP1 =我;
                    打破;
                }
            }

            的for(int i =位置; I< input.Length;我++)
            {
                焦炭CH =输入[I]
                如果(CH ==''|| CH =='\ N')
                {
                    SP2 =我;
                    打破;
                }
            }

            返回input.Substring(SP1,SP2  -  SP1).Replace(\ N,);
        }
    }
}
 

I want to display a tooltip when the mouse hovers over a link in my custom rich edit control. Consider the following text:

We all sleep at night .

In my case the word sleep is a link.

When the user moves the mouse under the link, in this case "sleep", I want to display a tooltip for the link.

The following came to my mind, but they are not working

1) Trapping OnMouseHover

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

But this does not work out.

UPDATE

The links mentioned are not URLs, i.e these are custom links, so Regex won't be of much help here, it can be any text. The user can choose to create it a a link.

Though I have not tried GetPosition method, I dont think it would be that elegant in terms of design and maintenance.

Let me say I have the following line, in my richedit box

We sleep at night. But the bats stay awake. Cockroaches become active at night.

In the above sentence, I want three different tooltips, when the mouse hovers over them.

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

I trapped OnMouseMove as follows:

Working- with Messagebox

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

Not Working - with Tooltip - Tooltip does not show up

OnMouseMove( MouseventArgs e )
{

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

解决方案

Well, take a look, this works, If you have problems please tell me:

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

这篇关于显示工具提示的文本鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆