我找不到任何选项卡容器的属性来更改包含选项卡的条的背景颜色,我该怎么办?
private void Form1_Load(object sender, EventArgs e)
{
Panel tabBackground = new Panel
{
Location = tabControl1.Location,
Size = tabControl1.Size,
// Your color here
BackColor = Color.Magenta
};
tabBackground.Controls.Add(tabControl1);
Controls.Add(tabBackground);
tabControl1.Dock = DockStyle.Fill;
}
http://rajeshkm.blogspot.com/2006/07/how-to-change-color-of-tab-control-in.html
请确保阅读第一条评论,因为其中包含了一个修正代码的错误。
借用(并修正)的代码提示:
private void ChangeTabColor(object sender, DrawItemEventArgs e)
{
Font TabFont;
Brush BackBrush = new SolidBrush(Color.Green); //Set background color
Brush ForeBrush = new SolidBrush(Color.Yellow);//Set foreground color
if (e.Index == this.tabControl1.SelectedIndex)
{
TabFont = new Font(e.Font, FontStyle.Italic | FontStyle.Bold);
}
else
{
TabFont = e.Font;
}
string TabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
e.Graphics.FillRectangle(BackBrush, e.Bounds);
Rectangle r = e.Bounds;
r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);
//Dispose objects
sf.Dispose();
if (e.Index == this.tabControl1.SelectedIndex)
{
TabFont.Dispose();
BackBrush.Dispose();
}
else
{
BackBrush.Dispose();
ForeBrush.Dispose();
}
}
要在您的代码中使用此功能,请将以下代码放置在表单的加载事件中:
tabControl1.DrawItem += ChangeTabColor.