Bootstrap

C# tabcontrol当标签页被选中后改变颜色

C# tabcontrol当标签页被选中后改变颜色

private TabControl tabsControl = new TabControl();
 
tabsControl.DrawMode = TabDrawMode.OwnerDrawFixed;
 
tabsControl.DrawItem += new DrawItemEventHandler(this.tabsControl_DrawItem);
 
private void tabCameraSubjectTuingLogsControl_DrawItem(object sender, DrawItemEventArgs e)
{
 
    #region 重绘标签头=======================
    SolidBrush back;
    SolidBrush white;
    if (e.Index == tabsControl.SelectedIndex)//当前Tab page页的样式
    {
        //背景颜色
        back = new SolidBrush(/*Color.FromArgb(45, 45, 48)*/Color.Wheat);
        //字体颜色
        white = new SolidBrush(Color.Blue);
    }
    else//其余Tab page页的样式
    {
        //背景颜色
        back = new SolidBrush(Color.SeaShell);
        //字体颜色
        white = new SolidBrush(Color.Blue);
    }
    StringFormat sf = new StringFormat()
    {
        //文本水平/垂直居中对齐
        Alignment = StringAlignment.Center,
        LineAlignment = StringAlignment.Center,
    };
 
    //绑定选项卡
    Rectangle rec = tabsControl.GetTabRect(e.Index);
    //设置选项卡背景
    e.Graphics.FillRectangle(back, rec);
 
    //设置选项卡字体及颜色
    e.Graphics.DrawString(tabsControl.TabPages[e.Index].Text, new Font("微软雅黑", 8, FontStyle.Bold), white, rec, sf);
    #endregion
}

在这里插入图片描述

;