⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 superrichtextbox.cs

📁 个人信息的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace PublicUIComponent
{
	/// <summary>
	/// SuperRichTextBox 的摘要说明。
	/// </summary>
	public class SuperRichTextBox : System.Windows.Forms.RichTextBox
	{
		#region Windows 窗体设计器生成的代码

		private System.Windows.Forms.FontDialog fontDialog1;
		private System.Windows.Forms.ColorDialog colorDialog1;
		private System.Windows.Forms.OpenFileDialog openImageFileDialog;
		private System.Windows.Forms.ContextMenu ctxmRichTextBox;

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SuperRichTextBox()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			Init();
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.ctxmRichTextBox = new System.Windows.Forms.ContextMenu();
			this.openImageFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.fontDialog1 = new System.Windows.Forms.FontDialog();
			this.colorDialog1 = new System.Windows.Forms.ColorDialog();
			// 
			// openImageFileDialog
			// 
			this.openImageFileDialog.Filter = "所有可打开的图像文件|*.jpg;*.gif;*.bmp;*.ico;*.wmf|任意文件|*.*";
			this.openImageFileDialog.Title = "打开图像文件";
			// 
			// SuperRichTextBox
			// 
			this.AcceptsTab = true;
			this.AllowDrop = true;
			this.HideSelection = false;
			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SuperRichTextBox_MouseDown);
			this.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.SuperRichTextBox_LinkClicked);

		}

		#endregion

		#region 变量区

		//通过调用Win32 API SendMessage来获取每行第一个字符的CharIndex值
		[DllImport("User32.dll")] 
		public static extern IntPtr SendMessage(IntPtr hWnd,uint msg,int wp,int lp); 

		//此常数用于SendMessage函数
		private const int EM_LINEINDEX = 0xBB;

		//插入行号窗体
		frmLineCount lineNumberWindow = null;

		//查找与替换窗体
		frmFind findWindow = null;

		//用于实现拖动编辑
		int lastIndex = 0;				//点击时原来的编辑光标位置
		string lastText = "";			//点击时原来选中的文本
		private bool isCopy = false;	//是拖动还是复制

		//对外接口
		public int Col
		{
			get
			{
				return this.GetCurrentColumn();
			}
		}
		public int Row
		{
			get
			{
				return this.GetCurrentRow();
			}
		}
		//实现所见即所得的打印
		private RTFPrintDocument pd;

		#endregion

		#region 初始化区

		private void Init()
		{
			pd = new RTFPrintDocument(this);

			this.CreateContextMenu();//创建弹出式菜单
			//行号窗体
			lineNumberWindow = new frmLineCount();
			lineNumberWindow._rtf = this;

			//创建“查找与替换”窗体
			this.findWindow = new frmFind();
			//将RichTextBox对象变量传给“查找与替换”窗体
			this.findWindow.rt = this;
			//添加拖放事件
			base.DragEnter += new DragEventHandler(DoWithDragEnter);
			base.DragDrop += new DragEventHandler(DoWithDragDrop);
			base.DragOver += new DragEventHandler(DoWithDragOver);
		}

		#endregion

		#region 系统功能区

		#region 创建弹出式菜单

		bool _showctxMenu = false;
		/// <summary>
		/// 是否显示自定义的内部弹出式菜单
		/// </summary>
		public bool ShowContextMenu
		{
			get
			{
				return this._showctxMenu;
			}
			set
			{
				if(value)
				{
					this.ContextMenu = this.ctxmRichTextBox;
				}
				else
				{
					this.ContextMenu = null;
				}
				this._showctxMenu = value;
			}
		}
		/// <summary>
		/// 创建弹出式菜单,并绑定事件
		/// </summary>
		private void CreateContextMenu()
		{
			MenuItem item = new MenuItem("复制",new EventHandler(mnuCopyClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("剪切", new EventHandler(mnuCutClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("粘贴", new EventHandler(mnuPasteClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("-");
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("查找与替换", new EventHandler(mnuFindAndReplaceClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("-");
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("清除格式", new EventHandler(mnuResetTextClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("全选", new EventHandler(mnuSelectAllClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("移到开头", new EventHandler(mnuMoveToBeginClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("移到结尾", new EventHandler(mnuMoveToEndClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("放大", new EventHandler(mnuZoomInClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("-");
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("页面设置", new EventHandler(mnuPageSetupClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("打印机设置", new EventHandler(mnuPrinterSetupClick));
			this.ctxmRichTextBox.MenuItems.Add(item);

			item = new MenuItem("打印", new EventHandler(mnuPrintClick));
			this.ctxmRichTextBox.MenuItems.Add(item);
		}
		private void mnuCopyClick(object sender, System.EventArgs e)
		{
			this.Copy();
		}
		private void mnuCutClick(object sender, System.EventArgs e)
		{
			this.Cut();
		}
		private void mnuPasteClick(object sender, System.EventArgs e)
		{
			this.PasteOnlyText();
		}
		private void mnuFindAndReplaceClick(object sender, System.EventArgs e)
		{
			this.FindAndReplace();
		}
		private void mnuResetTextClick(object sender, System.EventArgs e)
		{
			this.ClearFormat();
		}
		private void mnuSelectAllClick(object sender, System.EventArgs e)
		{
			this.SelectAll();
		}
		private void mnuMoveToBeginClick(object sender, System.EventArgs e)
		{
			this.MoveToBegin();
		}
		private void mnuMoveToEndClick(object sender, System.EventArgs e)
		{
			this.MoveToEnd();
		}
		private void mnuZoomInClick(object sender, System.EventArgs e)
		{
			this.ZoomIn();
		}
		private void mnuPageSetupClick(object sender, System.EventArgs e)
		{
			this.PageSetup();
		}
		private void mnuPrinterSetupClick(object sender, System.EventArgs e)
		{
			this.PrinterSetup();
		}
		private void mnuPrintClick(object sender, System.EventArgs e)
		{
			this.Print();
		}

		#endregion

		#region 行号列号

		/// <summary>
		/// 获取RichTextBox的当前列值
		/// </summary>
		/// <returns></returns>
		private int GetCurrentColumn()
		{
			//每行第一个字符的CharIndex值
			int lineIndex = (int)SendMessage(this.Handle, EM_LINEINDEX, -1, 0);
			return this.SelectionStart - lineIndex;
		}
		/// <summary>
		/// 获取RichTextBox的当前行值
		/// </summary>
		/// <returns></returns>
		private int GetCurrentRow()
		{
			return this.GetLineFromCharIndex(this.SelectionStart);
		}

		#endregion

		/// <summary>
		/// 插入图片
		/// </summary>
		public void InsertImage()
		{
			string fileName;
			Bitmap bmp;
			//让用户选择图像文件
			if(this.openImageFileDialog.ShowDialog() == DialogResult.OK)
			{
				fileName = this.openImageFileDialog.FileName;
				try
				{
					//读入图片
					bmp = new Bitmap(fileName);
					//复制到剪贴板
					Clipboard.SetDataObject(bmp);

					//获取代表位图的剪贴板格式
					DataFormats.Format fmt = DataFormats.GetFormat(DataFormats.Bitmap);

					if(this.CanPaste(fmt))
					{
						this.Paste(fmt);
					}
				}
				catch(Exception ex)
				{
					MessageBox.Show("不能插入图片。系统提示:" + ex.Message, "提示信息" ,MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		/// <summary>
		/// 仅可粘贴文本
		/// </summary>
		public void PasteOnlyText()
		{
			IDataObject iData = Clipboard.GetDataObject();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -