本应用使用C#语言Windows窗体应用程序设计,是对Windows记事本部分简单功能的复现。
1.记事本界面及功能展示
2.界面设计过程
2.1 MenuStrip
从工具箱拖入Menustrip命名为menuStrip1
并在该menustrip下新建文件、新建、打开、保存、另存为、退出、编辑、撤销、剪切、复制、粘贴、删除、全选、日期、格式、自动换行、字体、查看、帮助、关于笔记本等上述ToolStripMenuItem。
Name属性依次更改为文件ToolStripMenuItem、新建ToolStripMenuItem、打开ToolStripMenuItem、保存ToolStripMenuItem、另存为ToolStripMenuItem、退出ToolStripMenuItem、编辑ToolStripMenuItem、撤销ToolStripMenuItem、剪切ToolStripMenuItem、复制ToolStripMenuItem、粘贴ToolStripMenuItem、删除ToolStripMenuItem、全选ToolStripMenuItem、日期ToolStripMenuItem、格式ToolStripMenuItem、自动换行ToolStripMenuItem、字体ToolStripMenuItem、查看ToolStripMenuItem、状态栏ToolStripMenuItem、帮助ToolStripMenuItem 、关于记事本ToolStripMenuItem,并将Text属性命名为上述汉字。
若存在快捷键,则在ShortcutKeys属性中设置相对应的快捷键,并将该ToolStripMenuItem的ShowShortcutKeys属性设定为True。
2.2 ToolStrip
从工具箱中拖入Toolstrip,并选择加入标准项,自动生成了如前文图中的图标,包含新建,保存,另存为,复制等item。
2.3 RichTextBox
从工具箱拖入RichTextBox,置于合适位置,即为该应用程序中输入文字的部分,可将Anchor属性设置为如下,可实现窗口放大缩小时该文本框的自适应。
2.4 StatusStrip
该控件对应上图部分,在该StatusStrip中加入statuslabel1和statuslabel2,将Text属性设置为就绪、显示日期/时间。
2.5 OpenFileDialog
从工具箱中拖入OpenFileDialog控件,默认命名为openFileDialog1,将其Filter属性设为“RTF文件|*.rtf|所有文件|*.*”,该控件对应该记事本程序的打开文件操作。
2.6 SaveFileDialog
从工具箱拖入SaveFileDialog控件,默认命名为saveFileDialog1,将其FileName属性改为“无标题”,Filter属性设为“RTF文件|*.rtf”,该控件对应记事本程序的保存操作。
2.7 FontDialog
从工具箱中拖入FontDialog控件,默认命名为fontDialog1,该控件对应记事本文字字体调整操作,自动包含如下图功能。
2.8 Timer
从工具箱中拖入Timer窗体中,Enabled属性设为“True”,Interval属性设为“1000”,表示1秒种触发一次Tick事件,即1秒钟改变一次时钟,对应状态栏中显示时间功能。
以上步骤完成了对该记事本程序的界面设计
3.代码编写
首先在该Form类中定义两个bool型变量,对应保存文件以及文字更改。
public bool issave = false;
public bool tchange = false;
3.1 RichTextBox代码
当向文字框内输入内容后,该段代码运行,含义为:文字已经change且未save。
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
tchange = true;
issave = false;
}
3.2 功能代码编写
3.2.1 文件菜单功能
在该Form类中定义一个string变量,对应文件名。
public string fname = "";
新建
private void isbsave()
{
if (issave == false && tchange == true)
{
if (MessageBox.Show("是否保存数据", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
save();
}
}
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
isbsave();
richTextBox1.ResetText();
issave = false;
tchange = false;
fname = "";
}
其中isbsasve()函数功能在于文字框中存在内容且未保存的情况下生成如下窗口
打开
打开键对应执行open函数,将fname修改为该文件名,将文件内容载入文本框中。
private void open()
{
openFileDialog1.Title = "打开";
openFileDialog1.FileName = "";
openFileDialog1.Filter = "文本文件|*.txt;";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fname = openFileDialog1.FileName;
richTextBox1.LoadFile(fname, RichTextBoxStreamType.PlainText);
issave = false;
tchange = false;
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
open();
}
保存
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
save();
}
private void save()
{
if (fname == "")
{
saveFileDialog1.Title = "保存";
this.saveFileDialog1.Filter = "文本文件|*.txt;";
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.FileName != "")
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
issave = true;
}
}
}
else
{
richTextBox1.SaveFile(fname, RichTextBoxStreamType.PlainText);
issave = true;
}
}
另存为
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
osave();
}
private void osave()
{
saveFileDialog1.Title = "另存为";
this.saveFileDialog1.Filter = "文本文件|*.txt;";
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.FileName != "")
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
issave = true;
}
}
}
退出
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
3.2.2 编辑菜单功能
该部分代码多为自带函数,较为简单,于一个代码框内放置。
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.SelectedText = "";
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
private void 日期ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
DateTime dt;
dt = DateTime.Now;
richTextBox1.AppendText(dt.Year.ToString() + "年 " + dt.Month.ToString() + "月 " + dt.Day.ToString() + "日" + dt.Hour.ToString() + "时 " + dt.Minute.ToString() + "分");
}
3.2.3 格式菜单功能
自动换行
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
textWordWrap();
}
private void textWordWrap()
{
if (自动换行ToolStripMenuItem.Checked = !自动换行ToolStripMenuItem.Checked)
{
richTextBox1.WordWrap = true;
}
else
{
richTextBox1.WordWrap = false;
}
}
字体
对之前拖入的FontDialog的控件的调用
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
richTextBox1.SelectionFont = fontDialog1.Font;
}
3.2.4 查看菜单功能
状态栏
private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
statusBar();
}
private void statusBar()
{
if (状态栏ToolStripMenuItem.Checked = !状态栏ToolStripMenuItem.Checked)
{
statusStrip1.Visible = true;
scriptstatus();
}
else
{
statusStrip1.Visible = false;
}
}
3.2.5 帮助菜单功能
关于记事本
使用一个新窗口显示记事本相关信息,该界面内容可自由编辑。
private void 关于记事本ToolStripMenuItem_Click(object sender ,EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
3.3 工具栏代码
此部分是工具栏的点击实现
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
int n;
n = toolStrip1.Items.IndexOf(e.ClickedItem);
switch(n)
{
case 0:
新建ToolStripMenuItem_Click(sender, e);
break;
case 1:
打开ToolStripMenuItem_Click(sender, e);
break;
case 2:
保存ToolStripMenuItem_Click(sender, e);
break;
case 3:
关于记事本ToolStripMenuItem_Click(sender, e);
break;
case 4:
剪切ToolStripMenuItem_Click(sender, e);
break;
case 5:
粘贴ToolStripMenuItem_Click(sender, e);
break;
case 7:
关于记事本ToolStripMenuItem_Click(sender, e);
break;
}
}