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

后端 未结 3 1761
孤街浪徒
孤街浪徒 2021-01-18 16:30

Ex

  |Tab1|Tab2|Tab3| {    }
  |                     |
  |                     |
  |                     |
  |                     |
  |____________________         


        
相关标签:
3条回答
  • 2021-01-18 17:22

    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.

    0 讨论(0)
  • 2021-01-18 17:32

    you can also create a custom tabcontrol as you did

    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);
        }
    
    }
    
    0 讨论(0)
  • 2021-01-18 17:33

    I think the only way to give that space a color is to override the OnPaintBackground method of the window, so just paste this on your form (window)

    you must also change the Appearance Property to "Normal"

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

    for me it's working perfectly

    enter image description here

    0 讨论(0)
提交回复
热议问题