0

我有一个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事件中的选定选项卡并仅绘制选定的选项卡。

4

1 回答 1

2

我终于找到了我的问题的答案。我将全局变量修改为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);    
    }
}

对我来说很好。

于 2013-09-21T23:10:57.273 回答