- 方法一:
主要用到“TreeView”控件、“Pannel”控件、“MainMenu”控件、“StatusBar”控件、“ImageList” 控件,新建Form1(主窗体)、Form2两个窗体,将Form1窗体的“IsMdiContainer”设为True,运行效果如下:
1. 右边子窗体有标题栏:

2. 右边子窗体无标题栏(设置temp_form.FormBorderStyle=FormBorderStyle.None;):

附:(Form1.cs代码)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MDIForm
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.ImageList imageList1;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.treeView1 = new System.Windows.Forms.TreeView();
this.panel1 = new System.Windows.Forms.Panel();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem4,
this.menuItem5});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2});
this.menuItem1.Text = "文件(&F)";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "退出(&X)";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "选项(&O)";
//
// menuItem5
//
this.menuItem5.Index = 2;
this.menuItem5.Text = "帮助(&H)";
//
// treeView1
//
this.treeView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.treeView1.Font = new System.Drawing.Font("宋体",
this.treeView1.ImageList = this.imageList1;
this.treeView1.ItemHeight = 14;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("业务类别", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("销售"),
new System.Windows.Forms.TreeNode("生产管理")}),
new System.Windows.Forms.TreeNode("其他", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("基本数据录入")}),
new System.Windows.Forms.TreeNode("信息", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("信息一览")})});
this.treeView1.ShowLines = false;
this.treeView1.Size = new System.Drawing.Size(184, 680);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(184, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(840, 680);
this.panel1.TabIndex = 3;
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 681);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(1016, 32);
this.statusBar1.TabIndex = 5;
this.statusBar1.Text = "当前打开窗体:";
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(1016, 713);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.treeView1);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "MDIForm";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void
{
Application.Run(new Form1());
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
System.String str = treeView1.SelectedNode.Text;//取得相应treeview结点的text值
bool remark=true;
for(int i=0; i<this.MdiChildren.Length;i++)
if(this.MdiChildren[i].Name.Equals( str )) //查看有没有相同的MDI子窗体
{
this.MdiChildren[i].Activate(); //子窗体已经被创立,激活它
remark=false;
break;
}
if(remark) // 末创立,建立子窗体
{
panel1.Controls.Clear();
Form2 temp_form = new Form2();
temp_form.TopLevel=false;
temp_form.Parent=panel1;
temp_form.Dock=DockStyle.Fill;
temp_form.Name=str;
temp_form.Text=str;
//设置子窗体无标题栏
//temp_form.FormBorderStyle=FormBorderStyle.None;
temp_form.WindowState=FormWindowState.Maximized;
temp_form.Show();
}
statusBar1.Text="当前打开窗体:"+e.Node.Text;
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
- 方法二:
主要用到“TreeView”控件、“Splitter”控件、“MainMenu”控件、“StatusBar”控件、“ImageList”控件、“ToolBar”控件,新建Form1(主窗体)、Form2、Form3 、Form4四个窗体,将Form1窗体的“IsMdiContainer”设为True,运行效果如下:
1. 子窗体非最大化显示:

2. 子窗体最大化显示:

附:(Form1.cs代码)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace MDIForm
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FrmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MenuItem menuItem11;
private System.Windows.Forms.MenuItem menuItem12;
private System.Windows.Forms.MenuItem menuItem13;
private System.Windows.Forms.MenuItem menuItem14;
private System.Windows.Forms.MenuItem menuItem15;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ToolBarButton toolBarButton4;
private System.Windows.Forms.ToolBarButton toolBarButton5;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem16;
private System.Windows.Forms.MenuItem menuItem17;
private System.Windows.Forms.MenuItem menuItem18;
private System.Windows.Forms.MenuItem menuItem19;
private System.Windows.Forms.MenuItem menuItem20;
private System.Windows.Forms.MenuItem menuItem21;
private System.Windows.Forms.MenuItem menuItem22;
private System.Windows.Forms.MenuItem menuItem23;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Splitter splitter1;
private System.ComponentModel.IContainer components;
//private Form3 _form3=new Form3();
public FrmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FrmMain));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem20 = new System.Windows.Forms.MenuItem();
this.menuItem22 = new System.Windows.Forms.MenuItem();
this.menuItem15 = new System.Windows.Forms.MenuItem();
this.menuItem11 = new System.Windows.Forms.MenuItem();
this.menuItem12 = new System.Windows.Forms.MenuItem();
this.menuItem21 = new System.Windows.Forms.MenuItem();
this.menuItem13 = new System.Windows.Forms.MenuItem();
this.menuItem14 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem23 = new System.Windows.Forms.MenuItem();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem16 = new System.Windows.Forms.MenuItem();
this.menuItem17 = new System.Windows.Forms.MenuItem();
this.menuItem18 = new System.Windows.Forms.MenuItem();
this.menuItem19 = new System.Windows.Forms.MenuItem();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
this.treeView1 = new System.Windows.Forms.TreeView();
this.splitter1 = new System.Windows.Forms.Splitter();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3,
this.menuItem4,
this.menuItem5});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem20,
this.menuItem22,
this.menuItem15,
this.menuItem11,
this.menuItem12,
this.menuItem21,
this.menuItem13,
this.menuItem14,
this.menuItem7,
this.menuItem6});
this.menuItem1.Text = "文件(&F)";
//
// menuItem20
//
this.menuItem20.Index = 0;
this.menuItem20.Text = "登录系统(&L)";
this.menuItem20.Click += new System.EventHandler(this.menuItem20_Click);
//
// menuItem22
//
this.menuItem22.Index = 1;
this.menuItem22.Text = "-";
//
// menuItem15
//
this.menuItem15.Index = 2;
this.menuItem15.Text = "新建业务(&N)";
this.menuItem15.Click += new System.EventHandler(this.menuItem15_Click);
//
// menuItem11
//
this.menuItem11.Index = 3;
this.menuItem11.Text = "打开业务(&O)";
//
// menuItem12
//
this.menuItem12.Index = 4;
this.menuItem12.Text = "保存业务(&S)";
//
// menuItem21
//
this.menuItem21.Index = 5;
this.menuItem21.Text = "-";
//
// menuItem13
//
this.menuItem13.Index = 6;
this.menuItem13.Text = "关闭当前(&C)";
this.menuItem13.Click += new System.EventHandler(this.menuItem13_Click);
//
// menuItem14
//
this.menuItem14.Index = 7;
this.menuItem14.Text = "关闭所有(&A)";
this.menuItem14.Click += new System.EventHandler(this.menuItem14_Click);
//
// menuItem7
//
this.menuItem7.Index = 8;
this.menuItem7.Text = "-";
//
// menuItem6
//
this.menuItem6.Index = 9;
this.menuItem6.Text = "退出系统(&E)";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "编辑(&E)";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "工具(&T)";
//
// menuItem4
//
this.menuItem4.Index = 3;
this.menuItem4.MdiList = true;
this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem9,
this.menuItem10});
this.menuItem4.Text = "窗口(&W)";
//
// menuItem9
//
this.menuItem9.Index = 0;
this.menuItem9.Text = "平铺";
this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
//
// menuItem10
//
this.menuItem10.Index = 1;
this.menuItem10.Text = "层叠";
this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
//
// menuItem5
//
this.menuItem5.Index = 4;
this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem23});
this.menuItem5.Text = "帮助(&H)";
//
// menuItem23
//
this.menuItem23.Index = 0;
this.menuItem23.Text = "关于(&A)";
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 371);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(632, 22);
this.statusBar1.TabIndex = 1;
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// toolBar1
//
this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3,
this.toolBarButton4,
this.toolBarButton5});
this.toolBar1.ButtonSize = new System.Drawing.Size(16, 16);
this.toolBar1.ContextMenu = this.contextMenu1;
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Location = new System.Drawing.Point(0, 0);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(632, 28);
this.toolBar1.TabIndex = 3;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButton1
//
this.toolBarButton1.ImageIndex = 0;
//
// toolBarButton2
//
this.toolBarButton2.DropDownMenu = this.contextMenu1;
this.toolBarButton2.ImageIndex = 1;
this.toolBarButton2.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
//
// contextMenu1
//
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem8,
this.menuItem16,
this.menuItem17,
this.menuItem18,
this.menuItem19});
//
// menuItem8
//
this.menuItem8.Index = 0;
this.menuItem8.Text = "工资管理";
//
// menuItem16
//
this.menuItem16.Index = 1;
this.menuItem16.Text = "考勤管理";
//
// menuItem17
//
this.menuItem17.Index = 2;
this.menuItem17.Text = "申请假期";
//
// menuItem18
//
this.menuItem18.Index = 3;
this.menuItem18.Text = "销售开票";
//
// menuItem19
//
this.menuItem19.Index = 4;
this.menuItem19.Text = "库存查询";
//
// toolBarButton3
//
this.toolBarButton3.ImageIndex = 2;
//
// toolBarButton4
//
this.toolBarButton4.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
this.treeView1.Font = new System.Drawing.Font("宋体",
this.treeView1.ImageIndex = 1;
this.treeView1.ImageList = this.imageList1;
this.treeView1.Location = new System.Drawing.Point(0, 28);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("业务类别", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("销售"),
new System.Windows.Forms.TreeNode("生产管理")}),
new System.Windows.Forms.TreeNode("其他", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("基本数据录入")}),
new System.Windows.Forms.TreeNode("信息", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("信息一览")})});
this.treeView1.Size = new System.Drawing.Size(176, 343);
this.treeView1.TabIndex = 5;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// splitter1
//
this.splitter1.Location = new System.Drawing.Point(176, 28);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(16, 343);
this.splitter1.TabIndex = 6;
this.splitter1.TabStop = false;
//
// FrmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(632, 393);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.statusBar1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "FrmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = " MDIForm";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.FrmMain_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void
{
bool createdNew;
Mutex m=new Mutex(true,"test",out createdNew);
if (createdNew)
{
Application.Run(new FrmMain());
m.ReleaseMutex();
}
else
{
MessageBox.Show("系统只允许运行一个实例!","系统提示");
}
}
private void menuItem6_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void menuItem9_Click(object sender, System.EventArgs e)
{
this.LayoutMdi(System.Windows.Forms.MdiLayout.TileHorizontal);
}
private void menuItem15_Click(object sender, System.EventArgs e)
{
Form3 child1 = new Form3();
child1.MdiParent = this;
child1.Show();
}
private void menuItem10_Click(object sender, System.EventArgs e)
{
this.LayoutMdi(System.Windows.Forms.MdiLayout.Cascade);
}
private void FrmMain_Load(object sender, System.EventArgs e)
{
menuItem2.Enabled= true;
menuItem3.Enabled=true;
menuItem4.Enabled=true;
toolBar1.Enabled = true;
}
private void menuItem13_Click(object sender, System.EventArgs e)
{
if (this.MdiChildren.Length>0)//如果当前有子窗口打开
{
this.ActiveMdiChild.Close();
}
}
private void menuItem14_Click(object sender, System.EventArgs e)
{
Form[] children = this.MdiChildren; // 获取当前打开的子窗体
for( int i=0;i<children.Length;i++)
children[i].Close();
}
private void menuItem20_Click(object sender, System.EventArgs e)
{
Form2 frm = new Form2();//创建一个登录窗口
frm.MdiParent=this;
frm.Show();//显示这个登录窗口
menuItem20.Enabled = false;
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button==this.toolBarButton1)
{
Form3 child1 = new Form3();
child1.MdiParent = this;
child1.Show();
}
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
System.String str = treeView1.SelectedNode.Text;//取得相应treeview结点的text值
bool remark=true;
for(int i=0; i<this.MdiChildren.Length;i++)
if(this.MdiChildren[i].Name.Equals( str )) //查看有没有相同的MDI子窗体
{
this.MdiChildren[i].Activate(); //子窗体已经被创立,激活它
remark=false;
break;
}
if(remark) // 末创立,建立子窗体
{
Form4 temp_form = new Form4();
temp_form.MdiParent=this;
temp_form.Name=str;
temp_form.Text=str;
temp_form.Show();
}
statusBar1.Text="当前打开窗体:"+treeView1.SelectedNode.Text;
}
}
}
浙公网安备 33010602011771号