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

时间:2012-08-06 05:01:05

标签: c# winforms tabs tabcontrol

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

我可以更改backcolor的{​​{1}}和forecolor ..但我想更改{} - >的颜色空的空间可以做到这一点。 ..它显示默认的winforms color..help me in dis ..

Tab

3 个答案:

答案 0 :(得分:5)

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

您可能需要稍微调整一下以覆盖一些未绘制的边距。

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); }

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

答案 1 :(得分:4)

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

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

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 );
}

对我来说它完美无缺

enter image description here

答案 2 :(得分:1)

你也可以像你一样创建一个自定义tabcontrol

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);
    }

}