using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//新建一个Windows窗体应用程序,并命名为“Notepad”,选择 模版 → Visual C# → windows→ //windows窗体应用程序 在下面的名称写Notepad
namespace 记事本
{
public partial class Form1 : Form
{
bool b = false;//b=false表示文件是新建的,true表示从磁盘打开的
bool s = true;//s=true表示文件是已经被保存了,false表示文件没有被保存
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void saveFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void rtxtNotepad_TextChanged(object sender, EventArgs e)
{
s = false;//文件被修改之后,没有被保存
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 判断当前文件是否从磁盘打开,或者新建时文档不为空,并且文件未被保存
if (b == true || rtxtNotepad.Text.Trim() != "")
{
// 若文件未保存
if (s == false)
{
string result;
result = MessageBox.Show("文件尚未保存,是否保存?",
"保存文件", MessageBoxButtons.YesNoCancel).ToString();
switch (result)
{
case "Yes":
// 若文件是从磁盘打开的
if (b == true)
{
// 按文件打开的路径保存文件
rtxtNotepad.SaveFile(openFileDialog1.FileName);
}
// 若文件不是从磁盘打开的
else if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
rtxtNotepad.SaveFile(saveFileDialog1.FileName);
}
s = true;
rtxtNotepad.Text = "";
break;
case "No":
b = false;
rtxtNotepad.Text = "";
break;
}
}
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (b == true || rtxtNotepad.Text.Trim() != "")
{
if (s == false)
{
string result;
result = MessageBox.Show("文件尚未保存,是否保存?",
"保存文件", MessageBoxButtons.YesNoCancel).ToString();
switch (result)
{
case "Yes":
if (b == true)
{
rtxtNotepad.SaveFile(openFileDialog1.FileName);
}
else if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
rtxtNotepad.SaveFile(saveFileDialog1.FileName);
}
s = true;
break;
case "No":
b = false;
rtxtNotepad.Text = "";
break;
}
}
}
openFileDialog1.RestoreDirectory = true;
if ((openFileDialog1.ShowDialog() == DialogResult.OK) && openFileDialog1.FileName != "")
{
rtxtNotepad.LoadFile(openFileDialog1.FileName);//打开代码语句
b = true;
}
s = true;
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (b == true && rtxtNotepad.Modified == true)
{
rtxtNotepad.SaveFile(openFileDialog1.FileName);
s = true;
}
else if (b == false && rtxtNotepad.Text.Trim() != "" &&
saveFileDialog1.ShowDialog() == DialogResult.OK)
{
rtxtNotepad.SaveFile(saveFileDialog1.FileName);//保存语句
s = true;
b = true;
openFileDialog1.FileName = saveFileDialog1.FileName;
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
rtxtNotepad.SaveFile(saveFileDialog1.FileName);
s = true;
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();//程序结束
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
rtxtNotepad.Undo();//撤销
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
rtxtNotepad.Copy();//复制
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
rtxtNotepad.Cut();//剪切
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
rtxtNotepad.Paste();//粘贴
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
rtxtNotepad.SelectAll();//全选
}
private void 日期ToolStripMenuItem_Click(object sender, EventArgs e)
{
rtxtNotepad.AppendText(System.DateTime.Now.ToString());//显示当前日期
}
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (自动换行ToolStripMenuItem.Checked == false)
{
自动换行ToolStripMenuItem.Checked = true; // 选中该菜单项
rtxtNotepad.WordWrap = true; // 设置为自动换行
}
else
{
自动换行ToolStripMenuItem.Checked = false;
rtxtNotepad.WordWrap = false;
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
fdlgNotepad.ShowColor = true;
if (fdlgNotepad.ShowDialog() == DialogResult.OK)
{
rtxtNotepad.SelectionColor = fdlgNotepad.Color;
rtxtNotepad.SelectionFont = fdlgNotepad.Font;
}
}
private void 工具栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
Point point;
if (工具栏ToolStripMenuItem.Checked == true)
{
// 隐藏工具栏时,把坐标设为(0,24),因为菜单的高度为24
point = new Point(0, 24);
工具栏ToolStripMenuItem.Checked = false;
tlsNotepad.Visible = false;
// 设置多格式文本框左上角位置
rtxtNotepad.Location = point;
// 隐藏工具栏后,增加文本框高度
rtxtNotepad.Height += tlsNotepad.Height;
}
else
{
/* 显示工具栏时,多格式文本框左上角位置的位置为(0,49),
因为工具栏的高度为25,加上菜单的高度24后为49 */
point = new Point(0, 49);
工具栏ToolStripMenuItem.Checked = true;
tlsNotepad.Visible = true;
rtxtNotepad.Location = point;
rtxtNotepad.Height -= tlsNotepad.Height;
}
}
private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (状态栏ToolStripMenuItem.Checked == true)
{
状态栏ToolStripMenuItem.Checked = false;
stsNotepad.Visible = false;
rtxtNotepad.Height += stsNotepad.Height;
}
else
{
状态栏ToolStripMenuItem.Checked = true;
stsNotepad.Visible = true;
rtxtNotepad.Height -= stsNotepad.Height;
}
}
}
}
运行之后的记事本界面为
仓库地址:
MY PROGRAM_new: My first program (gitee.com)