亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

richtextbox控件插入鏈接代碼分享

2020-01-24 02:58:41
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace RichTextBoxLinks
{
 public class RichTextBoxEx : 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 const byte CFU_UNDERLINEHAIRLINE = 0x0000000A; /* (*) displayed as ordinary underline */

  #endregion

  #endregion

  public RichTextBoxEx()
  {
   // 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 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)
  {
   if (position < 0 || position > this.Text.Length)
    throw new ArgumentOutOfRangeException("position");

   this.SelectionStart = position;
   this.SelectedRtf = @"{/rtf1/ansi "+text+@"/v #"+hyperlink+@"/v0}";
   this.Select(position, text.Length + hyperlink.Length + 1);
   this.SetSelectionLink(true);
   this.Select(position + text.Length + hyperlink.Length + 1, 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);
  }
  /// <summary>
  /// Get the link style for the current selection
  /// </summary>
  /// <returns>0: link style not set, 1: link style set, -1: mixed</returns>
  public int GetSelectionLink()
  {
   return GetSelectionStyle(CFM_LINK, CFE_LINK);
  }


  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 int GetSelectionStyle(UInt32 mask, UInt32 effect)
  {
   CHARFORMAT2_STRUCT cf = new CHARFORMAT2_STRUCT();
   cf.cbSize = (UInt32)Marshal.SizeOf(cf);
   cf.szFaceName = new char[32];

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

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

   cf = (CHARFORMAT2_STRUCT)Marshal.PtrToStructure(lpar, typeof(CHARFORMAT2_STRUCT));

   int state;
   // dwMask holds the information which properties are consistent throughout the selection:
   if ((cf.dwMask & mask) == mask)
   {
    if ((cf.dwEffects & effect) == effect)
     state = 1;
    else
     state = 0;
   }
   else
   {
    state = -1;
   }

   Marshal.FreeCoTaskMem(lpar);
   return state;
  }
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美性猛交丰臀xxxxx网站| 国产热re99久久6国产精品| 欧美成人精品一区二区三区| 日韩综合中文字幕| 最近2019年好看中文字幕视频| 亚洲成人亚洲激情| 亚洲人成电影网站色| 日韩电影免费在线观看| 欧美视频中文字幕在线| 国内精品免费午夜毛片| 久久久人成影片一区二区三区观看| 国产精品av在线播放| 欧美体内谢she精2性欧美| 亚洲深夜福利在线| 欧美精品久久久久久久免费观看| 欧美洲成人男女午夜视频| 亚洲欧美在线一区| 欧美一级淫片videoshd| 国产精品香蕉在线观看| 97视频免费观看| 国产在线视频2019最新视频| 精品一区二区亚洲| 精品福利视频导航| 中文字幕精品网| 91经典在线视频| 亚洲人高潮女人毛茸茸| 精品福利樱桃av导航| 国产亚洲精品va在线观看| 成人在线一区二区| 日韩精品视频免费专区在线播放| 欧美电影免费在线观看| 中文字幕av一区中文字幕天堂| 成人女保姆的销魂服务| 久久久久久伊人| 欧美日在线观看| 富二代精品短视频| 成人淫片在线看| 欧美日韩在线视频一区| 亚洲国产第一页| 亚洲精品综合精品自拍| 国产91色在线|| 日韩欧美高清视频| 69久久夜色精品国产69| 国产日韩在线视频| 欧美日本在线视频中文字字幕| 亚洲永久免费观看| 国产欧美精品一区二区三区介绍| 欧美日韩国产黄| 国产精品欧美一区二区| 国自产精品手机在线观看视频| 精品国产91久久久久久| 久久免费高清视频| 日韩理论片久久| 久久久久久久成人| 亚洲欧美国产va在线影院| 久久视频免费在线播放| 日韩激情av在线免费观看| 黑人精品xxx一区一二区| 日韩不卡中文字幕| 精品少妇一区二区30p| 中文字幕日韩av| 青青精品视频播放| 5566日本婷婷色中文字幕97| 45www国产精品网站| 91精品久久久久久久久久另类| 精品中文字幕在线观看| 国产成人精品国内自产拍免费看| 亚洲一区二区免费| 亚洲男人天堂网站| 亲爱的老师9免费观看全集电视剧| 亚洲精品国产美女| 亚洲精品欧美一区二区三区| 国产精品成人aaaaa网站| 另类美女黄大片| 97久久精品视频| 日韩精品中文字幕在线观看| 欧美在线欧美在线| 欧美高清第一页| 日韩免费在线免费观看| 日韩在线欧美在线国产在线| 性色av一区二区三区在线观看| 亚洲国产高潮在线观看| 91中文在线视频| 成人午夜一级二级三级| 久久亚洲国产精品| 日韩美女主播视频| 久久视频在线免费观看| 亚洲电影在线看| 国产精品久久久久久久久久三级| 97碰在线观看| 日韩欧美亚洲综合| 亚洲国产精品女人久久久| 日韩高清免费在线| 91av在线精品| 欧美日韩色婷婷| 伦伦影院午夜日韩欧美限制| 精品久久久av| 色哟哟亚洲精品一区二区| 另类专区欧美制服同性| 欧美日韩国产中文精品字幕自在自线| 欧美一区二区三区精品电影| 亚洲国产成人一区| 亚洲欧美一区二区三区在线| 青青草一区二区| 欧美一级bbbbb性bbbb喷潮片| 国产精品成人va在线观看| 国产成人拍精品视频午夜网站| 日韩欧美中文字幕在线观看| 欧美视频国产精品| 日韩av电影在线播放| 国产va免费精品高清在线| 97av在线播放| 成人免费视频在线观看超级碰| 亚洲成人教育av| 在线视频一区二区| 久久国产精品影视| 欧美激情三级免费| 国产在线播放91| 欧美一级视频免费在线观看| 国产亚洲精品久久久久久| 国产成人精品av| 日本老师69xxx| 亚洲www视频| 精品国产一区二区三区久久狼5月| 国产精品电影久久久久电影网| 国产精品白嫩初高中害羞小美女| 日韩高清中文字幕| 亚洲国产精品久久| 日韩国产高清视频在线| 久久精品国产96久久久香蕉| 亚洲区免费影片| 精品无人国产偷自产在线| 大伊人狠狠躁夜夜躁av一区| 日韩美女在线播放| 欧美国产欧美亚洲国产日韩mv天天看完整| 91精品视频网站| 欧美日韩国产成人在线| 91牛牛免费视频| 九色精品免费永久在线| 久久久99免费视频| 欧美中文字幕在线播放| 亚洲欧洲国产伦综合| 亚洲天堂久久av| 久久99久国产精品黄毛片入口| 亚洲一区中文字幕在线观看| 亚洲成人激情在线观看| 日韩福利伦理影院免费| 4438全国亚洲精品在线观看视频| 国产主播在线一区| 久久精品久久久久久| 中文字幕日韩在线视频| 欧美激情乱人伦| 久久精品一本久久99精品| 亚洲r级在线观看| 中文字幕日韩欧美精品在线观看| 国产亚洲精品久久久优势| 45www国产精品网站| 欧美激情精品在线| 91午夜在线播放| 亚洲第五色综合网| 在线播放日韩精品| 欧洲s码亚洲m码精品一区| 欧美视频免费在线观看| 最新国产精品亚洲|