富文本框中的链接?

问题描述 投票:0回答:7

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

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

编辑:有没有更好用的东西(比如更容易格式化)?

c# winforms hyperlink richtextbox rtf
7个回答
11
投票

当然可以通过在您的控件中调用一些 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);

您可以通过扩展基类简单地创建您自己的 RichTextBox 控件 - 在那里您可以重写您需要的方法,例如 DetectUrls。


10
投票

在这里您可以找到通过 linkLabel 在 Rich Textbox 中添加链接的示例:

    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;

这是处理程序:

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

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

为了改进代码,可以创建一种优雅的方式将多个“热文本”字符串映射到它们自己的链接 URL(可能是

Dictionary<K, V>
)。另一个改进是子类化
RichTextBox
以封装上面代码中的功能。


1
投票

许多个月后,.NET(核心)的解决方案,至少从.NET 6.0(可能更早)开始 - 对于.NET Framework(其最新和最后一个版本是4.8),您仍然会使用这里需要其他解决方案之一:

.Rtf
属性现在可以识别RTF格式的超链接;例如,以下呈现为:

这是一个真正的 RTF 超链接:示例链接

this.richTextBox1.Rtf = @"{\rtf1 This is a true RTF hyperlink:\line {\field{\*\fldinst HYPERLINK ""https://example.org""}{\fldrslt Example Link}}} }";

0
投票

标准的 RichTextBox 控件(假设您使用的是 Windows 窗体)公开了一组相当有限的功能,因此不幸的是您需要执行一些 Win32 互操作来实现这一点(沿着 SendMessage()、CFM_LINK、EM_SETCHARFORMAT 等)。

您可以在此答案中找到有关如何执行此操作的更多信息。


0
投票

在 .net Framework 6.2 或更高版本中,这将是有效的代码:

this.labRichText.LinkClicked += LabRichText_LinkClicked;

private void LabRichText_LinkClicked(object sender, LinkClickedEventArgs e)
{
    System.Diagnostics.Process.Start(e.LinkText);
}

-1
投票

richtextbox 在几年前从来不需要超链接。将其全部删除。 我在 Windows Forms 应用程序中使用它作为智能终端模拟器,与外部设备目标进行异步串行通信。

删除此处不需要的 ilink。

© www.soinside.com 2019 - 2024. All rights reserved.