C#-UI-Tabcontrol控件重绘以及关闭按钮绘制

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

namespace 控件库_
{
    public partial class tabcontrol重绘 : UserControl
    {
        public tabcontrol重绘()
        {
            InitializeComponent();
        }
     
        const int CLOSE_SIZE = 15;    
        //重绘关闭按钮
        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {
                //Rectangle rec = tabControl1.ClientRectangle;
                //SolidBrush br = new SolidBrush(Color.White);
                //e.Graphics.FillRectangle(br, rec); 
                // 重绘tabPage 
                Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index);
                //添加TabPage属性 


                //新建一个StringFormat对象,用于对标签文字的布局设置 


                StringFormat StrFormat = new StringFormat();              

                //重绘字体和标签颜色
                var tabcon = sender as TabControl;
                //重绘标签头=======================
                SolidBrush back;
                SolidBrush white;
                if (e.Index == tabcon.SelectedIndex)//当前Tab page页的样式
                {
                    //背景颜色
                    back = new SolidBrush(/*Color.FromArgb(45, 45, 48)*/Color.FromArgb(1, 158, 213));
                    //字体颜色
                    white = new SolidBrush(Color.White);
                }
                else//其余Tab page页的样式
                {
                    //背景颜色
                    back = new SolidBrush(Color.White);
                    //字体颜色
                    white = new SolidBrush(Color.FromArgb(3, 41, 81));
                }
                StringFormat sf = new StringFormat()
                {
                    //文本水平/垂直居中对齐
                    Alignment = StringAlignment.Near,
                    LineAlignment = StringAlignment.Center,
                };

                //绑定选项卡
                Rectangle rec = tabcon.GetTabRect(e.Index);
                //设置选项卡背景
                e.Graphics.FillRectangle(back, rec);

                //设置选项卡字体及颜色           
                e.Graphics.DrawString(tabcon.TabPages[e.Index].Text, new Font("楷体", 13, FontStyle.Bold), white, rec, sf);



                //再画一个矩形框
                using (Pen p = new Pen(Color.Black))
                {
                    myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
                    myTabRect.Width = CLOSE_SIZE;
                    myTabRect.Height = CLOSE_SIZE;
                    e.Graphics.DrawRectangle(p, myTabRect);
                }
                //填充矩形框
                Color recColor = e.State == DrawItemState.Selected ? Color.FromArgb(200, 227, 253) : Color.White;
                using (Brush b = new SolidBrush(recColor))
                {
                    e.Graphics.FillRectangle(b, myTabRect);
                }
                tabControl1.Appearance = TabAppearance.Normal;
                //画关闭符号
                using (Pen p = new Pen(Color.FromArgb(3, 41, 81)))
                {
                    //画"/"线
                    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();
            }
            catch (Exception ex)
            {


            }
        }


        private void tabControl1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && this.tabControl1.SelectedIndex > 0)
            {
                int x = e.X, y = e.Y;
                //计算关闭区域   
                Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.SelectedIndex);
                myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
                myTabRect.Width = CLOSE_SIZE;
                myTabRect.Height = CLOSE_SIZE;


                //如果鼠标在区域内就关闭选项卡   
                bool isClose = x > myTabRect.X && x < myTabRect.Right
                 && y > myTabRect.Y && y < myTabRect.Bottom;


                if (isClose == true)
                {
                    this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
                }
            }

            return;
        }

        private void tabcontrol重绘_Load(object sender, EventArgs e)
        {

            //this.tabControl1.TabPages.Clear();
            this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.tabControl1.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE);
            this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);
            this.tabControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseDown);
          
        }




    }
}

   








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值