2012-03-24 118 views
17

我知道richtextboxes可以檢測鏈接(如http://www.yahoo.com),但有沒有辦法讓我添加鏈接,它看起來像文本,但它的鏈接?像哪裏可以選擇鏈接的標籤?例如,而不是它表現爲http://www.yahoo.com它顯示爲Click here to go to yahoo富文本框內的鏈接?

編輯:忘了,即時通訊使用的Windows窗體

編輯:有什麼是多數民衆贊成更好的使用(如更容易格式)?

+0

它是自動的。只需輸入「www。」即可或「http://」和Shazam!這是一個鏈接。也在代碼中工作。使用LinkClicked事件來檢測點擊。 – 2012-03-24 22:09:12

+0

閱讀問題,「而不是出現在http:// ...它看起來像點擊這裏...」 – Oztaco 2012-03-24 22:10:34

+0

嗯,這是一個Shazam!評論。 EM_SETPARAFORMAT看起來不愉快。 – 2012-03-24 22:11:24

回答

5

當然也可以通過調用一些WIN32功能集成到你的控制,但如果你正在尋找一些標準的方式,檢查了這張貼出來: Create hyperlink in TextBox control

大約有整合不同的方式一些討論。

問候

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

因爲富文本框控件提供了一些功能,以 「DetectUrls」。然後,你可以處理被點擊的鏈接很容易:

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

,並通過擴展基類,你可以簡單的創建自己的RichTextBox contorl - 那裏你可以覆蓋你需要的方法,例如DetectUrls。

0

標準的RichTextBox控件(假設你使用Windows窗體)公開了一組相當有限的特性,所以不幸的是你需要做一些Win32互操作來實現這一點(沿着SendMessage(),CFM_LINK,EM_SETCHARFORMAT等)。

你可以在SO上的this answer找到更多關於如何做到這一點的信息。

6

在這裏,你可以找到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()); 
    } 
0

我找到了一種方法可能不是最優雅,但它只是幾行代碼並完成工作。也就是說,這個想法是通過字體變化來模擬超鏈接的外觀,並通過檢測鼠標指針的位置來模擬超鏈接的行爲。

代碼:

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封裝在上面代碼中的功能。