c# 自定义TabControl

该代码实现了一个自定义的TabControl控件,允许用户设置头部背景色、字体及颜色,并添加了关闭按钮功能。当鼠标移动到选项卡关闭区域时,可以点击关闭对应的Tab页面。同时,提供了删除Tab页面的方法。
摘要由CSDN通过智能技术生成

可自定义header背景色,headertext字体以及颜色,带关闭按钮。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PACS.Common.Controls
{
    public partial class MyTabcontrol : TabControl
    {
        private const int CLOSE_SIZE = 15;
        private bool _isEnterClose;//是否进入关闭区域
        private int _enterIndex;//当前进入的tabpageIndex

        public MyTabcontrol()
        {
            this.Padding = new Point(12, 3);

            //DrawMode:指定用户还是系统来绘制标题
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.HotTrack = true;
            this.DrawItem += new DrawItemEventHandler(this.MyTabControl_DrawItem);
            this.MouseDown += new MouseEventHandler(this.MyTabControl_MouseDown);
            this.MouseMove += MyTabcontrol_MouseMove;
        }
        
        private void MyTabcontrol_MouseMove(object sender, MouseEventArgs e)
        {
            int x = e.X, y = e.Y;
            //计算关闭区域  
            for (int i = 0; i < this.TabPages.Count; i++)
            {
                Rectangle tab = this.GetTabRect(i);
                tab.Offset(tab.Width - (CLOSE_SIZE + 1), tab.Height / 2 - CLOSE_SIZE / 2);
                tab.Width = CLOSE_SIZE;
                tab.Height = CLOSE_SIZE;
                //如果鼠标在区域内就关闭选项卡  
                _isEnterClose = x > tab.X && x < tab.Right && y > tab.Y && y < tab.Bottom;
                if (_isEnterClose)
                {
                    _enterIndex = i;
                    return;
                }
            }
        }

        private void MyTabControl_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {
                //循环所有TabPages
                for (int i = 0; i < this.TabPages.Count; i++)
                {
                    Rectangle rect;
                    //是否为当前选中tab页
                    if (this.SelectedIndex == i)
                    {
                        rect = this.GetTabRect(this.SelectedIndex);
                        //填充选中tabpage页header颜色
                        e.Graphics.FillRectangle(new SolidBrush(SelectedHeaderColor), rect);
                    }
                    else
                    {
                        var orginRec = this.GetTabRect(i);
                        rect = new Rectangle(orginRec.Location.X, orginRec.Location.Y, orginRec.Width - 1, orginRec.Height);
                        //填充未选中tabpage页header颜色
                        e.Graphics.FillRectangle(new SolidBrush(HeaderBackColor), rect);
                    }
                    TabPage tbg = TabPages[i];
                    //绘制header文字
                    e.Graphics.DrawString(tbg.Text, this.Font, SystemBrushes.ControlText, rect.X + 2, rect.Y + 2);

                    if(tbg.Text == "首页")
                        continue;
                    //绘制关闭按钮的矩形框
                    var tabHeight = rect.Height;
                    Rectangle recClose = new Rectangle(rect.X + rect.Width - CLOSE_SIZE - 1, tabHeight / 2 - CLOSE_SIZE / 2, CLOSE_SIZE, CLOSE_SIZE);
                    e.Graphics.DrawRectangle(new Pen(Color.Transparent), recClose);

                    //填充关闭按钮的矩形框
                    Color recColor = e.State == DrawItemState.Selected ? Color.FromArgb(181, 224, 252) : Color.Transparent;
                    using (Brush b = new SolidBrush(recColor))
                    {
                        e.Graphics.FillEllipse(b, recClose);
                    }
                    //画关闭符号
                    using (Pen objpen = new Pen(Color.Black))
                    {
                        //"\"线
                        Point p1 = new Point(recClose.X + 3, recClose.Y + 3);
                        Point p2 = new Point(recClose.X + recClose.Width - 3, recClose.Y + recClose.Height - 3);
                        e.Graphics.DrawLine(objpen, p1, p2);
                        //"/"线
                        Point p3 = new Point(recClose.X + 3, recClose.Y + recClose.Height - 3);
                        Point p4 = new Point(recClose.X + recClose.Width - 3, recClose.Y + 3);
                        e.Graphics.DrawLine(objpen, p3, p4);
                    }
                }
                //选中的标签页填充颜色
                e.Graphics.Dispose();
            }
            catch (Exception)
            { }
        }

        private int _oldSelectedIndex;
        public Action<string> DeletePage;
        private void MyTabControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (_isEnterClose == true)
                {
                    if (_enterIndex == 0)
                        return;
                    DeletePage.Invoke(TabPages[_enterIndex].Name);

                    if (_enterIndex == _oldSelectedIndex)
                    {
                        if (_enterIndex == TabPages.Count - 1)
                        {
                            this.TabPages.Remove(TabPages[_enterIndex]);
                            this.SelectedTab = this.TabPages[this.TabPages.Count - 1];
                            _oldSelectedIndex = this.TabPages.Count - 1;
                        }
                        else
                        {
                            this.TabPages.Remove(TabPages[_enterIndex]);
                            this.SelectedTab = this.TabPages[_enterIndex];
                            _oldSelectedIndex = _enterIndex;
                        }
                    }
                    else if (_enterIndex < _oldSelectedIndex)
                    {
                        this.TabPages.Remove(TabPages[_enterIndex]);
                        this.SelectedTab = this.TabPages[_oldSelectedIndex - 1];
                        _oldSelectedIndex = _oldSelectedIndex - 1;
                    }
                    else
                    {
                        this.TabPages.Remove(TabPages[_enterIndex]);
                        this.SelectedTab = this.TabPages[_oldSelectedIndex];
                    }
                }
                else
                {
                    _oldSelectedIndex = SelectedIndex;
                }
            }
        }

        private Color _headerColor = Color.White;
        public Color SelectedHeaderColor
        {
            get { return _headerColor; }
            set { _headerColor = value; }
        }

        private Color _headerBackColor = Color.White;
        public Color HeaderBackColor
        {
            get { return _headerBackColor; }
            set { _headerBackColor = value; }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值