一.使用工具、创建环境、语言
使用工具:Visual Studio 2022(社区免费版)
创建环境:Win 11系统
语言:C#
二.创建步骤
第一步:创建一个新的项目,具体步骤如下图所示
第二步:项目创建好后如下界面
在工具箱中拖动需要的控件到窗体上,若页面中没有工具箱,可以在视图中点击。页面右下角是控件的相关属性和事件。(若一个解决方案中有多个窗体,可以在Program.cs文件中更改启动窗体)
第三步:文本编辑器所需控件及其相关属性如下表:
控件类型 | 控件名称(Name属性) | 控件文本内容(Text属性) |
Form窗体 | FrmMain_Edit | 文本编辑器 |
MenuStrip | menuStrip1 | menuStrip1 |
ToolStrip | toolStrip1 | toolStrip1 |
RichTextBox | rtxtText | 哈利波特骑着扫帚飞...(可自行修改) |
StatusStrip | statusStrip1 | statusStrip1 |
ToolStripMenuItem | tsmiFile | 文件(&F) |
ToolStripMenuItem | tsmiEdit | 编辑(&E) |
ToolStripMenuItem | tsmiNew | 新建<Ctrl+N> |
ToolStripMenuItem | tsmiOpen | 打开<Ctrl+O> |
ToolStripMenuItem | tsmiSave | 保存<Ctrl+S> |
ToolStripMenuItem | tsmiPrint | 打印<Ctrl+P> |
ToolStripMenuItem | tsmiExit | 退出<Ctrl+E> |
ToolStripMenuItem | tsmiCut | 剪切<Ctrl+X> |
ToolStripMenuItem | tsmiCopy | 复制<Ctrl+C> |
ToolStripMenuItem | tsmiPaste | 粘贴<Ctrl+V> |
ToolStripButton | tbtnCut | 剪切 |
ToolStripButton | tbtnCopy | 复制 |
ToolStripButton | tbtnPaste | 粘贴 |
ToolStripComboBox | cboFont | 定义字体类型 |
ToolStripComboBox | cboSize | 定义字体大小 |
ToolStripStatusLabel | tsslMousePosition | toolStripStatusLabel1 |
ContextMenuStrip | contextMenuStrip1 | ------ |
ToolStripMenuItem | cmiCut | 剪切<Ctrl+X> |
ToolStripMenuItem | cmiCopy | 复制<Ctrl+C> |
ToolStripMenuItem | cmiPaste | 粘贴<Ctrl+V> |
搭建好的界面如下图所示:
第四步:编写相关控件的代码: 双击页面上的控件进入代码编辑界面
public partial class Frm65_Edit : Form
{
public Frm65_Edit()
{
InitializeComponent();
}
private void tsmiCut_Click(object sender, EventArgs e)
{
//将所选内容放到剪切板上,清除所选内容
Clipboard.SetDataObject(rtxtText.SelectedText);
rtxtText.SelectedText = String.Empty;
}
private void tsmiCopy_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(rtxtText.SelectedText);
}
private void Paste()
{
//将剪切板上的内容放到指定位置
IDataObject iData = Clipboard.GetDataObject();
rtxtText.SelectedText = (String)iData.GetData(DataFormats.Text);
}
private void tsmiPaste_Click(object sender, EventArgs e)
{
Paste();
}
private void cmiCut_Click(object sender, EventArgs e)
{
//将所选内容放到剪切板上,清除所选内容
Clipboard.SetDataObject(rtxtText.SelectedText);
rtxtText.SelectedText = String.Empty;
}
private void cmiPaste_Click(object sender, EventArgs e)
{
Paste();
}
private void cmiCopy_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(rtxtText.SelectedText);
}
private void tbtnCut_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(rtxtText.SelectedText);
rtxtText.SelectedText = String.Empty;
}
private void tbtnCopy_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(rtxtText.SelectedText);
}
private void tbtnPaste_Click(object sender, EventArgs e)
{
Paste();
}
private void cboFont_SelectedIndexChanged(object sender, EventArgs e)
{
rtxtText.SelectionFont = new Font(cboFont.Text, rtxtText.SelectionFont.Size);
}
private void cboSize_SelectedIndexChanged(object sender, EventArgs e)
{
float dSize = 0;
dSize = Convert.ToSingle(cboSize.Text);
rtxtText.SelectionFont = new Font(rtxtText.SelectionFont.Name, dSize);
}
private void tsmiOpen_Click(object sender, EventArgs e)
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.InitialDirectory = "c:\\";
dlgOpen.Filter = "所有文件(*.*)|*.*|文本文件(*.txt)|*.txt|" + "RTF格式(*.RTF)|*.rtf";
dlgOpen.FilterIndex = 1;
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
rtxtText.LoadFile(dlgOpen.FileName, RichTextBoxStreamType.PlainText);
}
}
private void tsmiSave_Click(object sender, EventArgs e)
{
SaveFileDialog dlgSave = new SaveFileDialog();
dlgSave.Filter = "所有文件(*.*)|*.*|文本文件(*.txt)|*.txt|" + "RTF格式(*.RTF)|*.rtf";
dlgSave.FilterIndex = 2;
if (dlgSave.ShowDialog() == DialogResult.OK)
{
rtxtText.SaveFile(dlgSave.FileName, RichTextBoxStreamType.PlainText);
}
}
private void rtxtText_MouseMove(object sender, MouseEventArgs e)
{
tsslMousePosition.Text = "位置 X:" + e.X.ToString() + "Y:" + e.Y.ToString();
}
}