我正在尝试为在选项卡控件中创建的选项卡添加字体颜色和背景颜色,并为没有选项卡的空位置添加背景颜色。因为我的 tabcontrol 被拉伸(stretch)到我的全屏窗体的大小。假设在tabcontrol中只创建了一个tab,那么tab的背景颜色应该是蓝色,tabcontrol的其余地方应该是深蓝色。默认颜色是灰色,非常难看。
我正在使用下面的代码,它适用于选项卡的字体颜色和背景颜色。但是我没有得到其他地方的背景颜色。
我错过了什么吗?
private void tabControl1_DrawItem_1(object sender, DrawItemEventArgs e)
{
//set color for Background
Brush BG_BackBrush = new SolidBrush(Color.FromArgb(111, 111, 111)); //Set background color
//Rectangle BG_r = e.Bounds;
Rectangle BG_r = new Rectangle(1682, 34, this.Width - 2, this.Height - 2);
BG_r = new Rectangle(BG_r.X, BG_r.Y + 8, BG_r.Width, BG_r.Height - 3);
e.Graphics.FillRectangle(BG_BackBrush, e.Bounds);
//set color for Tabs
Font TabFont;
Brush BackBrush = new SolidBrush(Color.FromArgb(147, 188, 200)); //Set background color
Brush ForeBrush = new SolidBrush(Color.FromArgb(11, 51, 106));//Set foreground color
if (e.Index == TaskBarRef.tabControl1.SelectedIndex)
{
TabFont = new Font(e.Font, FontStyle.Bold);
}
else
{
TabFont = new Font(e.Font, FontStyle.Regular);
}
Rectangle r = e.Bounds;
r = new Rectangle(r.X, r.Y + 8, r.Width, r.Height - 3);
string TabName = TaskBarRef.tabControl1.TabPages[e.Index].Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
e.Graphics.FillRectangle(BackBrush, e.Bounds);
e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);
sf.Dispose();
if (e.Index == TaskBarRef.tabControl1.SelectedIndex)
{
TabFont.Dispose();
BackBrush.Dispose();
}
else
{
BackBrush.Dispose();
ForeBrush.Dispose();
}
}
最佳答案
我会查看 this代码项目示例。它显示了如何为选项卡以及整个选定控件着色。
在您的示例中,您可以将 BackBrush 更改为 Solid 画笔并将其添加到 sf.Dispose 行之前:
TaskBarRef.tabControl1.TabPages[e.Index].BackColor = BackBrush.Color;
关于c# - C# Windows 应用程序中 Tabcontrol 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1558323/