给你的MDI程序加上一个标签栏,方便地切换和关闭子窗体
来源:互联网 发布:易语言鼠标连点器源码 编辑:程序博客网 时间:2024/04/30 03:03
首先创建一个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);
- 给你的MDI程序加上一个标签栏,方便地切换和关闭子窗体
- 给你的MDI程序加上一个标签栏,方便地切换和关闭子窗体
- 给你的MDI程序加上一个标签栏
- 给你的MDI程序加上一个标签栏
- MFC中MDI程序切换子窗体
- 关闭MDI窗体子窗体
- C# 实现MDI子窗体只打开一个(打开新的窗口,关闭其他窗口)
- 屏蔽MDI子窗体的关闭铵钮
- 只允许建立一次子窗体的MDI程序
- MDI的子窗体问题
- Delphi MDI 子窗体的创建和销毁 [zhuan]
- DELPHI中MDI子窗口的关闭 和打开
- MDI 子窗体 最大化 无最大化最小化按钮 和无关闭按钮
- 如何动态地切换MDI子窗口或者在SDI主应用程序窗口显示的视图?
- VS.NET 2005(C#)的C/S程序中MDI子窗体控制其父窗体
- MDI窗体环境下只弹出一个子窗体
- MDI窗体内只显示一个子窗体
- 给你的C程序加上颜色
- 中国职员的九种劣根性
- 全面剖析XMLHttpRequest对象——XMLHttpRequest对象的方法
- 隐藏链接的虚线框
- 分页类
- 全面剖析XMLHttpRequest对象——发送请求
- 给你的MDI程序加上一个标签栏,方便地切换和关闭子窗体
- 用微软 IE 8 Beta 调试 JavaScript
- Symbian 的HelloWorld的详细解读
- 全面剖析XMLHttpRequest对象——处理请求
- XML:XSD 复合类型指示器
- PHP常用函数
- BI、ERP系统实施顺序问题
- myfaces+ajax4jsf+spring+jpa+chartCreate 配置备忘
- 易用性更强!Firefox3.0Beta4发布