C#中设置多标签页面

C#中设置多标签页面

设置panel1,添加button2,button2按钮,接着设定panel2,添加tabControl1, 然后添加form2, form3, 以验证效果。

1、tabControl1的Anchor属性设置Top, Bottom, Left, Right(上下左右都点选),Dock属性设置Fill

2、tabControl1的DrawMode属性设置OwnerDrawFixed (可以看到关闭的按钮)

3、tabControl1的SizeMode属性设置FillToRight (为标签页可以完整显示,如标签:TEST++++++)

4、tabControl1的TabPages属性根据需要添加或称除,可以全部删除

5、添加panel时,BorderStyle的属性设置Fixed3D

6、将Form2,Form3的BackColor的属性设置ActiveBorder,可以较为明显的看到窗口是否真的最大化,是否达到预期的效果,此处设置根据需要而定

private void button1_Click(object sender, EventArgs e)

{

    //foreach是为了只打开一个窗口

    foreach (TabPage tabpagesum in tabControl1.TabPages)

    {

        if (tabpagesum.Name == "Form2")

        {

            tabControl1.SelectTab("Form2");

            return;

        }

    }

    TabPage tab = new TabPage();

    tab.Name = @"Form2";

    tab.Text = @"ADD_Form2";

    Form2 f2 = new Form2();

    f2.TopLevel = false; 

    f2.FormBorderStyle = FormBorderStyle.None; //关闭掉最大化,最小化,关闭按钮

    tab.Controls.Add(f2);

    tabControl1.TabPages.Add(tab);

    f2.Dock = DockStyle.Fill; //让整个tabcontrol控件填满窗体

    f2.Show();

    f2.WindowState = FormWindowState.Normal;

    tab.ControlRemoved += new ControlEventHandler(tabpage_ControlRemoved);

}

private void tabpage_ControlRemoved(object sender, EventArgs e)

{

    if (tabControl1.SelectedIndex >= 0)

    {

        tabControl1.TabPages.Remove(tabControl1.SelectedTab);

    }

}

private void button2_Click(object sender, EventArgs e)

{

    //foreach是为了只打开一个窗口

    foreach (TabPage tabpagesum in tabControl1.TabPages)

    {

        if (tabpagesum.Name == "Form3")

        {

            tabControl1.SelectTab("Form3");

            return;

        }

    }

    TabPage tab = new TabPage();

    tab.Name = @"Form3";

    tab.Text = @"ADD_Form3++++++++++++++";//验证内容较多时,是否完全显示

    Form3 f3 = new Form3();

    f3.TopLevel = false;

    f3.FormBorderStyle = FormBorderStyle.None; //关闭掉最大化,最小化,关闭按钮

    tab.Controls.Add(f3);

    tabControl1.TabPages.Add(tab);

    f3.Dock = DockStyle.Fill;//让整个tabcontrol控件填满窗体

    f3.Show();

    tab.ControlRemoved += new ControlEventHandler(tabpage_ControlRemoved);

}

//鼠标点击关闭按扭

private void tabControl1_MouseDown(object sender, MouseEventArgs e)

{

    if (e.Button == MouseButtons.Left)

    {

        int x = e.X;

        int y = e.Y;

        Rectangle tabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex);

        tabRect.Offset(tabRect.Width - 0x12, 2);

        tabRect.Width = 15;

        tabRect.Height = 15;

        if ((((x > tabRect.X) && (x < tabRect.Right)) && (y > tabRect.Y)) && (y < tabRect.Bottom))

        {

            this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);

        }

    }

}

//给TabControl的Page添加个关闭按钮

const int CloseSize = 12;

private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)

{

    Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index);   

    e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);

    using (Pen p = new Pen(Color.Black))

    {

        myTabRect.Offset(myTabRect.Width - (CloseSize + 3), 2);

        myTabRect.Width = CloseSize;

        myTabRect.Height = CloseSize;

        e.Graphics.DrawRectangle(p, myTabRect);

    }

    Color recColor = e.State == DrawItemState.Selected ? Color.DarkRed : Color.DarkGray;

    using (Brush b = new SolidBrush(recColor))

    {

        e.Graphics.FillRectangle(b, myTabRect);

    }

    using (Pen p = new Pen(Color.White))

    {

        Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);

        Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);

        e.Graphics.DrawLine(p, p1, p2);

        Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);

        Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);

        e.Graphics.DrawLine(p, p3, p4);

    }

    e.Graphics.Dispose();

}

你可能感兴趣的:(C#)