给MDI程序加上标签栏之新解-感谢沐枫!
去掉了原有基类,所有功能都在主窗体中完成,拖入一个TabControl,加入以下代码:
MainFormMDI_MdiChildActivate#region MainFormMDI_MdiChildActivate
private void MainFormMDI_MdiChildActivate(object sender, System.EventArgs e)
{
if (this.ActiveMdiChild != null)
{
this.tabControl1.Visible = true;
for (int i = 0; i < this.tabControl1.TabCount; i++)
{
if(this.ActiveMdiChild.Equals(tabControl1.TabPages[i].Tag))
{
this.tabControl1.SelectedTab = this.tabControl1.TabPages[i];
break;
}
}
}
else
{
this.tabControl1.Visible = false;
}
}
#endregion
private void MainFormMDI_MdiChildActivate(object sender, System.EventArgs e)
{
if (this.ActiveMdiChild != null)
{
this.tabControl1.Visible = true;
for (int i = 0; i < this.tabControl1.TabCount; i++)
{
if(this.ActiveMdiChild.Equals(tabControl1.TabPages[i].Tag))
{
this.tabControl1.SelectedTab = this.tabControl1.TabPages[i];
break;
}
}
}
else
{
this.tabControl1.Visible = false;
}
}
#endregion
把MDI窗口与TabControl关联并加入快捷菜单#region 把MDI窗口与TabControl关联并加入快捷菜单
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
foreach (Form childForm in this.MdiChildren)
{
//Check for its corresponding MDI child form
if (this.tabControl1.SelectedTab != null && childForm.Equals(tabControl1.SelectedTab.Tag))
{
//Activate the MDI child form
childForm.Select();
}
}
}
private void AddMDIChildToTabCtrl(Form frmChild)
{
frmChild.MdiParent = this;
frmChild.Closed += new EventHandler(frmChild_Closed);
//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.Tag = frmChild;
tp.Show();
//child Form will now hold a reference value to a tabpage
frmChild.Show();
this.tabControl1.SelectedTab = tp;
}
void frmChild_Closed(object sender, EventArgs e)
{
this.RemoveTabPageFromTabCtrl();
}
private void ctxmnuClose_Click(object sender, System.EventArgs e)
{
if(this.tabControl1.Visible && tabControl1.TabCount > 0)
{
Form childForm = this.ActiveMdiChild;
childForm.Close();
this.RemoveTabPageFromTabCtrl();
}
}
/**//// <summary>
/// 在子窗体关闭时移除对应的TabPage
/// </summary>
private void RemoveTabPageFromTabCtrl()
{
for (int i = 0; i < this.tabControl1.TabCount; i++)
{
if (this.ActiveMdiChild.Equals(tabControl1.TabPages[i].Tag))
{
this.tabControl1.TabPages.Remove(this.tabControl1.TabPages[i]);
break;
}
}
}
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 (Form childForm in this.MdiChildren)
{
//Check for its corresponding MDI child form
if (this.tabControl1.SelectedTab != null && childForm.Equals(tabControl1.SelectedTab.Tag))
{
//Activate the MDI child form
childForm.Select();
}
}
}
private void AddMDIChildToTabCtrl(Form frmChild)
{
frmChild.MdiParent = this;
frmChild.Closed += new EventHandler(frmChild_Closed);
//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.Tag = frmChild;
tp.Show();
//child Form will now hold a reference value to a tabpage
frmChild.Show();
this.tabControl1.SelectedTab = tp;
}
void frmChild_Closed(object sender, EventArgs e)
{
this.RemoveTabPageFromTabCtrl();
}
private void ctxmnuClose_Click(object sender, System.EventArgs e)
{
if(this.tabControl1.Visible && tabControl1.TabCount > 0)
{
Form childForm = this.ActiveMdiChild;
childForm.Close();
this.RemoveTabPageFromTabCtrl();
}
}
/**//// <summary>
/// 在子窗体关闭时移除对应的TabPage
/// </summary>
private void RemoveTabPageFromTabCtrl()
{
for (int i = 0; i < this.tabControl1.TabCount; i++)
{
if (this.ActiveMdiChild.Equals(tabControl1.TabPages[i].Tag))
{
this.tabControl1.TabPages.Remove(this.tabControl1.TabPages[i]);
break;
}
}
}
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子窗口时,调用AddMDIChildToTabControl即可。