给你的MDI程序加上一个标签栏,方便地切换和关闭子窗体

单击显示全图,Ctrl+滚轮缩放图片
首先创建一个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; }
        }
    }
}

然后,在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

最后,在创建MDI窗口对象时,把它放进TabControl就行了
Employee emp  =   new  Employee();
this .AddMDIChildToTabCtrl(emp);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值