Winform修改tabControl标题栏的颜色
时间: 2023-05-17 19:03:40 浏览: 742
您可以使用以下代码来修改 Winform 中 tabControl 标题栏的颜色:
1. 首先,在 tabControl 的 DrawItem 事件中添加以下代码:
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Brush _textBrush;
// Get the item from the collection.
TabPage _tabPage = tabControl1.TabPages[e.Index];
// Get the real bounds for the tab rectangle.
Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
if (e.State == DrawItemState.Selected)
{
// Draw a different background color for the selected tab.
_textBrush = new SolidBrush(Color.White);
g.FillRectangle(Brushes.Blue, e.Bounds);
}
else
{
_textBrush = new System.Drawing.SolidBrush(e.ForeColor);
e.DrawBackground();
}
// Use our own font.
Font _tabFont = new Font("Arial", (float)10.0, FontStyle.Bold, GraphicsUnit.Pixel);
// Draw string. Center the text.
StringFormat _stringFlags = new StringFormat();
_stringFlags.Alignment = StringAlignment.Center;
_stringFlags.LineAlignment = StringAlignment.Center;
g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags));
}
2. 然后,在 tabControl 的 DrawMode 属性中选择 OwnerDrawFixed。
这样就可以修改 tabControl 标题栏的颜色了。