在RichTextBox的自定义链接 [英] Custom links in RichTextBox

查看:172
本文介绍了在RichTextBox的自定义链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想每一个字开头的来产生一个双击事件。为此,我已实现了以下测试code:

Suppose I want every word starting with a # to generate an event on double click. For this I have implemented the following test code:

private bool IsChannel(Point position, out int start, out int end)
{
    if (richTextBox1.Text.Length == 0)
    {
        start = end = -1;
        return false;
    }

    int index = richTextBox1.GetCharIndexFromPosition(position);
    int stop = index;

    while (index >= 0 && richTextBox1.Text[index] != '#')
    {
        if (richTextBox1.Text[index] == ' ')
        {
            break;
        }
        --index;
    }

    if (index < 0 || richTextBox1.Text[index] != '#')
    {
        start = end = -1;
        return false;
    }

    while (stop < richTextBox1.Text.Length && richTextBox1.Text[stop] != ' ')
    {
        ++stop;
    }
    --stop;

    start = index;
    end = stop;

    return true;
}

private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
    textBox1.Text = richTextBox1.GetCharIndexFromPosition(new Point(e.X, e.Y)).ToString();
    int d1, d2;
    if (IsChannel(new Point(e.X, e.Y), out d1, out d2) == true)
    {
        if (richTextBox1.Cursor != Cursors.Hand)
        {
            richTextBox1.Cursor = Cursors.Hand;
        }
    }
    else
    {
        richTextBox1.Cursor = Cursors.Arrow;
    }
}

该处理检测与启动,使鼠标光标手,当它盘旋在他们的话。不过,我有以下两个问题:

This handles detecting words that start with # and making the mouse cursor a hand when it hovers over them. However, I have the following two problems:

  1. 如果我尝试实现双击事件 richTextBox1 ,我可以检测到一个词被点击时,但这个词被高亮显示(选择),我会想避免。我可以以编程方式取消它通过选择文本的结尾,但由此导致的闪烁,这是我想避免的。什么方法是有做到这一点?
  2. GetCharIndexFromPosition 方法返回的性格最接近光标的指标。这意味着,如果我的RichTextBox中包含的唯一的事情是开始了那么光标将是一只手无论从哪富文本控件是一个字。我怎样才能让这个只有一只手,当鼠标悬停在某个实际的词或字符是一个字,我很感兴趣的部分?所实现的URL检测也部分患有此问题。如果我能够检测URL,而只写 www.test.com 中的富文本编辑器,光标将是一只手,只要它是在或以下的链接。这不会是一个手,如果它是向然而链路的右侧。我很好即使有这样的功能,如果使光标一只手,当且仅当它是在文本被证明是太困难了。
  1. If I try to implement a double click event for richTextBox1, I can detect when a word is clicked, however that word is highlighted (selected), which I'd like to avoid. I can deselect it programmatically by selecting the end of the text, but that causes a flicker, which I would like to avoid. What ways are there to do this?
  2. The GetCharIndexFromPosition method returns the index of the character that is closest to the cursor. This means that if the only thing my RichTextBox contains is a word starting with a # then the cursor will be a hand no matter where on the rich text control it is. How can I make it so that it is only a hand when it hovers over an actual word or character that is part of a word I'm interested in? The implemented URL detection also partially suffers from this problem. If I enable detection of URLs and only write www.test.com in the rich text editor, the cursor will be a hand as long as it is on or below the link. It will not be a hand if it's to the right of the link however. I'm fine even with this functionality if making the cursor a hand if and only if it's on the text proves to be too difficult.

我猜我将不得不诉诸某种形式的Windows API调用,但我真的不知道从哪里开始。

I'm guessing I'll have to resort to some sort of Windows API calls, but I don't really know where to start.

我使用Visual Studio 2008和我想实现这个自己。

I am using Visual Studio 2008 and I would like to implement this myself.

更新: 闪烁的问题将得到解决,如果我可以让这个没有文字可以选择通过双击,只能通过拖动鼠标光标和编程。这是更容易实现?因为我真的不关心,如果人能在这种情况下,选择文本或不通过双击。

Update: The flickering problem would be solved if I could make it so that no text is selectable through double clicking, only through dragging the mouse cursor and programmatically. Is this easier to achieve? Because I don't really care if one can select text or not by double clicking in this case.

推荐答案

在点(2)你可以尝试:

On point (2) you could try:

如果(richTextBox1.Text.Length == 0){...}

After if (richTextBox1.Text.Length == 0){ ... }

//get the mouse point in client coordinates
Point clientPoint = richTextBox1.PointToClient(richTextBox1.PointToScreen(position));
int index = richTextBox1.GetCharIndexFromPosition(position);
//get the position of the closest char
Point charPoint = richTextBox1.GetPositionFromCharIndex(index);

bool notOnTheSameLine = ((clientPoint.Y < charPoint.Y) || (clientPoint.Y > charPoint.Y + richTextBox1.Font.Height));
bool passedTheWord = (clientPoint.X > charPoint.X + richTextBox1.Font.SizeInPoints);

if (notOnTheSameLine || passedTheWord)
{
  start = end = -1;
  return false;
}


有关问题(1)可能有以下比DBL单击该链接以不同的方式?也许CNTL点击将避免这个词的问题变得选定...


For point (1) maybe have a different way of following the link than dbl-click? Maybe cntl-click would avoid the issues of the word becoming selected...

这篇关于在RichTextBox的自定义链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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