目录
1 思路分析
2 准备工作
2.1 新建子窗体和父窗体
创建WinForm窗体应用程序,删除原生窗体,新建Parent和FrmChild窗体
2.2 设置Program.cs的启动窗体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace notpad
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Parent());
}
}
}
2.3 子窗体设计
2.3.1 添加控件
2.3.2 添加图标资源
2.3.3 工具栏的设计
(1)将图片资源导入到Image属性下:
(2)按钮属性设置
【打开】执行如下设置:
(3)combobox属性设置
(5)TextBox控件设置
(4)效果展示
3 子窗体设计源码及解析
3.1 窗体加载事件
private void FrmChild_Load(object sender, EventArgs e)
{
//窗体加载时,需要加载系统字体
InstalledFontCollection myFonts = new InstalledFontCollection();
//获取InstalledFontCollection对象的数组
FontFamily[] ff = myFonts.Families;
//获取数组的长度
int count = ff.Length;
//将系统字体添加到字体集合
for (int i = 0; i < count; i++)
{
string FontName = ff[i].Name;
//将读取的系统字体添加到combobox控件中
toolStripComboBoxFonts.Items.Add(FontName);
}
//设置窗体保存标记为空
toolStripLabelMake.Text = "";
}
3.2 子窗体新建按钮
private void toolStripButton1_Click(object sender, EventArgs e)
{
//清空当前文本
textBoxNote.Text = "";
//清空窗体标题
this.Text = "";
//清空是否保存标记
toolStripLabelMake.Text = "";
}
3.3 子窗体打开按钮
private void toolStripButtonOpen_Click(object sender, EventArgs e)
{
//创建打开文件对话框对象
OpenFileDialog ofd = new OpenFileDialog();
//设置文件类型过滤器
ofd.Filter = "文本文档(*.txt)|*.txt";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//保存文件到用户目录
string inName = ofd.FileName;
//将当前文件路径添加到标题位置
this.Text = inName;
StreamReader sr = new StreamReader(inName, Encoding.UTF8);//设置为通用的UTF8格式
//读取到文件的最后一行
textBoxNote.Text = sr.ReadToEnd();
//关闭文件
sr.Close();
//清空保存标记
toolStripLabelMake.Text = "";
}
3.4 保存按钮的实现
private void toolStripButtonSave_Click(object sender, EventArgs e)
{
//点击保存时,把是否保存标记为空
toolStripLabelMake.Text = "";
//首先判断是否有文本内容
if (textBoxNote.Text.Trim() != "")
{
//判断窗体标题是否为空,默认为空
if (this.Text == "")
{
//创建保存对话框对象
SaveFileDialog sfd = new SaveFileDialog();
//定义保存文件类型过滤器
sfd.Filter = "文本文件(*.txt)|*.txt";
//获取用户点击的按钮
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//保存文件到用户目录
string outName = sfd.FileName;
//创建写数据流对象
StreamWriter sw = new StreamWriter(outName, false);
//将数据写入到文件中
sw.WriteLine(textBoxNote.Text.Trim());
//刷新流
sw.Flush();
//关闭文件
sw.Close();
//将窗体标题设置为保存的路径名称
this.Text = outName;
}
}
else
{
StreamWriter sw = new StreamWriter(this.Text, false);
sw.WriteLine(textBoxNote.Text.Trim());
sw.Flush();
sw.Close();
}
}
else
{
MessageBox.Show("内容为空,不能保存","信息提示");
}
}
3.5 字体加粗的点击事件
//加粗按钮
private void toolStripButtonBold_Click(object sender, EventArgs e)
{
//点击按钮加粗,加粗时再点击按钮取消加粗
if (textBoxNote.Font.Bold == false)
{
textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Bold);//加粗
}
else
{
textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Regular);//普通文本
}
}
3.6 字体倾斜的点击事件
private void toolStripButtonItalic_Click(object sender, EventArgs e)
{
if (textBoxNote.Font.Italic == false)
{
textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Italic);//倾斜
}
else
{
textBoxNote.Font = new Font(textBoxNote.Font, FontStyle.Regular);//普通文本
}
}
3.7 字体控件的索引变更事件
//改变选择字体的索引值
private void toolStripComboBoxFonts_SelectedIndexChanged(object sender, EventArgs e)
{
string fontName = toolStripComboBoxFonts.Text;
textBoxNote.Font = new Font(fontName, float.Parse(toolStripComboBoxSize.Text));
}
3.8 字号控件的索引变更事件
private void toolStripComboBoxSize_SelectedIndexChanged(object sender, EventArgs e)
{
string fontName = toolStripComboBoxFonts.Text;
textBoxNote.Font = new Font(fontName, float.Parse(toolStripComboBoxSize.Text));
}
3.9 手动设置字号
private void toolStripComboBoxSize_TextChanged(object sender, EventArgs e)
{
string fontName = toolStripComboBoxSize.Text;
if (fontName == "")
{
fontName = "10";
}
else
{
textBoxNote.Font = new Font(fontName, float.Parse(toolStripComboBoxSize.Text));
}
}
3.10 文本发生变化监测事件
//标记文本是否发生修改
private void textBoxNote_TextChanged(object sender, EventArgs e)
{
toolStripLabelMake.Text = "*";
}
3.11 窗体关闭提示事件
private void FrmChild_FormClosing(object sender, FormClosingEventArgs e)
{
//判断记号是否为*
if (toolStripLabelMake.Text == "*")
{
DialogResult re = MessageBox.Show("文档尚未保存,确定要退出吗", "友情提示", MessageBoxButtons.YesNo);
if (re == System.Windows.Forms.DialogResult.Yes)
{
this.Close();
this.Dispose();
}
else
{
e.Cancel = true;
}
}
}
4 父窗体设计源码与实现
4.1 控件准备
拖入MenuStrip控件,添加【文件】菜单
4.2 新建--【打开子窗体】
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmChild child = new FrmChild();
child.MdiParent = this;
child.Show();
}
4.3 关闭--【关闭当前激活状态的子窗体】
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form frm = this.ActiveMdiChild;
frm.Close();
}
4.4 关闭全部--【遍历激活窗体依次关闭】
private void 关闭全部ToolStripMenuItem_Click(object sender, EventArgs e)
{
//获取父窗体中包含的子窗体数组
foreach (Form frm in this.MdiChildren)
{
//遍历到激活窗体,关闭之
Form form = this.ActiveMdiChild;
form.Close();
}
}
4.5 退出
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}