首先创建一个MDI子窗口的基类
using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace ECHaierHR.Common
{
/// <summary>
/// MDIChild 的摘要说明。
/// </summary>
public class MDIChild : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null ;
private TabControl tabCtrl;
private TabPage tabPag;
public MDIChild()
{
//
// 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()
{
//
// MDIChild
//
this .AutoScaleBaseSize = new System.Drawing.Size( 6 , 14 );
this .ClientSize = new System.Drawing.Size( 292 , 273 );
this .Name = " MDIChild " ;
this .Text = " MDIChild " ;
this .Closing += new System.ComponentModel.CancelEventHandler( this .MDIChild_Closing);
this .Activated += new System.EventHandler( this .MDIChild_Activated);
}
#endregion
private void MDIChild_Closing( object sender, System.ComponentModel.CancelEventArgs e)
{
// Destroy the corresponding Tabpage when closing MDI child form
if ( this .tabPag != null )
this .tabPag.Dispose();
// If no Tabpage left
if ( this .tabCtrl != null && ! tabCtrl.HasChildren)
{
tabCtrl.Visible = false ;
}
}
private void MDIChild_Activated( object sender, System.EventArgs e)
{
// Activate the corresponding Tabpage
tabCtrl.SelectedTab = tabPag;
if ( ! tabCtrl.Visible)
{
tabCtrl.Visible = true ;
}
}
public TabControl TabCtrl
{
set { this .tabCtrl = value; }
}
public TabPage TabPag
{
get { return this .tabPag; }
set { this .tabPag = value; }
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace ECHaierHR.Common
{
/// <summary>
/// MDIChild 的摘要说明。
/// </summary>
public class MDIChild : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null ;
private TabControl tabCtrl;
private TabPage tabPag;
public MDIChild()
{
//
// 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()
{
//
// MDIChild
//
this .AutoScaleBaseSize = new System.Drawing.Size( 6 , 14 );
this .ClientSize = new System.Drawing.Size( 292 , 273 );
this .Name = " MDIChild " ;
this .Text = " MDIChild " ;
this .Closing += new System.ComponentModel.CancelEventHandler( this .MDIChild_Closing);
this .Activated += new System.EventHandler( this .MDIChild_Activated);
}
#endregion
private void MDIChild_Closing( object sender, System.ComponentModel.CancelEventArgs e)
{
// Destroy the corresponding Tabpage when closing MDI child form
if ( this .tabPag != null )
this .tabPag.Dispose();
// If no Tabpage left
if ( this .tabCtrl != null && ! tabCtrl.HasChildren)
{
tabCtrl.Visible = false ;
}
}
private void MDIChild_Activated( object sender, System.EventArgs e)
{
// Activate the corresponding Tabpage
tabCtrl.SelectedTab = tabPag;
if ( ! tabCtrl.Visible)
{
tabCtrl.Visible = true ;
}
}
public TabControl TabCtrl
{
set { this .tabCtrl = value; }
}
public TabPage TabPag
{
get { return this .tabPag; }
set { this .tabPag = value; }
}
}
}
然后,在MDI的主窗体中拖上一个TabControl,加入一下代码
#region
把MDI窗口与TabControl关联并加入快捷菜单
private void tabControl1_SelectedIndexChanged( object sender, System.EventArgs e)
{
foreach (MDIChild childForm in this .MdiChildren)
{
// Check for its corresponding MDI child form
if (childForm.TabPag.Equals(tabControl1.SelectedTab))
{
// Activate the MDI child form
childForm.Select();
}
}
}
private void AddMDIChildToTabCtrl(MDIChild frmChild)
{
frmChild.MdiParent = this ;
// child Form will now hold a reference value to the tab control
frmChild.TabCtrl = tabControl1;
// Add a Tabpage and enables it
TabPage tp = new TabPage();
tp.Parent = tabControl1;
tp.Text = frmChild.Text;
tp.Show();
// child Form will now hold a reference value to a tabpage
frmChild.TabPag = tp;
frmChild.Show();
}
private void ctxmnuClose_Click( object sender, System.EventArgs e)
{
if ( this .tabControl1.Visible)
{
MDIChild childForm = (MDIChild) this .ActiveMdiChild;
childForm.Close();
}
}
private void tabControl1_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Rectangle rct;
for ( int i = 0 ; i < tabControl1.TabPages.Count; i ++ )
{
rct = tabControl1.GetTabRect(i);
if (rct.Contains(e.X, e.Y))
{
// tabControl1.SelectedIndex = i;
tabControl1.SelectedTab = tabControl1.TabPages[i];
break ;
}
}
}
}
#endregion
private void tabControl1_SelectedIndexChanged( object sender, System.EventArgs e)
{
foreach (MDIChild childForm in this .MdiChildren)
{
// Check for its corresponding MDI child form
if (childForm.TabPag.Equals(tabControl1.SelectedTab))
{
// Activate the MDI child form
childForm.Select();
}
}
}
private void AddMDIChildToTabCtrl(MDIChild frmChild)
{
frmChild.MdiParent = this ;
// child Form will now hold a reference value to the tab control
frmChild.TabCtrl = tabControl1;
// Add a Tabpage and enables it
TabPage tp = new TabPage();
tp.Parent = tabControl1;
tp.Text = frmChild.Text;
tp.Show();
// child Form will now hold a reference value to a tabpage
frmChild.TabPag = tp;
frmChild.Show();
}
private void ctxmnuClose_Click( object sender, System.EventArgs e)
{
if ( this .tabControl1.Visible)
{
MDIChild childForm = (MDIChild) this .ActiveMdiChild;
childForm.Close();
}
}
private void tabControl1_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Rectangle rct;
for ( int i = 0 ; i < tabControl1.TabPages.Count; i ++ )
{
rct = tabControl1.GetTabRect(i);
if (rct.Contains(e.X, e.Y))
{
// tabControl1.SelectedIndex = i;
tabControl1.SelectedTab = tabControl1.TabPages[i];
break ;
}
}
}
}
#endregion
最后,在创建MDI窗口对象时,把它放进TabControl就行了
Employee emp
=
new
Employee();
this .AddMDIChildToTabCtrl(emp);
this .AddMDIChildToTabCtrl(emp);