读书人

richtextbox如何实现超链接

发布时间: 2012-12-14 10:33:08 作者: rapoo

richtextbox怎么实现超链接
richtextbox怎么把特定的中文解析成为超链接 [你好],把[]里的值你好做成超链接
[解决办法]
关注中...
[解决办法]
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
namespace richtextboxshree
{
public class richtextboxshree : System.Windows.Forms.RichTextBox
{
#region Interop-Defines
[StructLayout(LayoutKind.Sequential)]
private struct CHARFORMAT2_STRUCT
{
public UInt32 cbSize;
public UInt32 dwMask;
public UInt32 dwEffects;
public Int32 yHeight;
public Int32 yOffset;
public Int32 crTextColor;
public byte bCharSet;
public byte bPitchAndFamily;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public char[] szFaceName;
public UInt16 wWeight;
public UInt16 sSpacing;
public int crBackColor; // Color.ToArgb() -> int
public int lcid;
public int dwReserved;
public Int16 sStyle;
public Int16 wKerning;
public byte bUnderlineType;
public byte bAnimation;
public byte bRevAuthor;
public byte bReserved1;
}

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

private const int WM_USER = 0x0400;


private const int EM_GETCHARFORMAT = WM_USER + 58;
private const int EM_SETCHARFORMAT = WM_USER + 68;

private const int SCF_SELECTION = 0x0001;
private const int SCF_WORD = 0x0002;
private const int SCF_ALL = 0x0004;

#region CHARFORMAT2 Flags
private const UInt32 CFE_BOLD = 0x0001;
private const UInt32 CFE_ITALIC = 0x0002;
private const UInt32 CFE_UNDERLINE = 0x0004;
private const UInt32 CFE_STRIKEOUT = 0x0008;
private const UInt32 CFE_PROTECTED = 0x0010;
private const UInt32 CFE_LINK = 0x0020;
private const UInt32 CFE_AUTOCOLOR = 0x40000000;
private const UInt32 CFE_SUBSCRIPT = 0x00010000; /* Superscript and subscript are */
private const UInt32 CFE_SUPERSCRIPT = 0x00020000; /* mutually exclusive */

private const int CFM_SMALLCAPS = 0x0040; /* (*) */
private const int CFM_ALLCAPS = 0x0080; /* Displayed by 3.0 */
private const int CFM_HIDDEN = 0x0100; /* Hidden by 3.0 */
private const int CFM_OUTLINE = 0x0200; /* (*) */
private const int CFM_SHADOW = 0x0400; /* (*) */
private const int CFM_EMBOSS = 0x0800; /* (*) */
private const int CFM_IMPRINT = 0x1000; /* (*) */
private const int CFM_DISABLED = 0x2000;
private const int CFM_REVISED = 0x4000;

private const int CFM_BACKCOLOR = 0x04000000;
private const int CFM_LCID = 0x02000000;
private const int CFM_UNDERLINETYPE = 0x00800000; /* Many displayed by 3.0 */
private const int CFM_WEIGHT = 0x00400000;


private const int CFM_SPACING = 0x00200000; /* Displayed by 3.0 */
private const int CFM_KERNING = 0x00100000; /* (*) */
private const int CFM_STYLE = 0x00080000; /* (*) */
private const int CFM_ANIMATION = 0x00040000; /* (*) */
private const int CFM_REVAUTHOR = 0x00008000;


private const UInt32 CFM_BOLD = 0x00000001;
private const UInt32 CFM_ITALIC = 0x00000002;
private const UInt32 CFM_UNDERLINE = 0x00000004;
private const UInt32 CFM_STRIKEOUT = 0x00000008;
private const UInt32 CFM_PROTECTED = 0x00000010;
private const UInt32 CFM_LINK = 0x00000020;
private const UInt32 CFM_SIZE = 0x80000000;
private const UInt32 CFM_COLOR = 0x40000000;
private const UInt32 CFM_FACE = 0x20000000;
private const UInt32 CFM_OFFSET = 0x10000000;
private const UInt32 CFM_CHARSET = 0x08000000;
private const UInt32 CFM_SUBSCRIPT = CFE_SUBSCRIPT
[解决办法]
CFE_SUPERSCRIPT;
private const UInt32 CFM_SUPERSCRIPT = CFM_SUBSCRIPT;

private const byte CFU_UNDERLINENONE = 0x00000000;
private const byte CFU_UNDERLINE = 0x00000001;
private const byte CFU_UNDERLINEWORD = 0x00000002; /* (*) displayed as ordinary underline */
private const byte CFU_UNDERLINEDOUBLE = 0x00000003; /* (*) displayed as ordinary underline */
private const byte CFU_UNDERLINEDOTTED = 0x00000004;
private const byte CFU_UNDERLINEDASH = 0x00000005;
private const byte CFU_UNDERLINEDASHDOT = 0x00000006;
private const byte CFU_UNDERLINEDASHDOTDOT = 0x00000007;
private const byte CFU_UNDERLINEWAVE = 0x00000008;
private const byte CFU_UNDERLINETHICK = 0x00000009;


private RichTextBox richTextBox1;
private IContainer components;
private ContextMenuStrip contextMenuStrip1;
private const byte CFU_UNDERLINEHAIRLINE = 0x0000000A; /* (*) displayed as ordinary underline */

#endregion

#endregion

[解决办法]
public richtextboxshree()
{
// Otherwise, non-standard links get lost when user starts typing
// next to a non-standard link
this.DetectUrls = false;
}

[DefaultValue(false)]
public new bool DetectUrls
{
get { return base.DetectUrls; }
set { base.DetectUrls = value; }
} /// <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>
/// Set the current selection's link style
/// </summary>
/// <param name="link">true: set link style, false: clear link style</param>
public void SetSelectionLink(bool link)
{
SetSelectionStyle(CFM_LINK, link ? CFE_LINK : 0);
} private void SetSelectionStyle(UInt32 mask, UInt32 effect)
{
CHARFORMAT2_STRUCT cf = new CHARFORMAT2_STRUCT();
cf.cbSize = (UInt32)Marshal.SizeOf(cf);
cf.dwMask = mask;
cf.dwEffects = effect;

IntPtr wpar = new IntPtr(SCF_SELECTION);
IntPtr lpar = Marshal.AllocCoTaskMem(Marshal.SizeOf(cf));
Marshal.StructureToPtr(cf, lpar, false);

IntPtr res = SendMessage(Handle, EM_SETCHARFORMAT, wpar, lpar);

Marshal.FreeCoTaskMem(lpar);
}

[解决办法]

private Hashtable myHashTable = null;
private string contextvalue = "";
private int x;
private int y;
private int z;
private int h;
private string Linkvalue = "";
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);


this.SuspendLayout();
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(100, 96);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// contextMenuStrip1
//
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4);
this.ResumeLayout(false);

}
private bool boolrichbox = true;
public bool IsTrigger
{
get { return boolrichbox; }
set { boolrichbox = value; }
}
/// <summary>
/// 改写ontextchanged事件
/// </summary>
/// <param name="e"></param>
protected override void OnTextChanged(EventArgs e)
{
if (IsTrigger)
{
SetRicheTextBox();// 改写richtextbox的值
}
base.OnTextChanged(e);
}
/// <summary>
/// 改写richtextbox的值
/// </summary>
private void SetRicheTextBox()
{
string txtbox = this.Text.ToString();
int index = 0;//richtextbox中包含[的索引
int end;//richtextbox中包含]的索引


string setValue = "";
string[] stringzu;
int j = 1;

Hashtable hashtable = null;
if (hashtable == null)
{
hashtable = new Hashtable();
}
if (myHashTable == null)//hashtable是否首次创建
{
myHashTable = new Hashtable();//创建hashtabl
}
int indexof = 0;//定位i的结果
string startString = "";
for (int i = 0; i < txtbox.Length; i++)
{
if (txtbox[i].ToString() == "[")
{
index = i + 1;
}
if (txtbox[i].ToString() == "]")
{
end = i;
setValue = txtbox.Substring(index, end - index);
stringzu = setValue.Split('
[解决办法]
');
if (myHashTable.Contains(stringzu[0].ToString()) == false)//hastable的key是否存在
{
myHashTable.Add(stringzu[0].ToString(), setValue.ToString());
}
end = i;
hashtable.Add(j, stringzu[0].ToString() + "


[解决办法]
" + index.ToString());
j++;
startString = txtbox.Substring(0, index-1);
startString += txtbox.Substring(end + 1, txtbox.Length - end - 1);
indexof = i;
txtbox = startString;
i = index;
}
}
this.Text = txtbox;
for (int i = 0; i < hashtable.Count; i++)
{

string richtexvalue = hashtable[i+1].ToString();
string[] splic = richtexvalue.Split('

读书人网 >.NET

热点推荐