编程语言
首页 > 编程语言> > c# – 将工具提示显示为MouseHover文本

c# – 将工具提示显示为MouseHover文本

作者:互联网

与此相关q:
Displaying tooltip on mouse hover of a text

如何在RichTextBox中的关键字上执行相同的操作,而不是链接.

样品:

在RichTextBox中输入:

你好世界,你好吗?

我为工具提示设置了悬停“世界”这个词,当它悬停时,工具提示会出现“这就是世界”!

但如果它悬停在RichTextBox或“World”这个词中,工具提示将会消失.

希望你能帮帮我.谢谢你.更多力量和GodBless!

解决方法:

这有点简单.您必须使用RichTextBox的GetCharIndexFromPosition方法获取Mouse指针下的Char索引,制作一些简单的循环来查找整个单词,然后通常在Tooltip弹出窗口中显示它.这是代码:

    string punctuations = " ,.;!?'\")]}\n";
    //This saves your words with their corresponding definitions/details
    Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
    ToolTip tt = new ToolTip();
    int k;
    int lineBreakIndex = 60;
    int textHeight;
    //MouseMove event handler for your richTextBox1
    private void richTextBox1_MouseMove(object sender, MouseEventArgs e){
        if (richTextBox1.TextLength == 0) return;
        Point lastCharPoint = richTextBox1.GetPositionFromCharIndex(richTextBox1.TextLength - 1);
        if (e.Y > textHeight ||  (e.Y  >= lastCharPoint.Y && e.X > lastCharPoint.X + textHeight - lastCharPoint.Y))            
        {
            tt.Hide(richTextBox1);
            k = -1;
            return;
        } 
        int i = richTextBox1.GetCharIndexFromPosition(e.Location);            
        int m = i, n = i;
        while (m>-1&&!punctuations.Contains(richTextBox1.Text[m])) m--;            
        m++;
        while (n<richTextBox1.TextLength&&!punctuations.Contains(richTextBox1.Text[n])) n++;
        if (n > m){
            string word = richTextBox1.Text.Substring(m, n - m);
            if (dict.ContainsKey(word)){
                if (k != m){
                    tt.ToolTipTitle = word;
                    tt.Show(dict[word], richTextBox1, e.X, e.Y + 10);
                    k = m;
                }
            }
            else{
                tt.Hide(richTextBox1);
                k = -1;
            }
        }
    }
    //This will get the entry text with lines broken.
    private string GetEntryText(string key){
        string s = dict[key];
        int lastLineEnd = lineBreakIndex;
        for (int i = lastLineEnd; i < s.Length; i += lineBreakIndex)
        {
            while (s[i] != ' '){
                if (--i < 0) break;                    
            }
            i++;
            s = s.Insert(i, "\n");
            lastLineEnd = i+1;
        }
        return s;
    }
    //MouseLeave event handler for your richTextBox1
    private void richTextBox1_MouseLeave(object sender, EventArgs e){
        tt.Hide(richTextBox1);
        k = -1;
    }
    //ContentsResized event handler for your richTextBox1
    private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)
    {
        textHeight = e.NewRectangle.Height;
    }
    //Here are some sample words with definitions:
    dict.Add("world", "- World is a common name for the whole of human civilization, specifically human experience, history, or the human condition in general, worldwide, i.e. anywhere on Earth.");
    dict.Add("geek", "- A person who is single-minded or accomplished in scientific or technical pursuits but is felt to be socially inept");

标签:c,winforms,tooltip,mousehover
来源: https://codeday.me/bug/20190629/1325746.html