C# Winform: How to set the Base Color of a TabControl (not the tabpage)

后端 未结 2 1699
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 02:32

It seems like a simple question but how do I set the bacground color of the \'tab control\', it seems to be derived from the standard window theme color. Is it Possible to c

2条回答
  •  耶瑟儿~
    2021-01-03 03:26

    http://dotnetrix.co.uk/tabcontrol.htm

    private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
    TabPage CurrentTab = tabControl1.TabPages[e.Index];
    Rectangle ItemRect = tabControl1.GetTabRect(e.Index);
    SolidBrush FillBrush = new SolidBrush(Color.Red);
    SolidBrush TextBrush = new SolidBrush(Color.White);
    StringFormat sf = new StringFormat();
    sf.Alignment = StringAlignment.Center;
    sf.LineAlignment = StringAlignment.Center;
    
    //If we are currently painting the Selected TabItem we'll
    //change the brush colors and inflate the rectangle.
    if (System.Convert.ToBoolean(e.State & DrawItemState.Selected))
    {
        FillBrush.Color = Color.White;
        TextBrush.Color = Color.Red;
        ItemRect.Inflate(2, 2);
    }
    
    //Set up rotation for left and right aligned tabs
    if (tabControl1.Alignment == TabAlignment.Left || tabControl1.Alignment == TabAlignment.Right)
    {
        float RotateAngle = 90;
        if (tabControl1.Alignment == TabAlignment.Left)
            RotateAngle = 270;
        PointF cp = new PointF(ItemRect.Left + (ItemRect.Width / 2), ItemRect.Top + (ItemRect.Height / 2));
        e.Graphics.TranslateTransform(cp.X, cp.Y);
        e.Graphics.RotateTransform(RotateAngle);
        ItemRect = new Rectangle(-(ItemRect.Height / 2), -(ItemRect.Width / 2), ItemRect.Height, ItemRect.Width);
    }
    
    //Next we'll paint the TabItem with our Fill Brush
    e.Graphics.FillRectangle(FillBrush, ItemRect);
    
    //Now draw the text.
    e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, (RectangleF)ItemRect, sf);
    
    //Reset any Graphics rotation
    e.Graphics.ResetTransform();
    
    //Finally, we should Dispose of our brushes.
    FillBrush.Dispose();
    TextBrush.Dispose();
    }
    

提交回复
热议问题