6

前任

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

我可以更改 .. 的backcolorand forecolorTab但我想更改 { } 的颜色 --> 空白空间是否可以这样做。..它显示默认的winforms颜色..帮助我在dis..

 private void Form1_Load(object sender, EventArgs e)
    {

    }


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

        if ( e.Index == this.tabControl1.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.tabControl1.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);

    }
4

3 回答 3

8

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

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

私人无效tabControl1_DrawItem(对象发送者,System.Windows.Forms.DrawItemEventArgs e)
{
      SolidBrush fillbrush=新的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 控件。

于 2012-09-07T19:24:38.787 回答
6

我认为给该空间一个颜色的唯一方法是覆盖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 );
}

对我来说它工作得很好

在此处输入图像描述

于 2012-08-06T06:56:59.557 回答
2

您还可以像以前一样创建自定义选项卡控件

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

}
于 2012-08-06T07:57:18.367 回答