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

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

Ex

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


        
3条回答
  •  -上瘾入骨i
    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

提交回复
热议问题