1.MDI:Multiple document interface
2.当MDI应用程序启动时,首先会显示父窗体,所有的子窗体都在父窗体中打开,每个应用程序只能有一个父窗体,其他子窗体不能移除父窗体的框架区域
3.如果要将某个窗体设置为子窗体,首先必须含有一个父窗体(通过窗体的load事件用IsMdiContainer属性进行语句设定),然后在工程中新建Windows窗体,通过窗体的MdiParent属性来设置
例子代码
这个把Form1设置为父窗体,Form2、Form3、Form4为子窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
//以下form3、form4同理
Form3 form3 = new Form3();
form3.MdiParent = this;
form3.Show();
Form4 form4 = new Form4();
form4.MdiParent = this;
form4.Show();
}
}
}
运行结果
运行结果.PNG
当然了,你也可以设置子窗体的排列方式,c#提供MdiLayout枚举类型,可到网上查看,这里用一下TitleHorizontal
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
//以下form3、form4同理
Form3 form3 = new Form3();
form3.MdiParent = this;
form3.Show();
Form4 form4 = new Form4();
form4.MdiParent = this;
form4.Show();
//设置窗体平铺
this.LayoutMdi(MdiLayout.TileHorizontal);
}
}
}
运行结果
运行结果.PNG