C# Winform TabControl标签,背景设置图片

改变TabControl的风格有两个方式,1、直接重绘标签及背景;2、重写TabControl控件

1、直接重绘标签及背景

1)设置TabControl属性DrawMode为OwnerDraw 

2)添加事件代码

private void tabControl_DrawItem(object sender, DrawItemEventArgs e)
        {                        
                Image imgButton = Mana.Properties.Resources.button;
                Image imgBJ = Mana.Properties.Resources.bg3;
                //绘制主控件的背景
                Rectangle Rect = new Rectangle(0, 0, this.tabControl.Width, this.tabControl.Height);
                e.Graphics.DrawImage(imgBJ, Rect);

                //新建一个StringFormat对象,用于对标签文字的布局设置
                StringFormat StrFormat = new StringFormat();
                StrFormat.LineAlignment = StringAlignment.Center;// 设置文字垂直方向居中
                StrFormat.Alignment = StringAlignment.Center;// 设置文字水平方向居中          
                
                SolidBrush bruFont = new SolidBrush(Color.FromArgb(255, 255, 255));// 标签字体颜色
                Font font = new System.Drawing.Font("微软雅黑", 10F, FontStyle.Bold);//设置标签字体样式

                //绘制标签样式         
                for (int i = 0; i < tabControl.TabPages.Count; i++)
                {
                    //获取标签头的工作区域
                    Rectangle recChild = tabControl.GetTabRect(i);
                    Rectangle newRect = new Rectangle(recChild.Left - 7, recChild.Top, recChild.Width - 7, recChild.Height);
                    //绘制标签头背景颜色
                    e.Graphics.DrawImage(imgButton, newRect);
                    //绘制标签头的文字
                    e.Graphics.DrawString(tabControl.TabPages[i].Text, font, bruFont, newRect, StrFormat);
                }
            Console.WriteLine("tabControl_DrawItem"+"  "+countTest .ToString ());
            countTest++;
        }

2、重写TabControl控件

重写TabControl控件可以改变其背景为透明或绘制背景图,绘制标签风格等各种,但是有一个问题是,在X64编辑环境下,重写的控件无法直接拖拽到界面进行设计,只能通过设计器代码InitializeComponent()中将原先用的this.tabControl = new System.Windows.Forms.TabControl();改为this.tabControl = new TabControlEx();,此时,则不能打开设计界面,会报错,但是仍然有效

重写控件代码(拷贝的网上的):

public class TabControlEx : TabControl
    {
        private Color _BackColor; //背景颜色

        public TabControlEx()
        {
            this.SetStyle(ControlStyles.UserPaint, true);//用户自己绘制
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);   //
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            //让控件支持透明色
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.UpdateStyles();
        }

        public override Color BackColor
        {//重写backcolor属性 
            get
            {
                return this._BackColor;
            }
            set
            {
                this._BackColor = value;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            this.DrawTitle(e.Graphics);
            base.OnPaint(e);
        }

        protected virtual void DrawTitle(Graphics g)
        {
            Image imgButton = OcvMana.Properties.Resources.button;

            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            Font font = new System.Drawing.Font("微软雅黑", 10F, FontStyle.Bold);//设置标签字体样式
            using (SolidBrush sb = new SolidBrush(Color.FromArgb(127, 0, 0, 0)))
            {
                for (int i = 0; i < this.TabPages.Count; i++)
                {
                    Rectangle rect = this.GetTabRect(i);
                    Rectangle newRect = new Rectangle(rect.Left + 7, rect.Top, rect.Width - 7, rect.Height);
                   
                    g.DrawImage(imgButton, newRect);
                    g.DrawString(this.TabPages[i].Text, font, Brushes.White, rect, sf);
                }
            }
        }
    }

此时New 了新的控件后,直接调试运行发现TabPage页仍然不透明,可在页面Load事件中设置选中第一个页面的同时设置其背景色为透明或其他颜色,代码如下:

this.tabControl.SelectedTab = tp1;    //选择要显示页面1
tp1.BackColor = Color.FromArgb(0, 255, 255, 255);     //设置页面1的背景透明

你可能感兴趣的:(winform)