protected override void OnMouseDown(MouseEventArgs e)
{
if (boolLinkStyle)
{
int intCharIndex;
int intLine;
int intStart;
int intEnd=0;
string strLine;
char ch;
ch = this.GetCharFromPosition(new Point(e.X,e.Y));
intCharIndex = this.GetCharIndexFromPosition(new Point(e.X,e.Y));
if (this.Cursor == Cursors.Hand)
{
if (ch != '【' && ch != '】')
{
intLine = this.GetLineFromCharIndex(intCharIndex);
strLine = this.Text;
intStart = strLine.Substring(0,intCharIndex).LastIndexOf("【");
intEnd = strLine.IndexOf("】",intCharIndex);
if ((intEnd >= 0) && (intStart >= 0))
this.OnLinkClicked(new LinkClickedEventArgs(strLine.Substring(intStart + 1,intEnd - intStart - 1)));
}
this.Select(intEnd,0);
}
}
base.OnMouseDown (e);
}
protected override void OnTextChanged(EventArgs e)
{
if (boolLinkStyle)
{
int intCharIndex;
int intLine;
int intCurIdx;
string strLine;
char ch;
Font newFont;
Font oldFont;
Color oldColor;
可以的.
Class Test
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=4)]
public class CHARFORMAT2
{
public int cbSize = Marshal.SizeOf(typeof(CHARFORMAT2));
public int dwMask;
public int dwEffects;
public int yHeight;
public int yOffset;
public int crTextColor;
public byte bCharSet;
public byte bPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string szFaceName;
public short wWeight;
public short sSpacing;
public int crBackColor;
public int lcid;
public int dwReserved;
public short sStyle;
public short wKerning;
public byte bUnderlineType;
public byte bAnimation;
public byte bRevAuthor;
public byte bReserved1;
}
[DllImportAttribute("user32", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] CHARFORMAT2 lParam);
public static void SetSelectAttr(RichTextBox rtb, bool link)
{
if (rtb == null)
{
throw new ArgumentNullException("rtb");
}
HARFORMAT2 charformat;
charformat = new NativeMethods.CHARFORMAT2();
charformat.dwMask = 0x00000020;//CFM_LINK
charformat.dwEffects = link ? 0x20/*CFE_LINK*/ : 0;
SendMessage(rtb.Handle, WinMessage.EM_SETCHARFORMAT, 1/*SCF_SELECTION*/, charformat);
}