tabControl 设置标题颜色

主要代码来源:https://www.cnblogs.com/Hua-Min/archive/2013/05/14/TabControl.html   博主:蓝色狙击手

发现的问题:标题栏的背景色不好看,并调整了代码。

1、将TabControl的属性【DrawMode】设置为:OwnerDrawFixed

2、在TabControl事件【DrawItem】中添加如下代码: 

      Color seletedColor =     //选中项背景色,自行设置。

      Color unSeletedColor =   //未选中项背景色,自行设置。

      Color borderColor =  //边框背景色,自行设置。

      Color backC = seletedColor;     //标题栏面板背景色
                    if (tc.SelectedTab != null)
                    {
                        backC = tc.SelectedTab.BackColor;
                    }

                    int itemsWidth = 0; //所有项目宽度
                    int itemHeight = 0; //项目高度

                    #region 各标题背景颜色设置

                    //标签文字填充颜色
                    SolidBrush FrontBrush = new SolidBrush(Color.Black);
                    StringFormat StringF = new StringFormat();
                    //设置文字对齐方式
                    StringF.Alignment = StringAlignment.Center;
                    StringF.LineAlignment = StringAlignment.Center;

                    for (int i = 0; i < tc.TabPages.Count; i++)
                    {
                        //标签背景填充颜色
                        SolidBrush BackBrush = null;
                        //获取标签头工作区域
                        Rectangle Rec = tc.GetTabRect(i);
                        itemsWidth += Rec.Width;
                        itemHeight = Rec.Height;

                        if (tc.SelectedTab == tc.TabPages[i])
                        {
                            //绘制标签头背景颜色、边框 ->存在的问题,3D效果默认生成的边框,无法覆盖。
                            BackBrush = new SolidBrush(seletedColor);
                            e.Graphics.FillRectangle(BackBrush, Rec);

                            Pen pen = new Pen(borderColor);
                            e.Graphics.DrawRectangle(pen, Rec);
                            e.Graphics.DrawLine(new Pen(seletedColor), new Point(Rec.X, Rec.Y + Rec.Height), new Point(Rec.X + Rec.Width, Rec.Y + Rec.Height)); //额外画一条线,遮挡选中项的绘图的下边框。
                            e.Graphics.DrawLine(new Pen(backC), new Point(Rec.X, Rec.Y + Rec.Height + 1), new Point(Rec.X + Rec.Width, Rec.Y + Rec.Height+1)); //额外画一条线,遮挡选中项的控件的下边框。
                        }
                        else
                        {
                            //将标签的颜色重置为  backC – 标题栏面板背景色
                            Rec = new Rectangle(Rec.X, Rec.Y – 2, Rec.Width, Rec.Height + 2);   //-2 是为了将边框颜色覆盖掉。
                            e.Graphics.FillRectangle(new SolidBrush(backC), Rec);

                            BackBrush = new SolidBrush(unSeletedColor);
                            //绘制标签头背景颜色、边框
                            Rec = new Rectangle(Rec.X, Rec.Y + 4, Rec.Width, Rec.Height – 4); //将边框高度降低 4 像素来凸显选中项
                            e.Graphics.FillRectangle(BackBrush, Rec);
                            Pen pen = new Pen(borderColor);
                            e.Graphics.DrawRectangle(pen, Rec);
                        }

                        //绘制标签头文字
                        e.Graphics.DrawString(tc.TabPages[i].Text, tc.Font, FrontBrush, Rec, StringF);
                    }

                    #endregion

                    #region 标题栏面板背景颜色设置
                   
                    Rectangle recOfMainBack = tc.ClientRectangle;
                    Rectangle newRecOfMainBack = new Rectangle(recOfMainBack.X + itemsWidth + 3, recOfMainBack.Y, recOfMainBack.Width – itemsWidth, itemHeight + 1); //获取标题栏空白位置的 Rectangle
                    e.Graphics.FillRectangle(new SolidBrush(backC), newRecOfMainBack);

                    #endregion

 

3、仍存在的问题,3D效果的边框不太好看。选中项的边框的3D效果未能成功覆盖。

转载于:https://www.cnblogs.com/DarkWill-BlackGod/p/9336217.html

    原文作者:weixin_30905133
    原文地址: https://blog.csdn.net/weixin_30905133/article/details/95711940
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞