如何更改C#winforms中未使用的空格选项卡的背景色?

如何更改C#winforms中未使用的空格选项卡的背景色?,c#,winforms,tabs,tabcontrol,C#,Winforms,Tabs,Tabcontrol,前 我可以更改选项卡的背景色和前景色。。但是我想改变{}-->空空间的颜色,这样做可能吗。。它显示默认的winforms颜色..在dis中帮助我 |Tab1|Tab2|Tab3| { } | | | | | | | | |_____________________| 我认为为该空间添加颜色的唯一方法是覆盖窗

我可以更改
选项卡的
背景色
前景色
。。但是我想改变{}-->空空间的颜色,这样做可能吗。。它显示默认的winforms颜色..在dis中帮助我

  |Tab1|Tab2|Tab3| {    }
  |                     |
  |                     |
  |                     |
  |                     |
  |_____________________|

我认为为该空间添加颜色的唯一方法是覆盖窗口的
OnPaintBackground
方法,因此只需将其粘贴到表单(窗口)上即可


还必须将“外观”属性更改为“正常”

对我来说,它工作得很好


您也可以像以前一样创建自定义tabcontrol

private void Form1_Load(object sender, EventArgs e)
{

}

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
    RectangleF emptyspacerect = new RectangleF(
            lasttabrect.X + lasttabrect.Width + tabControl1.Left,
            tabControl1.Top + lasttabrect.Y, 
            tabControl1.Width - (lasttabrect.X + lasttabrect.Width), 
            lasttabrect.Height);

    Brush b = Brushes.BlueViolet; // the color you want
    e.Graphics.FillRectangle(b, emptyspacerect );
}

尝试将以下代码添加到DrawItem事件处理程序中。不要忘记将DrawMode设置为“OwnerdrawFixed”

您可能需要对其进行一些调整,以覆盖一些未绘制的边距


"这个答案比前一个好得多。但是tabCtrl没有定义。它必须是tabControl1控件。

我认为标准的.NET TabControl不允许您将选项卡右侧的“可用空间”设置为特定的背景色-它是透明的,只显示窗体的背景色。如果您真的需要此功能,您必须找到另一个支持此功能的TabControl将外观属性更改为“正常”它将更改为透明有更多的理由不这样做。@marc_s:OP正在将其tab control DrawMode设置为OwnerDrawFixed以自定义tab控件的样式。但是,执行此操作不再将背景空白设置为透明,而是将其设置为system.Control您不必编写代码,或者如果使用visual studio write
protected override
和space,它将为您提供重写方法,但“只需编写|粘贴代码”该方法将被自动覆盖@user1484603Seddik我也尝试过了……但它对我不起作用。。正如u所说的,我覆盖了这个方法并粘贴了代码…但是空的空间不会被绘制…尝试创建一个新项目,只添加这个TabControl&我的代码,看看它是否有效,在我的例子中,我有一个带有TabControl的窗口,它有效;即使我写了你的代码,也添加了OnPaintBackground方法,它工作得很好。你是否使用了
tabControl1.DrawMode=TabDrawMode.OwnerDrawFixed
@user1484603Seddik它的工作刚刚通过新项目检查过..非常感谢...:)在windows 10的“开始”菜单下拖动时,会出现此错误。它将使用控件颜色重新绘制组件
private void Form1_Load(object sender, EventArgs e)
{

}

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
    RectangleF emptyspacerect = new RectangleF(
            lasttabrect.X + lasttabrect.Width + tabControl1.Left,
            tabControl1.Top + lasttabrect.Y, 
            tabControl1.Width - (lasttabrect.X + lasttabrect.Width), 
            lasttabrect.Height);

    Brush b = Brushes.BlueViolet; // the color you want
    e.Graphics.FillRectangle(b, emptyspacerect );
}
public class mytab : TabControl
{
    public mytab()
        : base()
    {
        this.DrawMode = TabDrawMode.OwnerDrawFixed;
        this.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
    }

    private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        Font fntTab;
        Brush bshBack;
        Brush bshFore;

        if (e.Index == this.SelectedIndex)
        {
            fntTab = new Font(e.Font, FontStyle.Bold);
            bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            bshFore = Brushes.Black;
            //bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            //bshFore = Brushes.Blue;
        }
        else
        {
            fntTab = e.Font;
            bshBack = new SolidBrush(Color.Red);
            bshFore = new SolidBrush(Color.Aqua);

            //bshBack = new SolidBrush(Color.White);
            //bshFore = new SolidBrush(Color.Black);
        }

        string tabName = this.TabPages[e.Index].Text;
        StringFormat sftTab = new StringFormat();
        e.Graphics.FillRectangle(bshBack, e.Bounds);
        Rectangle recTab = e.Bounds;
        recTab = new Rectangle(recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
        e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);


        Rectangle r = this.GetTabRect(this.TabPages.Count - 1);

        RectangleF tf =
            new RectangleF(r.X + r.Width,
            r.Y-5, this.Width - (r.X + r.Width)+5, r.Height+5);
        Brush b = Brushes.BlueViolet;

        e.Graphics.FillRectangle(b, tf);
    }

}
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { SolidBrush fillbrush= new SolidBrush(Color.Red);

  //draw rectangle behind the tabs
  Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
  Rectangle background = new Rectangle();
  background.Location = new Point(lasttabrect.Right, 0);

  //pad the rectangle to cover the 1 pixel line between the top of the tabpage and the start of the tabs
  background.Size = new Size(tabControl1.Right - background.Left, lasttabrect.Height+1);
  e.Graphics.FillRectangle(fillBrush, background);
}