📄 hrichtextbox.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace CallCenter.BusinessInterfaces.MainForms
{
/// <summary>
/// HRichTextBox 的摘要说明。
/// </summary>
public class HRichTextBox : System.Windows.Forms.UserControl
{
private System.Windows.Forms.ToolBar toolBar1;
public System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ToolBarButton toolBarButton4;
private System.Windows.Forms.ToolBarButton toolBarButton6;
private System.Windows.Forms.ToolBarButton toolBarButton7;
private System.Windows.Forms.FontDialog fontDialog1;
private System.Windows.Forms.ColorDialog colorDialog1;
private System.Windows.Forms.ToolBarButton toolBarButton8;
private System.Windows.Forms.ToolBarButton toolBarButton9;
private System.Windows.Forms.ToolBarButton toolBarButton10;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolBarButton toolBarButton5;
private System.Windows.Forms.ToolBarButton toolBarButton11;
private System.Windows.Forms.ToolBarButton toolBarButton12;
private System.Windows.Forms.ToolBarButton toolBarButton13;
private System.Windows.Forms.ToolBarButton toolBarButton14;
private System.Windows.Forms.ToolBarButton toolBarButton15;
private System.Windows.Forms.ToolBarButton toolBarButton16;
private System.Windows.Forms.ToolBarButton toolBarButton17;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.ToolBarButton toolBarButton18;
private System.Windows.Forms.ToolBarButton toolBarButton19;
private System.Windows.Forms.ToolBarButton toolBarButton20;
private System.Windows.Forms.ToolBarButton toolBarButton21;
private System.ComponentModel.IContainer components;
public HRichTextBox()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton17 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton11 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton12 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton8 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton9 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton10 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton13 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton14 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton15 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton16 = new System.Windows.Forms.ToolBarButton();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.fontDialog1 = new System.Windows.Forms.FontDialog();
this.colorDialog1 = new System.Windows.Forms.ColorDialog();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.toolBarButton18 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton19 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton20 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton21 = new System.Windows.Forms.ToolBarButton();
this.SuspendLayout();
//
// toolBar1
//
this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3,
this.toolBarButton5,
this.toolBarButton4,
this.toolBarButton17,
this.toolBarButton11,
this.toolBarButton6,
this.toolBarButton7,
this.toolBarButton12,
this.toolBarButton8,
this.toolBarButton9,
this.toolBarButton10,
this.toolBarButton13,
this.toolBarButton14,
this.toolBarButton15,
this.toolBarButton16,
this.toolBarButton18,
this.toolBarButton19,
this.toolBarButton20,
this.toolBarButton21});
this.toolBar1.DropDownArrows = true;
this.toolBar1.Location = new System.Drawing.Point(0, 0);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(664, 41);
this.toolBar1.TabIndex = 0;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButton1
//
this.toolBarButton1.Tag = "居左";
this.toolBarButton1.Text = "居左";
this.toolBarButton1.ToolTipText = "居左";
//
// toolBarButton2
//
this.toolBarButton2.Tag = "居中";
this.toolBarButton2.Text = "居中";
this.toolBarButton2.ToolTipText = "居中";
//
// toolBarButton3
//
this.toolBarButton3.Tag = "居右";
this.toolBarButton3.Text = "居右";
this.toolBarButton3.ToolTipText = "居右";
//
// toolBarButton5
//
this.toolBarButton5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarButton4
//
this.toolBarButton4.Tag = "导出";
this.toolBarButton4.Text = "导出";
this.toolBarButton4.ToolTipText = "导出";
//
// toolBarButton17
//
this.toolBarButton17.Tag = "导入";
this.toolBarButton17.Text = "导入";
this.toolBarButton17.ToolTipText = "导入";
//
// toolBarButton11
//
this.toolBarButton11.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarButton6
//
this.toolBarButton6.Tag = "字体";
this.toolBarButton6.Text = "字体";
this.toolBarButton6.ToolTipText = "字体";
//
// toolBarButton7
//
this.toolBarButton7.Tag = "颜色";
this.toolBarButton7.Text = "颜色";
this.toolBarButton7.ToolTipText = "颜色";
//
// toolBarButton12
//
this.toolBarButton12.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarButton8
//
this.toolBarButton8.Tag = "剪切";
this.toolBarButton8.Text = "剪切";
this.toolBarButton8.ToolTipText = "剪切";
//
// toolBarButton9
//
this.toolBarButton9.Tag = "粘贴";
this.toolBarButton9.Text = "粘贴";
this.toolBarButton9.ToolTipText = "粘贴";
//
// toolBarButton10
//
this.toolBarButton10.Tag = "复制";
this.toolBarButton10.Text = "复制";
this.toolBarButton10.ToolTipText = "复制";
//
// toolBarButton13
//
this.toolBarButton13.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarButton14
//
this.toolBarButton14.Tag = "查找";
this.toolBarButton14.Text = "查找";
this.toolBarButton14.ToolTipText = "查找";
//
// toolBarButton15
//
this.toolBarButton15.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarButton16
//
this.toolBarButton16.Tag = "全选";
this.toolBarButton16.Text = "全选";
this.toolBarButton16.ToolTipText = "全选";
//
// richTextBox1
//
this.richTextBox1.ContextMenu = this.contextMenu1;
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(0, 41);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(664, 311);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "复制";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "剪切";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "粘贴";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// toolBarButton18
//
this.toolBarButton18.Tag = "清空";
this.toolBarButton18.Text = "清空";
this.toolBarButton18.ToolTipText = "清空";
//
// toolBarButton19
//
this.toolBarButton19.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// toolBarButton20
//
this.toolBarButton20.Tag = "撤消";
this.toolBarButton20.Text = "撤消";
this.toolBarButton20.ToolTipText = "撤消";
//
// toolBarButton21
//
this.toolBarButton21.Tag = "恢复";
this.toolBarButton21.Text = "恢复";
this.toolBarButton21.ToolTipText = "恢复";
//
// HRichTextBox
//
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.toolBar1);
this.Name = "HRichTextBox";
this.Size = new System.Drawing.Size(664, 352);
this.ResumeLayout(false);
}
#endregion
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
try
{
DialogResult dresult=new DialogResult();
switch (e.Button.Tag.ToString())
{
case "字体":
dresult=this.fontDialog1.ShowDialog();
if(dresult==DialogResult.OK)
{
this.richTextBox1.Font=this.fontDialog1.Font;
}
break;
case "颜色":
dresult=this.colorDialog1.ShowDialog();
if(dresult==DialogResult.OK)
{
this.richTextBox1.ForeColor=this.colorDialog1.Color;
}
break;
case "居左":
this.richTextBox1.SelectionAlignment=HorizontalAlignment.Left;
break;
case "居中":
this.richTextBox1.SelectionAlignment=HorizontalAlignment.Center;
break;
case "居右":
this.richTextBox1.SelectionAlignment=HorizontalAlignment.Right;
break;
case "剪切":
this.richTextBox1.Cut();
break;
case "粘贴":
this.richTextBox1.Paste();
break;
case "复制":
this.richTextBox1.Copy();
break;
case "导出":
this.saveFileDialog1.DefaultExt="rtf";
this.saveFileDialog1.Filter="word文件(*.rtf)|*.rtf|所有文件(*.*)|*.*";
this.saveFileDialog1.Title="导出文件";
dresult=this.saveFileDialog1.ShowDialog();
if(dresult==DialogResult.OK)
{
this.richTextBox1.SaveFile(this.saveFileDialog1.FileName);
MessageBox.Show("导出成功");
}
break;
case "查找":
FindForm findform=new FindForm(this);
findform.Show();
break;
case "全选":
this.richTextBox1.SelectAll();
break;
case "清空":
this.richTextBox1.Clear();
break;
case "撤消":
this.richTextBox1.Undo();
break;
case "恢复":
this.richTextBox1.Redo();
break;
case "导入":
this.openFileDialog1.DefaultExt="rtf";
this.openFileDialog1.Filter="word文件(*.rtf)|*.rtf";
this.openFileDialog1.Title="导入文件";
dresult=this.openFileDialog1.ShowDialog();
if(dresult==DialogResult.OK)
{
this.richTextBox1.LoadFile(this.openFileDialog1.FileName);
MessageBox.Show("导入成功");
}
break;
default:
break;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
this.richTextBox1.Copy();
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.richTextBox1.Cut();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
this.richTextBox1.Paste();
}
public string RtfText
{
get
{
return this.richTextBox1.Rtf;
}
set
{
this.richTextBox1.Rtf=value;
}
}
public override string Text
{
get
{
return this.richTextBox1.Text;
}
set
{
this.richTextBox1.Text=value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -