1、系统概述
本实验的主要内容是设计开发一个多文档文本编辑器。在文本编辑器、图像处理器这样的应用软件中,通常需要同时处理一个或多个文档,每个文档独立地执行软件所需要的功能。这种需要在一个窗体中同时包含多个子窗体的应用程序通常称为多文档(MDI)应用程序,子窗体之间可以进行数据交互,也可以互不相干。
2、系统设计
2.1设计目标
设计开发一个多文档文本编辑器
2.2 设计分析与算法流程
2.3 界面设计
2.4 关键类图
2.5 系统实现(运行调试)
3、系统扩展
在多文档文本编辑器SimpleMDIExample开发的基础上,从不同的角度进一步拓展项目的功能和使用范围,本实验中我拓展了的功能
4、总结
通过该项目的开发,使我进一步了解多文档(MDI)应用程序 ,为以后的学习坚实的基础。
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Web;
using System.Drawing.Text;
//using System.Web.UI;
//using System.Web.UI.WebControls.FontSize;
namespace SimpleMDIExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int _num = 1;
private void Form1_Load(object sender, EventArgs e)
{
tSCbBoxFontName.Items.Clear(); //获取系统所有字体,并将字体名称显示在下拉框中
InstalledFontCollection ifc = new InstalledFontCollection();
FontFamily[] ffs = ifc.Families;
foreach (FontFamily ff in ffs)
tSCbBoxFontName.Items.Add(ff.GetName(1));
LayoutMdi(MdiLayout.Cascade);
Text = "多文档文本编辑器";
WindowState = FormWindowState.Maximized;
FontSizeBox.Items.Clear();
for (float i = 1; i < 100; i = i + (float)0.5)
{
FontSizeBox.Items.Add(i);
}
//for (float i = 1; i < 100; i = i + (float)0.5)
//{
// FontSizeBox.Items.Add(i);
//}
// foreach
//(System.Drawing.FontFamily i in objFont.Families)
//{
// cboFont.Items.Add(i.Name.ToString());
//}
//cboFont.SelectedIndex = 0;
}
void Update()
{
string fonttype = "Arial";
int size = 1;
if (FontSizeBox.SelectedIndex > 0)
{
fonttype = FontSizeBox.SelectedItem.ToString();
}
if (FontSizeBox.SelectedIndex > 0)
{
size = Convert.ToInt32(FontSizeBox.SelectedItem);
}
Font f = new Font(fonttype, size);
this.FontSizeBox.Font = f;
}
private System.Drawing.Text.InstalledFontCollection
objFont = new System.Drawing.Text.InstalledFontCollection();
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
System.Drawing.Font objFonts = new
Font(FontSizeBox.Items[e.Index].ToString(), 14);
e.ItemHeight = objFonts.Height;
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
System.Drawing.Font objFonts = new
Font(FontSizeBox.Items[e.Index].ToString(), 14);
e.DrawBackground();
e.Graphics.DrawString(FontSizeBox.Items[e.Index].ToString(), objFonts, new
SolidBrush(e.ForeColor), new Point(e.Bounds.Left, e.Bounds.Top));
}
private void cboFont_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void 窗口层叠ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.Cascade);
this.窗口层叠ToolStripMenuItem.Checked = true;
this.垂直平铺ToolStripMenuItem.Checked = false;
this.水平平铺ToolStripMenuItem.Checked = false;
}
private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
this.窗口层叠ToolStripMenuItem.Checked = false;
this.垂直平铺ToolStripMenuItem.Checked = false;
this.水平平铺ToolStripMenuItem.Checked = true;
}
private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
this.窗口层叠ToolStripMenuItem.Checked = false;
this.垂直平铺ToolStripMenuItem.Checked = true;
this.水平平铺ToolStripMenuItem.Checked = false;
}
private void NewDoc()//新建文档,由于后面多处用到,所以写一个方法便于调用
{
FrmDoc fd = new FrmDoc();
fd.MdiParent = this;
fd.Text = "文档" + _Num;
fd.WindowState = FormWindowState.Maximized;
fd.Show();
fd.Activate();
_Num++;
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
NewDoc();
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "RTF格式(*.rtf)|*.rtf|文本文件(*.txt)|*.txt|所以文件(*.*)|*.*";
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
NewDoc();
_Num--;
if (openFileDialog1.FilterIndex == 1)
((FrmDoc)this.ActiveMdiChild).rTBDoc.LoadFile
(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
else
((FrmDoc)this.ActiveMdiChild).rTBDoc.LoadFile
(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
((FrmDoc)this.ActiveMdiChild).Text = openFileDialog1.FileName;
}
catch
{
MessageBox.Show("打开失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
openFileDialog1.Dispose();
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Count() > 0)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "RTF格式(*.rtf)|*.rtf|文本文件(*.txt)|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if (saveFileDialog1.FilterIndex == 1)
((FrmDoc)this.ActiveMdiChild).rTBDoc.SaveFile
(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
else
((FrmDoc)this.ActiveMdiChild).rTBDoc.SaveFile
(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
MessageBox.Show("保存成功!", " ", MessageBoxButtons.OK,
MessageBoxIcon.None);
}
catch
{
MessageBox.Show("保存失败!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
saveFileDialog1.Dispose();
}
}
private void 关闭CToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定要关闭当前文档吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "RTF格式(*.rtf)|*.rtf|文本文件(*.txt)|*.txt";
((FrmDoc)this.ActiveMdiChild).Close();
}
}
private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定退出应用程序吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
foreach (FrmDoc fd in this.MdiChildren)
fd.Close();
Application.Exit();
}
}
private void tSCbBoxFontName_Click(object sender, EventArgs e)
{
// if (this.MdiChildren.Count() > 0)
// {
// RichTextBox tempRTB = new RichTextBox();
// int RtbStart = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionStart;
//int len = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionLength;
//int tempRtbStart = 0;
// Font font = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionFont;
// if (len <= 0 && null != font)
// {
// ((FrmDoc)this.ActiveMdiChild).rTBDoc.Font = new Font(tSCbBoxFontName.Text, font.Size, font.Style);
// return;
// }
// tempRTB.Rtf = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedRtf;
//for (int i = 0; i < len; i++)
//{//
// tempRTB.Select(tempRtbStart + i, 1);
//tempRTB.SelectionFont = new Font(tSCbBoxFontName.Text,
// tempRTB.SelectionFont.Size, tempRTB.SelectionFont.Style);
// }
// tempRTB.Select(tempRtbStart, len);
// ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedRtf = tempRTB.SelectedRtf;
// ((FrmDoc)this.ActiveMdiChild).rTBDoc.Select(RtbStart, len);
// ((FrmDoc)this.ActiveMdiChild).rTBDoc.Focus();
// tempRTB.Dispose();
//}
}
private void ChangeRTBFontStyle(RichTextBox rtb, FontStyle style)
{
if (style != FontStyle.Bold && style != FontStyle.Italic && style != FontStyle.Underline)
throw new System.InvalidProgramException("字体格式错误");
RichTextBox tempRTB = new RichTextBox();
int curRtbStart = rtb.SelectionStart;
int len = rtb.SelectionLength;
int tempRtbStart = 0;
Font font = rtb.SelectionFont;
if (len <= 0 && font != null)
{
if (style == FontStyle.Bold && font.Bold || style == FontStyle.Italic
&& font.Italic || style == FontStyle.Underline && font.Underline)
rtb.Font = new Font(font, font.Style ^ style);
else
if (style == FontStyle.Bold && font.Bold || style == FontStyle.Italic
&& !font.Italic || style == FontStyle.Underline && !font.Underline)
rtb.Font = new Font(font, font.Style | style);
return;
}
tempRTB.Rtf = rtb.SelectedRtf;
tempRTB.Select(len - 1, 1);
Font tempFont = (Font)tempRTB.SelectionFont.Clone();
for (int i = 0; i < len; i++)
{
tempRTB.Select(tempRtbStart + i, 1);
if (style == FontStyle.Bold && tempFont.Bold
style == FontStyle.Italic && tempFont.Italic
style == FontStyle.Underline && tempFont.Underline)
tempRTB.SelectionFont = new Font(tempRTB.SelectionFont,
tempRTB.SelectionFont.Style ^ style);
else
if (style == FontStyle.Bold && !tempFont.Bold
style == FontStyle.Italic && !tempFont.Italic
style == FontStyle.Underline && !tempFont.Underline)
tempRTB.SelectionFont = new Font(tempRTB.SelectionFont,
tempRTB.SelectionFont.Style | style);
}
tempRTB.Select(tempRtbStart, len);
rtb.SelectedRtf = tempRTB.SelectedRtf;
rtb.Select(curRtbStart, len);
rtb.Focus();
tempRTB.Dispose();
}
private void 斜体ToolStripButton_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Count() > 0)
ChangeRTBFontStyle(((FrmDoc)this.ActiveMdiChild).rTBDoc,
FontStyle.Italic);
}
private void 粗体ToolStripButton_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Count() > 0)
ChangeRTBFontStyle(((FrmDoc)this.ActiveMdiChild).rTBDoc,
FontStyle.Bold);
}
private void 下划线ToolStripButton_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Count() > 0)
ChangeRTBFontStyle(((FrmDoc)this.ActiveMdiChild).rTBDoc, FontStyle.Underline);
}
private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Count() > 0)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "RTF格式(*.rtf)|*.rtf|文本文件(*.txt)|*.txt";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if (saveFileDialog1.FilterIndex == 1)
((FrmDoc)this.ActiveMdiChild).rTBDoc.SaveFile
(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);
else
((FrmDoc)this.ActiveMdiChild).rTBDoc.SaveFile
(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
MessageBox.Show("保存成功!", " ", MessageBoxButtons.OK,
MessageBoxIcon.None);
}
catch
{
MessageBox.Show("保存失败!", "错误", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
saveFileDialog1.Dispose();
}
}
private void 新建NToolStripButton_Click(object sender, EventArgs e)
{
NewDoc();
}
private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "RTF格式(*.rtf)|*.rtf|文本文件(*.txt)|*.txt|所以文件(*.*)|*.*";
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
NewDoc();
_Num--;
if (openFileDialog1.FilterIndex == 1)
((FrmDoc)this.ActiveMdiChild).rTBDoc.LoadFile
(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
else
((FrmDoc)this.ActiveMdiChild).rTBDoc.LoadFile
(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
((FrmDoc)this.ActiveMdiChild).Text = openFileDialog1.FileName;
}
catch
{
MessageBox.Show("打开失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
openFileDialog1.Dispose();
}
private void cboFont_SelectedIndexChanged_1(object sender, EventArgs e)
{
// rTBDoc.Font = cboFont.SelectedValue.ToString();
// ((FrmDoc)this.ActiveMdiChild).rTBDoc.Text = cboFont.SelectedValue.ToString();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 撤销toolStripButton_Click(object sender, EventArgs e)
{
//RichTextBox tempRTB = new RichTextBox();
//tempRTB.Redo();
((FrmDoc)this.ActiveMdiChild).rTBDoc.Undo();
}
private void 复制toolStripButton_Click(object sender, EventArgs e)
{
Clipboard.SetText(((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText);
}
private void 剪切toolStripButton_Click(object sender, EventArgs e)
{
Clipboard.SetText(((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText);
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText = "";
}
private void 粘贴toolStripButton_Click(object sender, EventArgs e)
{
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText = Clipboard.GetText();
}
private void cboFont_SelectedIndexChanged_2(object sender, EventArgs e)
{
// Update();
}
private void 字体颜色toolStripButton_Click(object sender, EventArgs e)
{
// ColorDialog MyDialog = new ColorDialog();
//MyDialog.AllowFullOpen = false;
//MyDialog.Color = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText.ForeColor;
//if (MyDialog.ShowDialog() == DialogResult.OK)
//((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText.ForeColor = MyDialog.Color;
}
private void 改变字体属性toolStripButton_Click(object sender, EventArgs e)
{
// ChangeFontSize((float)Convert.ToDouble(cboFont.Text));
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void ChangeFontSize(float fontSize)
{
if (fontSize <= 0.0)
throw new InvalidProgramException("字号参数错误");
int curRtbStart = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionStart;
int len = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionLength;
RichTextBox tempRichTextBox = new RichTextBox();
int tempRtbStart = 0;
Font font = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionFont;
if (len <= 1 && font != null)
{
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionFont = new Font(font.Name, fontSize, font.Style);
return;
}
tempRichTextBox.Rtf = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedRtf;
for (int i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1);
tempRichTextBox.SelectionFont = new Font(tempRichTextBox.SelectionFont.Name, fontSize, tempRichTextBox.SelectionFont.Style);
}
tempRichTextBox.Select(tempRtbStart, len);
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedRtf = tempRichTextBox.SelectedRtf;
((FrmDoc)this.ActiveMdiChild).rTBDoc.Select(curRtbStart, len);
((FrmDoc)this.ActiveMdiChild).rTBDoc.Focus();
}
private void FontSizeBox_TextChanged(object sender, EventArgs e)
{
ChangeFontSize(Math.Abs(Convert.ToInt32(FontSizeBox.SelectedIndex)));
}
private void 改变字体属性toolStripButton_Click_1(object sender, EventArgs e)
{
// ChangeFontSize((float)Convert.ToDouble(FontSizeBox.Text));
}
private void tSCbBoxFontName1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void 剪切ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Clipboard.SetText(((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText);
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText = "";
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetText(((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText);
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedText = Clipboard.GetText();
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
((FrmDoc)this.ActiveMdiChild).rTBDoc.Undo();
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void tSCbBoxFontName_Click_1(object sender, EventArgs e)
{
if (this.MdiChildren.Count() > 0)
{
RichTextBox tempRTB = new RichTextBox();
int RtbStart = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionStart;
int len = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionLength;
int tempRtbStart = 0;
Font font = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionFont;
if (len <= 0 && null != font)
{
((FrmDoc)this.ActiveMdiChild).rTBDoc.Font = new Font(tSCbBoxFontName.Text, font.Size, font.Style);
return;
}
tempRTB.Rtf = ((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedRtf;
for (int i = 0; i < len; i++)
{
tempRTB.Select(tempRtbStart + i, 1);
tempRTB.SelectionFont = new Font(tSCbBoxFontName.Text,
tempRTB.SelectionFont.Size, tempRTB.SelectionFont.Style);
}
tempRTB.Select(tempRtbStart, len);
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectedRtf = tempRTB.SelectedRtf;
((FrmDoc)this.ActiveMdiChild).rTBDoc.Select(RtbStart, len);
((FrmDoc)this.ActiveMdiChild).rTBDoc.Focus();
tempRTB.Dispose();
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog MyDialog = new ColorDialog();
MyDialog.AllowFullOpen = false;
MyDialog.Color = ((FrmDoc)this.ActiveMdiChild).rTBDoc.ForeColor;
if (MyDialog.ShowDialog() == DialogResult.OK)
((FrmDoc)this.ActiveMdiChild).rTBDoc.ForeColor = MyDialog.Color;
}
private void 居中对齐ToolStripButton_Click(object sender, EventArgs e)
{
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionAlignment = HorizontalAlignment.Center;
}
private void 左对齐ToolStripButton_Click(object sender, EventArgs e)
{
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionAlignment = HorizontalAlignment.Left;
}
private void 右对齐ToolStripButton_Click(object sender, EventArgs e)
{
((FrmDoc)this.ActiveMdiChild).rTBDoc.SelectionAlignment = HorizontalAlignment.Right;
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
}
}
具体的C#文件压缩包如下链接所示:
https://download.csdn.net/download/qq_41509200/11584074