📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SimpleTextEditor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Load some default text.
richTextBox1.Rtf = @"{\rtf1\ansi \b This\b0 is a \ul RichTextBox\ul0. \i Try\i0 some \b formatting\b0! Also try the right-click Context Menu.}";
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
manipulateFile(((ToolStripItem)e.ClickedItem).Text);
}
private void toolStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
formatText(((ToolStripItem)e.ClickedItem).Text);
}
private void fileMenuItem_Click(object sender, EventArgs e)
{
manipulateFile(((ToolStripMenuItem)sender).Text);
}
private void formatMenuItem_Click(object sender, EventArgs e)
{
formatText(((ToolStripMenuItem)sender).Text);
}
private void formatText(string menuItemText)
{
Font fontOfSelectedText = this.richTextBox1.SelectionFont;
FontStyle styleApplied;
switch (menuItemText)
{
case "粗体":
if (this.richTextBox1.SelectionFont.Bold == true)
styleApplied = FontStyle.Regular;
else
styleApplied = FontStyle.Bold;
break;
case "斜体":
if (this.richTextBox1.SelectionFont.Italic == true)
styleApplied = FontStyle.Regular;
else
styleApplied = FontStyle.Italic;
break;
case "下划线":
if (this.richTextBox1.SelectionFont.Italic == true)
styleApplied = FontStyle.Regular;
else
styleApplied = FontStyle.Underline;
break;
default:
if (this.richTextBox1.SelectionFont.Underline == true)
styleApplied = FontStyle.Regular;
else
styleApplied = FontStyle.Underline;
break;
}
Font FontToApply = new Font(fontOfSelectedText, styleApplied);
this.richTextBox1.SelectionFont = FontToApply;
}
private void manipulateFile(string menuItemText)
{
switch (menuItemText)
{
case "新建":
// Simulate creating a new document by merely clearing the existing text.
richTextBox1.Text = "";
richTextBox1.Focus();
break;
case "打开":
if (this.openFileDialog.ShowDialog() == DialogResult.OK)
{
FileOpen();
}
break;
case "保存":
if (this.saveFileDialog.ShowDialog() == DialogResult.OK)
{
FileSave();
}
break;
default:
this.Close();
this.Dispose();
break;
}
}
private void FileOpen()
{
string strFileName = this.openFileDialog.FileName;
FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(54936));
try
{
this.richTextBox1.Rtf = sr.ReadToEnd();
}
catch (System.IO.FileNotFoundException eNotFound)
{
MessageBox.Show("没有找到帮助文件,请确认您的安装包." + eNotFound.Message.ToString(), "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
MessageBox.Show("未知错误,请联系开发人员. \n" + e.Message.ToString());
}
finally
{
sr.Close();
fs.Close();
}
}
private void FileSave()
{
string strFileName = this.saveFileDialog.FileName;
FileStream fs = new FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(54936));
try
{
sw.Write(this.richTextBox1.Rtf);
}
catch (System.IO.FileNotFoundException eNotFound)
{
MessageBox.Show("没有找到帮助文件,请确认您的安装包." + eNotFound.Message.ToString(), "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
MessageBox.Show("未知错误,请联系开发人员. \n" + e.Message.ToString());
}
finally
{
sw.Close();
fs.Close();
}
}
private void ToolStripMenuItemContextClick(object sender, EventArgs e)
{
formatText(((ToolStripMenuItem)sender).Text);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -