MDI (Multiple Document Interface) 窗体被称为多文档窗体,它是很多 Windows 应用程序中常用的界面设计。
在一个窗体中打开另一个窗体的方式可以通过设置 MDI 窗体的方式实现。
MDI 窗体的设置并不复杂,只需要将窗体的属性 IsMdiContainer 设置为 True 即可。
在窗体加载事件 Load 中设置窗体为 MDI 窗体,代码如下。
this.IsMdiContainer = True;
在 MDI 窗体中,弹出窗体的代码与直接弹出窗体有些不同,
在使用 Show 方法显示窗体前需要使用窗体的 MdiParent 设置显示当前窗体的父窗体,实现的代码如下:
Test t = new Test();
t.MdiParent = this; //this:当前窗体
t.Show();
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 WindowsForm
{
public partial class MDI : Form
{
public MDI()
{
InitializeComponent();
//MDI窗体的设置:将窗体的属性IsMdiContainer设置为True
this.IsMdiContainer = true;
}
//打开文件菜单项的单击事件
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
//MDI弹出窗体:需要使用窗体的MdiParent设置当前窗体的父窗体
OpenFile file = new OpenFile();
file.MdiParent = this;
file.Show();
}
//保存文件菜单项单击事件
private void 保存文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFile save = new SaveFile();
save.MdiParent = this;
save.Show();
}
}
}