栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

richtextbox控件插入链接代码分享

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

richtextbox控件插入链接代码分享

复制代码 代码如下:
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;  
  private const UInt32 CFE_SUPERscript= 0x00020000;  

  private const int CFM_SMALLCAPS  = 0x0040;   
  private const int CFM_ALLCAPS  = 0x0080;   
  private const int CFM_HIDDEN  = 0x0100;   
  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;  
  private const int CFM_WEIGHT  = 0x00400000;
  private const int CFM_SPACING  = 0x00200000;  
  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;
  private const byte CFU_UNDERLINEDOUBLE  = 0x00000003;
  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;

  #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; }
  }

  ///


  /// Insert a given text as a link into the RichTextBox at the current insert position.
  ///

  /// Text to be inserted
  public void Insertlink(string text)
  {
   Insertlink(text, this.SelectionStart);
  }

  ///


  /// Insert a given text at a given position as a link.
  ///

  /// Text to be inserted
  /// Insert position
  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);
  }

  ///


  /// 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.
  ///

  /// Text to be inserted
  /// Invisible hyperlink string to be inserted
  public void Insertlink(string text, string hyperlink)
  {
   Insertlink(text, hyperlink, this.SelectionStart);
  }

  ///


  /// 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.
  ///

  /// Text to be inserted
  /// Invisible hyperlink string to be inserted
  /// Insert position
  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 = @"{rtf1ansi "+text+@"v #"+hyperlink+@"v0}";
   this.Select(position, text.Length + hyperlink.Length + 1);
   this.SetSelectionlink(true);
   this.Select(position + text.Length + hyperlink.Length + 1, 0);
  }

  ///


  /// Set the current selection's link style
  ///

  /// true: set link style, false: clear link style
  public void SetSelectionlink(bool link)
  {
   SetSelectionStyle(CFM_link, link ? CFE_link : 0);
  }
  ///
  /// Get the link style for the current selection
  ///

  /// 0: link style not set, 1: link style set, -1: mixed
  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;
  }
 }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/126826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号