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

📄 superrichtextbox.cs

📁 个人信息的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
			if(iData.GetDataPresent(DataFormats.Text))
			{
				this.SelectedText = iData.GetData(DataFormats.Text).ToString();
			}
		}
		/// <summary>
		/// 清除格式
		/// </summary>
		public void ClearFormat()
		{
			string str = this.Text;
			this.ResetText();
			this.Text = str;
			this.MoveToEnd();
		}
		/// <summary>
		/// 移到开头
		/// </summary>
		public void MoveToBegin()
		{
			this.SelectionStart = 0;
			this.ScrollToCaret();
		}
		/// <summary>
		/// 移到结尾
		/// </summary>
		public void MoveToEnd()
		{
			this.SelectionStart = this.Text.Length;
			this.ScrollToCaret();
		}
		/// <summary>
		/// 放大
		/// </summary>
		public void ZoomIn()
		{
			if(this.ZoomFactor + 1 > 64)
				this.ZoomFactor = 63;
			else
				this.ZoomFactor += 0.2F;
		}
		/// <summary>
		/// 打印
		/// </summary>
		public void Print()
		{
			try
			{
				PrintPreviewDialog ppd = new PrintPreviewDialog();
				ppd.Document = this.pd;
				ppd.ShowDialog();
			}
			catch(Exception ex)
			{
				MessageBox.Show("An error occurred attempting to preview the file to print - " + ex.Message);
			}
		}
		/// <summary>
		/// 页面设置
		/// </summary>
		public void PageSetup()
		{
			try
			{
				PageSetupDialog ps = new PageSetupDialog();

				ps.PageSettings = this.pd.DefaultPageSettings;

				if(ps.ShowDialog() == DialogResult.OK)
				{
					this.pd.DefaultPageSettings = ps.PageSettings;
				}
			}
			catch(Exception ex)
			{
				MessageBox.Show("An error occurred -" + ex.Message);
			}
		}
		/// <summary>
		/// 打印机设置
		/// </summary>
		public void PrinterSetup()
		{
			PrintDialog ptd = new PrintDialog();
			ptd.Document = this.pd;

			ptd.PrinterSettings = this.pd.PrinterSettings;

			if(ptd.ShowDialog() == DialogResult.OK)
			{
				this.pd.PrinterSettings = ptd.PrinterSettings;
			}
		}
		/// <summary>
		/// 单击网址,打开IE访问互联网
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SuperRichTextBox_LinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
		{
			if(this.DetectUrls == true)
			{
				System.Diagnostics.Process.Start("IExplore.exe", e.LinkText);
			}
		}
		/// <summary>
		/// 插入行号
		/// </summary>
		public void InsertLineNum()
		{
			this.lineNumberWindow.Show();
		}
		/// <summary>
		/// 查找与替换
		/// </summary>
		public void FindAndReplace()
		{
			if(findWindow == null)
				return;

			//如果用户选中了一个字词,则认为他想查找此字词,所以将其自动加入到查找窗体的“查找”组合框中
			if(this.SelectedText != string.Empty)
			{
				this.findWindow.cboFind.Text = this.SelectedText;
			}
			//如果窗体是最小化的,恢复原大小
			findWindow.WindowState = FormWindowState.Normal;
			//显示此窗体
			findWindow.Show();
		}
		/// <summary>
		/// 设置选中文字的字体
		/// </summary>
		public void SetSelectionFontFromFontDialog()
		{
			//按照选中部分的文字设置字体对话框的初始字体和颜色
			Font _font = this.SelectionFont;
			
			//如果能识别出字体
			if(_font != null)
				this.fontDialog1.Font = _font;
			
			Color _clr = this.SelectionColor;

			this.fontDialog1.ShowColor = true;
			this.fontDialog1.Color = _clr;

			if(this.fontDialog1.ShowDialog() == DialogResult.OK)
			{
				_font = this.fontDialog1.Font;
				this.SelectionColor = this.fontDialog1.Color;
				this.SelectionFont = _font;
			}
		}
		/// <summary>
		/// 设置选中文本的字体颜色
		/// </summary>
		public void SetSelectionColorFromColorDialog()
		{
			if(this.colorDialog1.ShowDialog() == DialogResult.OK)
			{
				this.SelectionColor = this.colorDialog1.Color;
			}
		}

		#region 支持拖放

		/// <summary>
		/// 从其它程序拖动文本进入本控件范围,设定鼠标光标的形状
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DoWithDragEnter(object sender,  System.Windows.Forms.DragEventArgs e)
		{
			//用户拖动的是文本对象
			if(e.Data.GetDataPresent(DataFormats.Text))
			{
				if((e.KeyState & 8) == 8)//用户压住了CTRL键
				{
					e.Effect = DragDropEffects.Copy;//设定复制光标
				}
				else
				{
					e.Effect = DragDropEffects.Move;//设定移动光标
				}
			}
			else//拖动的是其它对象,则不允许放置在本控件上,显示一个禁止光标(一个中间有一横线的小圆)
			{
				e.Effect = DragDropEffects.None;
			}
		}
		/// <summary>
		/// 鼠标左键松开,对象被放下
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DoWithDragDrop(object sender,  System.Windows.Forms.DragEventArgs e)
		{
			//原先选中了文本,但光标又拖回原来的地方,则不进行操作			
			if(this.lastText != string.Empty && this.SelectionStart > lastIndex && this.SelectionStart < lastIndex + lastText.Length)
			{}
			else
			{
				//原先选中了文本,光标拖到其它位置
				if(isCopy)//是复制状态,则仅仅插入新文本即可
				{
					this.SelectedText = e.Data.GetData(DataFormats.Text).ToString();
					//还原到初始状态
					lastIndex = 0;
					lastText = "";
					return;
				}
				
				//获取当前插入点
				int curIndex = this.SelectionStart;

				if(curIndex < lastIndex)//如果当前插入点在原来选中的文字之前
				{
					//删除原先所选的文字
					this.SelectionStart = lastIndex;
					this.SelectionLength = lastText.Length;
					this.SelectedText = "";

					//在新位置插入新文本
					this.SelectionStart = curIndex;
					this.SelectedText = e.Data.GetData(DataFormats.Text).ToString();
				}
				else//如果当前插入点在原来选中的文字之后,则先插入新文本
				{
					this.SelectedText = e.Data.GetData(DataFormats.Text).ToString();
					//删除原先所选的文字
					this.SelectionStart = lastIndex;
					this.SelectionLength = lastText.Length;
					this.SelectedText = "";
				}
				//还原到初始状态
				lastIndex = 0;
				lastText = "";
			}
		}
		/// <summary>
		/// 正在拖动过程中
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DoWithDragOver(object sender,  System.Windows.Forms.DragEventArgs e)
		{
			//在运行时动态检测按键情况,随时变换图标
			this.isCopy = false;
			if(e.Data.GetDataPresent(DataFormats.Text))
			{
				if((e.KeyState & 8) == 8)
				{
					e.Effect = DragDropEffects.Copy;
					isCopy = true;
				}
				else
				{
					e.Effect = DragDropEffects.Move;
				}
			}
			else
			{
				e.Effect = DragDropEffects.None;
			}
		}
		private int lastTime = 0;
		private void SuperRichTextBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			//仅处理左键单击
			if(e.Button == MouseButtons.Left)
			{
				//判断是否双击
				int pastTime;
				bool isDoubleClick = false;
				if(lastTime == 0)
				{
					lastTime = Environment.TickCount;
				}
				else
				{
					pastTime = Environment.TickCount - lastTime;
					lastTime = Environment.TickCount;
					if(pastTime < System.Windows.Forms.SystemInformation.DoubleClickTime)
					{
						isDoubleClick = true;
					}
					else
					{
						isDoubleClick = false;
					}
				}
				if(isDoubleClick)
				{
					return;
				}
				//获取距鼠标点击坐标最近的字符索引
				int index = -1;
				if(this.Text.Length > 0)
				{
					index = this.GetCharFromPosition(new Point(e.X, e.Y));
				}

				if(this.SelectionLength > 0 && this.AllowDrop)//如果原先已经选中了字串
				{
					//点击的文字在选中的字串内部
					if(index >= this.SelectionStart && index < this.SelectionStart + this.SelectionLength)
					{
						//记录下当前选中的文字和字符索引
						lastIndex = this.SelectionStart;
						lastText = this.SelectedText;
						//去掉最后一个换行符,以免拖动时对布局影响过大
						if(lastText.ToCharArray()[lastText.Length - 1] == (char)10)
						{
							lastText = lastText.Substring(0, lastText.Length - 1);
						}
						//开始拖动
						this.DoDragDrop(lastText, DragDropEffects.All);
					}
				}
				else
				{
					//还原到初始状态
					lastIndex = 0;
					lastText = "";
				}
			}
		}

		#endregion

		#endregion
	}
}

⌨️ 快捷键说明

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