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

后端 未结 2 1705
没有蜡笔的小新
没有蜡笔的小新 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:23

    I use something like this in mu TabControl derived class (and it will do gradients too):

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        // fill in the whole rect
        using (SolidBrush br = new SolidBrush(Theme.FormBackColor))
        {
            e.Graphics.FillRectangle(br, ClientRectangle);
        }
    
        // draw the tabs
        for (int i = 0; i < TabPages.Count; ++i)
        {
            TabPage tab = TabPages[i];
            // Get the text area of the current tab
            RectangleF tabTextArea = (RectangleF)GetTabRect(i);
    
            // determine how to draw the tab based on which type of tab it is
            Color tabTopBackColor = GetTopBackColor();
            Color tabBottomBackColor = GetBottomBackColor();
            Color tabTextColor = GetTextColor();
    
            // draw the background
            using (LinearGradientBrush br = new LinearGradientBrush(tabTextArea, tabTopBackColor, tabBottomBackColor, LinearGradientMode.Vertical))
            {
                e.Graphics.FillRectangle(br, tabTextArea);
            }
    
            // draw the tab header text
            using (SolidBrush brush = new SolidBrush(tabTextColor))
            {
                e.Graphics.DrawString(tab.Text, Font, brush, CreateTabHeaderTextRect(tabTextArea));
            }
        }
    }
    
    private RectangleF CreateTabHeaderTextRect(RectangleF tabTextArea)
    {
        tabTextArea.X += 3;
        tabTextArea.Y += 1;
    
        tabTextArea.Height -= 1;
    
        return tabTextArea;
    }
    

提交回复
热议问题