富文本框内的链接?

Ozt*_*aco 22 c# rtf richtextbox hyperlink winforms

我知道richtextboxes可以检测链接(比如http://www.yahoo.com),但有没有办法让我添加看起来像文本但链接的链接?在哪里可以选择链接的标签?例如,它显示为点击此处转到雅虎而不是它出现在http://www.yahoo.com

编辑:忘了,我正在使用Windows窗体

编辑:是否有更好的使用(如更容易格式化)?

Mar*_*aiß 9

当然可以通过在控件中调用一些WIN32功能,但是如果你正在寻找一些标准方法,请查看这篇文章: 在TextBox控件中创建超链接

关于不同的整合方式有一些讨论.

问候

更新1:最好的方法是遵循这种方法:http: //msdn.microsoft.com/en-us/library/f591a55w.aspx

因为RichText框控件为"DetectUrls"提供了一些功能.然后,您可以非常轻松地处理点击的链接:

this.richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox1_LinkClicked);
Run Code Online (Sandbox Code Playgroud)

您可以通过扩展基类来简单地创建自己的RichTextBox控件 - 您可以覆盖所需的方法,例如DetectUrls.

  • 问题是关于显示_带有友好标签_的链接,该链接_隐藏底层 URL_,就像答案中呈现的 Markdown 链接一样。您链接到的第一篇文章没有有用的答案,第二篇文章仅显示如何创建链接_以原始 URL 作为标签_。 (2认同)

Nim*_*ush 8

在这里,您可以找到通过linkLabel在富文本框中添加链接的示例:

    LinkLabel link = new LinkLabel();
    link.Text = "something";
    link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
    LinkLabel.Link data = new LinkLabel.Link();
    data.LinkData = @"C:\";
    link.Links.Add(data);
    link.AutoSize = true;
    link.Location =
        this.richTextBox1.GetPositionFromCharIndex(this.richTextBox1.TextLength);
    this.richTextBox1.Controls.Add(link);
    this.richTextBox1.AppendText(link.Text + "   ");
    this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
Run Code Online (Sandbox Code Playgroud)

这是处理程序:

    private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
    }
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢示例代码,我设法使其即使在滚动时也能工作。我放置 `link.Tag = link.Location;` 来备份链接位置,然后将其位置替换为 `Point p = (Point)l.Tag;` 和 `l.Top = richTextBox1.GetPositionFromCharIndex(0).Y + pY;` 在 `richTextBox1_VScroll` 和 `richTextBox1_HScroll` 上。 (2认同)

Gui*_*ici 6

我找到了一种可能不是最优雅的方法,但只需几行代码即可完成工作。即,其想法是通过字体变化来模拟超链接的外观,并通过检测鼠标指针所在的位置来模拟超链接的行为。

代码:

public partial class Form1 : Form
{
    private Cursor defaultRichTextBoxCursor = Cursors.Default;
    private const string HOT_TEXT = "click here";
    private bool mouseOnHotText = false;

    // ... Lines skipped (constructor, etc.)

    private void Form1_Load(object sender, EventArgs e)
    {
        // save the right cursor for later
        this.defaultRichTextBoxCursor = richTextBox1.Cursor;

        // Output some sample text, some of which contains
        // the trigger string (HOT_TEXT)
        richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Underline);
        richTextBox1.SelectionColor = Color.Blue;
        // output "click here" with blue underlined font
        richTextBox1.SelectedText = HOT_TEXT + "\n";

        richTextBox1.SelectionFont = new Font("Calibri", 11, FontStyle.Regular);
        richTextBox1.SelectionColor = Color.Black;
        richTextBox1.SelectedText = "Some regular text";
    }

    private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
    {
        int mousePointerCharIndex = richTextBox1.GetCharIndexFromPosition(e.Location);
        int mousePointerLine = richTextBox1.GetLineFromCharIndex(mousePointerCharIndex);
        int firstCharIndexInMousePointerLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine);
        int firstCharIndexInNextLine = richTextBox1.GetFirstCharIndexFromLine(mousePointerLine + 1);
        if (firstCharIndexInNextLine < 0)
        {
            firstCharIndexInNextLine = richTextBox1.Text.Length;
        }

        // See where the hyperlink starts, as long as it's on the same line
        // over which the mouse is
        int hotTextStartIndex = richTextBox1.Find(
            HOT_TEXT, firstCharIndexInMousePointerLine, firstCharIndexInNextLine, RichTextBoxFinds.NoHighlight);

        if (hotTextStartIndex >= 0 && 
            mousePointerCharIndex >= hotTextStartIndex && mousePointerCharIndex < hotTextStartIndex + HOT_TEXT.Length)
        {
            // Simulate hyperlink behavior
            richTextBox1.Cursor = Cursors.Hand;
            mouseOnHotText = true;
        }
        else
        {
            richTextBox1.Cursor = defaultRichTextBoxCursor;
            mouseOnHotText = false;
        }
        toolStripStatusLabel1.Text = mousePointerCharIndex.ToString();
    }

    private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && mouseOnHotText)
        {
            // Insert your own URL here, to navigate to when "hot text" is clicked
            Process.Start("http://www.google.com");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为了改进代码,可以创建一种优雅的方式将多个“热文本”字符串映射到它们自己的链接 URL(也许Dictionary<K, V>)。另一个改进是子类化RichTextBox以封装上面代码中的功能。