Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the background color of unused space tab in C# winforms?

Ex

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

I am able to change the backcolor and forecolor of Tab.. but I want to change the color of that { } -- > Empty space is this possible to do that. .. It shows default winforms color..help me in 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);

    }
like image 764
Aravind Avatar asked Aug 06 '12 05:08

Aravind


1 Answers

Try adding the following code to your DrawItem event handler. Don't forget to set the DrawMode to "OwnerdrawFixed".

You might have to tweak it a bit to cover some margins which aren't painted.

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

'This answer is much better than prior one. But the tabCtrl is not defined. It has to be tabControl1 control.

like image 156
Roast Avatar answered Sep 28 '22 06:09

Roast