using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

 

namespace WindowsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        #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 button1_Click(object sender, EventArgs e)
        {
            
            frmAddBill fab = new frmAddBill();

            if (fab != null && !ShowMDIChild(fab,fab))
            {
                fab.Dispose();
                fab = null;
            }
        }


        public bool ShowMDIChild(Form frmchild, object tag)
        {
        
            if (!IsAlreadyOpen(frmchild,tag))
            {
              
                try
                {
                    AddMDIChildToTabCtrl(frmchild, this.tabControl1);
                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }
            return true;
        }

        public bool IsAlreadyOpen(Form frmchild,object tag)
        {
            int intCount = this.MdiChildren.Length;
            if (intCount > 0)
            {
                Form[] frmArray = this.MdiChildren;
                foreach (Form frmMdi in frmArray)
                {
                    if ((Convert.ToString(frmMdi.Tag)) == (Convert.ToString(tag)))
                    {
                        frmMdi.Activate();
                        frmMdi.BringToFront();
                        return true;
                    }
                }
            }   
            return false;
        }

        private void tabControl1_SelectedIndexChanged(object sender, 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, TabControl tab)
        {
            frmChild.MdiParent = this;

            frmChild.Closed += new EventHandler(frmChild_Closed);
            this.MdiChildActivate += new EventHandler(MainFormMDI_MdiChildActivate);
            tab.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
            tab.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseDown);
         
            TabPage tp = new TabPage();
            tp.Parent = tabControl1;
            tp.Text = frmChild.Text;
            tp.Tag = frmChild;
            frmChild.Tag = frmChild;
            tp.Show();

            
            //child Form will now hold a reference value to a tabpage

            this.tabControl1.SelectedTab = tp;
            frmChild.FormBorderStyle = FormBorderStyle.None;
            frmChild.WindowState = FormWindowState.Maximized;
           
            frmChild.TopMost = false;
            frmChild.Show();
        }

 

        void frmChild_Closed(object sender, EventArgs e)
        {
            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, 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;
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

            Form3 frm3 = new Form3();


            if (frm3 != null && !ShowMDIChild(frm3, frm3))
            {
                frm3.Dispose();
                frm3 = null;
            }
        }

 

 

 

    }
}

posted on 2010-07-06 17:34  TsingCai  阅读(2149)  评论(1编辑  收藏  举报