我有一个tabcontrol,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
事件中的选定标签,并仅绘制选定的标签。
答案 0 :(得分:0)
我终于找到了问题的答案。我将全局变量修改为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);
}
}
对我来说很好。