父窗体界面:
子窗体界面:
父窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace E
{
public partial class MdiMain : Form
{
public MdiMain()
{
InitializeComponent();
}
//以下为顶级菜单“文件”的菜单项
private void mnuNewFile_Click(object sender, EventArgs e)
{
int iCountNewFile = 0; //该变量统计MDI子窗口成员变量IsNewFile为true的MDI子窗体个数。
MdiChild myMdiChild;
for (int i = 0; i < this.MdiChildren.Length; i++) //此循环统计MDI子窗口成员变量IsNewFile为true的MDI子窗体个数。
{
myMdiChild = (MdiChild)this.MdiChildren[i];
if (myMdiChild.IsNewFile) iCountNewFile++;
}
iCountNewFile++;
myMdiChild = new MdiChild();
myMdiChild.MdiParent = this;
myMdiChild.Text = "新文件" + iCountNewFile;
myMdiChild.IsNewFile = true; //“新建”菜单项的MDI子窗体变量IsNewFile赋值为true。
myMdiChild.Show();
}
private void mnuOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog myOpenFileDialog = new OpenFileDialog();
myOpenFileDialog.Title = "打开rtf文件对话框";
myOpenFileDialog.Filter = "rft文件|*.rtf|所有文件|*.*";
if (myOpenFileDialog.ShowDialog() == DialogResult.OK)
{
MdiChild myMdiChild=new MdiChild();
myMdiChild.MdiParent = this;
myMdiChild.richTextBox1.LoadFile(myOpenFileDialog.FileName);
myMdiChild.Text = myOpenFileDialog.FileName;
myMdiChild.IsNewFile = false; //打开的MDI子窗体变量IsNewFile赋值为false。
myMdiChild.Show();
}
}
private void mnuExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//以上为顶级菜单“文件”的菜单项
//以下为顶级菜单“编辑”的菜单项
private void mnuUndo_Click(object sender, EventArgs e)
{
MdiChild myActiveMdiChild = (MdiChild)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.richTextBox1.Undo();//Undo()在TextBox里面就是撤销
}
}
private void mnuRedo_Click(object sender, EventArgs e)
{
MdiChild myActiveMdiChild = (MdiChild)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.richTextBox1.Redo();
}
}
private void mnuCut_Click(object sender, EventArgs e)
{
MdiChild myActiveMdiChild = (MdiChild)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
if (myActiveMdiChild.richTextBox1.SelectionLength > 0)
{
myActiveMdiChild.richTextBox1.Cut();
}
}
}
private void mnuCopy_Click(object sender, EventArgs e)
{
MdiChild myActiveMdiChild = (MdiChild)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
if (myActiveMdiChild.richTextBox1.SelectionLength > 0)
{
myActiveMdiChild.richTextBox1.Copy();
}
}
}
private void mnuPaste_Click(object sender, EventArgs e)
{
MdiChild myActiveMdiChild = (MdiChild)this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.richTextBox1.Paste();
}
}
private void mnuSelectAll_Click(object sender, EventArgs e)
{
MdiChild myActiveMdiChild =(MdiChild) this.ActiveMdiChild;
if (myActiveMdiChild != null)
{
myActiveMdiChild.richTextBox1.SelectAll();
}
}
//以上为顶级菜单“编辑”的菜单项
//以下为顶级菜单“窗口”的菜单项
private void mnuArrangeIcons_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.ArrangeIcons);//MdiLayout是方法
}
private void mnuCascade_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.Cascade);
}
private void mnuTileHorizontal_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void mnuTileVertical_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileVertical);
}
//关闭所有子窗口
private void mnuCloseAllChildForm_Click(object sender, EventArgs e)
{
foreach (MdiChild myMdiChild in this.MdiChildren) //this.MdiChildren是数组
{
myMdiChild.Close();
}
}
//以上为顶级菜单“窗口”菜单项
private void mnuAbout_Click(object sender, EventArgs e)
{
MessageBox.Show("这是一个基于RichTextBox的MDI文本编辑器例子。", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
子窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace E
{
public partial class MdiChild : Form
{
PrintDocument myPrintDocument;
SolidBrush myBrush;
public bool IsNewFile; //若该MDI子窗口为“新建”菜单项打开的窗口,则赋值为true;
//若该MDI子窗口为“打开”菜单项打开的窗口,则赋值为false。
public MdiChild()
{
InitializeComponent();
myPrintDocument = new PrintDocument();
myPrintDocument.PrintPage += myPrintDocument_PrintPage;
myBrush = new SolidBrush(Color.Black);
}
void myPrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
myBrush.Color = this.richTextBox1.SelectionColor;
e.Graphics.DrawString(this.richTextBox1.Text, this.richTextBox1.Font, myBrush, e.MarginBounds);
}
//这个设的挺烦的哈哈,因为这样每次都要关闭
private void MdiChild_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("你打算关闭窗口 \""+ this.Text+"\"吗?","关闭提示", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question)== DialogResult.OK) //this.text表示消息对话框显示的字符串中包含要关闭窗口的标题。
{
if (MessageBox.Show("你想保存文件 \"" + this.Text + "\" 吗?", "保存提示", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question) == DialogResult.OK)
SaveFile();
}
else
e.Cancel = true;
}
//以下为顶级菜单“文件”的菜单项
private void mnuSaveFile_Click(object sender, EventArgs e)
{
SaveFile();
}
void SaveFile() //直接保存分为两种情况,见下面的注释。
{
if (this.IsNewFile)
SaveAsFile(); //若为“新建”菜单项打开的窗口,则保存时使用保存文件对话框,即调用SaveAsFile()方法。
else
this.richTextBox1.SaveFile(this.Text); //若为“打开”菜单项打开的窗口,则保存时直接保存。
}
private void mnuSaveAsFile_Click(object sender, EventArgs e)
{
SaveAsFile(); //“另存”菜单项调用保存文件对话框,即调用SaveAsFile()方法。
}
void SaveAsFile()
{
SaveFileDialog mySaveFileDialog = new SaveFileDialog();
mySaveFileDialog.Title = "另存rtf文件为";
mySaveFileDialog.Filter = "写字板文件|*.rtf|所有文件|*.*";
mySaveFileDialog.CreatePrompt = true;
mySaveFileDialog.OverwritePrompt = true;
string strFileFullName = this.Text, strPathName, strFileName;
int k = strFileFullName.LastIndexOf('\\'); //倒序匹配,返回匹配第一项的下标。
strPathName = strFileFullName.Substring(0, k + 1); //存放路径
strFileName = strFileFullName.Substring(k + 1); //存放文件名
mySaveFileDialog.FileName = strFileName;
if (mySaveFileDialog.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.SaveFile(mySaveFileDialog.FileName);
this.Text = mySaveFileDialog.FileName;
this.IsNewFile = false; //只要保存,变量IsNewFile就变成false。
}
}
private void 页面设置VToolStripMenuItem_Click(object sender, EventArgs e)
{
PageSetup();
}
private void PageSetup()
{
PageSetupDialog myPageSetupDialog = new PageSetupDialog();
myPageSetupDialog.Document = myPrintDocument;
myPageSetupDialog.PageSettings = myPrintDocument.DefaultPageSettings;
if (myPageSetupDialog.ShowDialog() == DialogResult.OK)
{
myPrintDocument.DefaultPageSettings = myPageSetupDialog.PageSettings;
}
}
private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintPreview();
}
private void PrintPreview()
{
PrintPreviewDialog myPrintPreviewDialog = new PrintPreviewDialog();
myPrintPreviewDialog.Document = myPrintDocument;
myPrintPreviewDialog.ShowDialog();
}
private void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
{
Print();
}
private void Print()
{
PrintDialog myPrintDialog = new PrintDialog();
myPrintDialog.Document = myPrintDocument;
if (myPrintDialog.ShowDialog() == DialogResult.OK)
{
myPrintDocument.Print();
}
}
private void mnuClose_Click(object sender, EventArgs e)
{
this.Close();
}
//以上为顶级菜单“文件”的菜单项
//以下为顶级菜单“设置”的菜单项
private void mnuSetupFont_Click(object sender, EventArgs e)
{
SetupFont();
}
private void SetupFont()
{
FontDialog myFontDialog = new FontDialog();
myFontDialog.ShowEffects = true;
myFontDialog.ShowColor = true;
myFontDialog.ShowApply = true;
myFontDialog.Apply += MyFontDialog_Apply;
myFontDialog.Font = this.richTextBox1.SelectionFont;
myFontDialog.Color = this.richTextBox1.SelectionColor;
if (myFontDialog.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.SelectionFont = myFontDialog.Font;
this.richTextBox1.SelectionColor = myFontDialog.Color;
}
}
private void MyFontDialog_Apply(object sender, EventArgs e)
{
this.richTextBox1.SelectionFont = ((FontDialog)sender).Font;
this.richTextBox1.SelectionColor = ((FontDialog)sender).Color;
}
private void mnuSetupColor_Click(object sender, EventArgs e)
{
SetupColor();
}
private void SetupColor()
{
ColorDialog myColorDialog = new ColorDialog();
myColorDialog.AllowFullOpen = true;
myColorDialog.Color = this.richTextBox1.SelectionColor;
if (myColorDialog.ShowDialog() == DialogResult.OK)
{
this.richTextBox1.SelectionColor = myColorDialog.Color;
}
}
//以上为顶级菜单“设置”的菜单项
//以下为右键快捷菜单
private void cmnuCut_Click(object sender, EventArgs e)
{
if (this.richTextBox1.SelectionLength > 0)
this.richTextBox1.Cut();
}
private void cmnuCopy_Click(object sender, EventArgs e)
{
if (this.richTextBox1.SelectionLength > 0)
this.richTextBox1.Copy();
}
private void cmnuPaste_Click(object sender, EventArgs e)
{
this.richTextBox1.Paste();
}
private void cmnuSelectAll_Click(object sender, EventArgs e)
{
this.richTextBox1.SelectAll();
}
private void cmnuSetupFont_Click(object sender, EventArgs e)
{
SetupFont();
}
private void cmnuSetupColor_Click(object sender, EventArgs e)
{
SetupColor();
}
private void 页面设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
PageSetup();
}
private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintPreview();
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
Print();
}
//以上为右键快捷菜单
}
}
实现的功能可以通过代码来看,大致就是如下功能:
父窗体:
子窗体:
然后在打开新文件之后,子窗体的文件菜单条会自动合并到父窗体的文件中。
因为不是系统的,所以可能没有办法给大家进行一个详细的过程分析,但程序最主要的就是代码,相信大家能够在代码中知道实现了什么功能。为了大家方便,我也直接上传了源程序。
共勉!