RichTextBox超链接解析
程序员文章站
2022-04-29 17:45:50
...
最近都在做一个能在Winform的RichTextBox中添加像网页中的超链接,点击后自动解析选项关键词弹出列表框,显示候选词,实现快速录入的功能.如下图所示
richTextBox插入链接的方法
/// <summary>
/// Insert a given text as a link into the RichTextBox at the current insert position.
/// </summary>
/// <param name="text">Text to be inserted</param>
public void InsertLink(string text)
{
InsertLink(text, this.SelectionStart);
}
/// <summary>
/// Insert a given text at a given position as a link.
/// </summary>
/// <param name="text">Text to be inserted</param>
/// <param name="position">Insert position</param>
public void InsertLink(string text, int position)
{
if (position < 0 || position > this.Text.Length)
throw new ArgumentOutOfRangeException("position");
this.SelectionStart = position;
this.SelectedText = text;
this.Select(position, text.Length);
this.SetSelectionLink(true);
this.Select(position + text.Length, 0);
}
/// <summary>
/// Insert a given text at at the current input position as a link.
/// The link text is followed by a hash (#) and the given hyperlink text, both of
/// them invisible.
/// When clicked on, the whole link text and hyperlink string are given in the
/// LinkClickedEventArgs.
/// </summary>
/// <param name="text">Text to be inserted</param>
/// <param name="hyperlink">Invisible hyperlink string to be inserted</param>
public void InsertLink(string text, string hyperlink)
{
InsertLink(text, hyperlink, this.SelectionStart);
}
/// <summary>
/// Insert a given text at a given position as a link. The link text is followed by
/// a hash (#) and the given hyperlink text, both of them invisible.
/// When clicked on, the whole link text and hyperlink string are given in the
/// LinkClickedEventArgs.
/// </summary>
/// <param name="text">Text to be inserted</param>
/// <param name="hyperlink">Invisible hyperlink string to be inserted</param>
/// <param name="position">Insert position</param>
public void InsertLink(string text, string hyperlink, int position)
{
try
{
if (position < 0 || position > this.Text.Length)
throw new ArgumentOutOfRangeException("position");
this.SelectionStart = position;
//this.SelectedRtf = @"{\rtf1\ansi " + text + @"\v #" + hyperlink + @"\v0}";
this.SelectedRtf = @"{\rtf1\ansicpg936" + text + @"\v #" + hyperlink + @"\v0}"; //解决中文乱码
this.Select(position, text.Length + hyperlink.Length + 1);
this.SetSelectionLink(true);
this.Select(position + text.Length + hyperlink.Length + 1, 0);
}
catch (Exception ex)
{
}
}
链接单击事件的方法
/// <summary>
/// 链接点击事件
/// </summary>
/// <param name="e"></param>
protected override void OnLinkClicked(LinkClickedEventArgs e)
{
string linkModKeyName = GetLinkedModId(e.LinkText);
string linkDispStr = GetLinkedDisplayString(e.LinkText);
SetSelectionLink(false);
// GetXY();
int linkstrSart = GetLeftLinkStartPostion(linkDispStr, iMouseIndex);
this.LastSelectionSart = linkstrSart;
this.LastSelectionLength = linkDispStr.Length;
this.SelectionStart = linkstrSart;
this.SelectionLength = linkDispStr.Length;
Point mPoint = Control.MousePosition;
ItemListFrm ilf = new ItemListFrm( cmmsp, linkModKeyName, mPoint);
ilf.Event_AfterSelectedItems += AfterChangeHouXuanCiReSetLink;
ilf.Show();
base.OnLinkClicked(e);
}
候选词窗口关闭事件
/// <summary>
/// 选词框关闭后操作
/// </summary>
/// <param name="oKeyName"></param>
/// <param name="oDisplayName"></param>
private void AfterChangeHouXuanCiReSetLink(string oKeyName, string oDisplayName)
{
this.EditLink(oDisplayName, oKeyName, this.LastSelectionSart, this.LastSelectionLength);
}
修改选中的词,并修改超链接
public void EditLink(string text,string hyperlink,int position, int oldLength)
{
try
{
if (position < 0 || position > this.Text.Length)
throw new ArgumentOutOfRangeException("position");
this.SelectionStart = position;
this.SelectionLength = oldLength;
string tmpstr = this.SelectedText;
this.SetSelectionLink(false);
this.SelectionStart = position;
this.SelectionLength = oldLength;
this.SelectedText = text;
//this.SelectionStart = position;
//this.SelectionLength = oldLength;
//this.SelectedText = "";
this.SelectionStart = position;
this.SelectionLength = text.Length;
this.SelectedRtf = @"{\rtf1\ansicpg936" + text + @"\v #" + hyperlink + @"\v0}"; //解决中文乱码
this.Select(position, text.Length + hyperlink.Length + 1);
this.SetSelectionLink(true);
this.Select(position + text.Length + hyperlink.Length + 1, 0);
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
演示下载地址:https://download.csdn.net/download/spd260/10278920