C#的TabControl模擬IE7程序事例,帶關閉功能

在csdn下載了一個類似ie7的功能,使用的是默認的TabControl控件  窗體算比較醜  功能是實現了  廢話不多說 貼代碼

1:先上圖

2:貼代碼

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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int isclicktwo = 0;
        private void tabControl1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 0) return;
            if (e.Button == MouseButtons.Right)
            {
                for (int i = 1; i < tabControl1.TabPages.Count; i++)
                {
                    TabPage tp = tabControl1.TabPages[i];
                    if (tabControl1.GetTabRect(i).Contains(new Point(e.X, e.Y)))
                    {
                        tabControl1.SelectedTab = tp;

                        this.contextMenuStrip1.Show(this.tabControl1, new Point(e.X, e.Y));
                        break;
                    }
                }
            }

            if (e.Button == MouseButtons.Left)
            {
                isclicktwo++;
                Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex);
                int width = myTabRect.Width / 4;
                int height=myTabRect .Height ;
                Rectangle closeRect = new Rectangle(myTabRect.X, myTabRect.Y, width, height);
                if (closeRect.Contains(new Point(e.X, e.Y)))
                {
                   
                    if(isclicktwo >=2)
                    {
                      this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
                      isclicktwo = 0;
                    }
                }
            }
        }

        private void 關閉ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 1; i < this.tabControl1.TabPages.Count; i++)
            {
                if(this.tabControl1.TabPages [i].ImageIndex!=-1)
                {
                    this.tabControl1.TabPages[i].ImageIndex = -1;
                }
            }
            if (this.tabControl1.SelectedIndex == 0) return;
            this.tabControl1.SelectedTab .ImageIndex =0;
            isclicktwo = 0;//轉換頁清零
           
        }

        private void tabControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 0) return;
            if (e.Button == MouseButtons.Left)
            {
                Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex);
                int width = myTabRect.Width / 4;
                int height = myTabRect.Height;
                Rectangle closeRect = new Rectangle(myTabRect.X, myTabRect.Y, width, height);
                if (closeRect.Contains(new Point(e.X, e.Y)))
                {
                    this.tabControl1.TabPages[this.tabControl1.SelectedIndex].ImageIndex = 1;
                    Application.DoEvents();
                }
            }
        
        }

        private void tabControl1_MouseLeave(object sender, EventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 0) return;
            this.tabControl1.TabPages[this.tabControl1.SelectedIndex].ImageIndex = 0;
        }
    }
}

功能還需完善  思路已出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章