我有一个选项卡控件,DrawMode
设置为 OwnerDrawFixed
.我已经能够绘制选项卡并为其着色Black
我想做的是为所选选项卡绘制单独的油漆并将其着色Gray
。这是我Draw_Item
活动。
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
//This is the code i want to use to color the selected tab (e.Graphics.FillRectangle(Brushes.Gray, e.Bounds.X, e.Bounds.Y, 200, 32);
e.Graphics.FillRectangle(Brushes.Black, e.Bounds.X, e.Bounds.Y, 200, 32);
e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right-17, e.Bounds.Top+4);
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.White, e.Bounds.Left + 12, e.Bounds.Top + 4);
e.DrawFocusRectangle();
if (e.Index == activeButton)
ControlPaint.DrawBorder(e.Graphics, new Rectangle(e.Bounds.Right - 22, e.Bounds.Top + 4, 20, 20), Color.Blue, ButtonBorderStyle.Inset);
}
我创建了一个全局变量TabPage current
,我想用它来存储当前选项卡页面SelectedIndexChanged
并且我已将所选选项卡分配给变量并调用Invalidate();
以强制重新绘制选项卡。
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
current = tabControl1.SelectedTab;
tabControl1.Invalidate();
}
现在我陷入困境的是如何在DrawItem
事件中仅对所选选项卡着色。
我现在的问题是如何在DrawItem
事件中检查选定的选项卡并仅绘制选定的选项卡。
我终于找到了问题的答案。我将全局变量修改为int
数据类型,然后在SelectedIndexChanged
中将索引分配给它,然后在DrawItem
中检查它。
int current;
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
current = tabControl1.SelectedIndex;
tabControl1.Invalidate();
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Black, e.Bounds.X, e.Bounds.Y, 200, 32);
e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right-17, e.Bounds.Top+4);
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.White, e.Bounds.Left + 12, e.Bounds.Top + 4);
if (e.Index == activeButton)
ControlPaint.DrawBorder(e.Graphics, new Rectangle(e.Bounds.Right - 22, e.Bounds.Top + 4, 20, 20), Color.Blue, ButtonBorderStyle.Inset);
if (e.Index == current)
{
e.Graphics.FillRectangle(Brushes.Gray, e.Bounds.X, e.Bounds.Y, 200, 32);
e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - 17, e.Bounds.Top + 4);
e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.White, e.Bounds.Left + 12, e.Bounds.Top + 4);
}
}
对我来说效果很好。