富文本框内的链接?

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

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

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

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

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

问候

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

因为RichText框控件为“DetectUrls”提供了一些function。 然后,您可以非常轻松地处理点击的链接:

this.richTextBox1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox1_LinkClicked); 

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

在这里,您可以找到通过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; 

这是处理程序:

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

标准的RichTextBox控件(假设您使用的是Windows窗体)公开了一组相当有限的function,所以很遗憾,您需要执行一些Win32互操作来实现这一点(沿着SendMessage(),CFM_LINK,EM_SETCHARFORMAT等行)。

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

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

代码:

 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 )。 另一个改进是将RichTextBox子类RichTextBox封装上面代码中的function。