问题
If you use the standard tab control in .NET for your tab pages and you try to change the look and feel a little bit then you are able to change the back color of the tab pages but not for the tab control. The property is available, you could set it but it has no effect. If you change the back color of the pages and not of the tab control it looks... uhm quite ugly.
I know Microsoft doesn't want it to be set. MSDN: 'This property supports the .NET Framework infrastructure and is not intended to be used directly from your code. This member is not meaningful for this control.' A control property just for color which supports the .NET infrastructure? ...hard to believe.
I hoped over the years Microsoft would change it but they did not. I created my own TabControl class which overrides the paint method to fix this. But is this really the best solution?
What is the reason for not supporting BackColor for this control? What is your solution to fix this? Is there a better solution than overriding the paint method?
回答1:
The solution in Rajesh's blog is really useful, but it colours the tab part of the control only. In my case I had a tabcontrol on a different coloured background. The tabs themselves were grey which wasn't a problem, but the area to the right of the tabs was displaying as a grey strip.
To change this colour to the colour of your background you need to add the following code to the DrawItem method (as described in Rajesh's solution). I'm using VB.Net:
...
Dim r As Rectangle = tabControl1.GetTabRect(tabControl1.TabPages.Count-1)
Dim rf As RectangleF = New RectangleF(r.X + r.Width, r.Y - 5, tabControl1.Width - (r.X + r.Width), r.Height + 5)
Dim b As Brush = New SolidBrush(Color.White)
e.Graphics.FillRectangle(b, rf)
...
Basically you need to get the rectangle made of the right hand side of the last tab to the right hand side of the tab control and then fill it to your desired colour.
回答2:
The background color of the tab seems to be controlled by the OS's Display Properties. Specifically the under the appearance tab, Windows and buttons property (Windows XP). When set to Windows Classic style, the tab doesn't change color ever. When set to Windows XP style, it at least changes from gray to white when selected. So not being able to control the background color is a feature!
回答3:
Thanks, LauraM. You helped get me on the right track. I had already found the link Oskar provided but that didn't do anything for the strip at the end.
In the end, I had to change quite a bit because I needed a background image on the form to bleed through or if the parent was something without a background image, the backcolor. I also needed icons to show if they were present. I have a full write-up with all the code in my TabControl BackColor fix post.
来源:https://stackoverflow.com/questions/67300/why-doesnt-backcolor-work-for-tabcontrols-in-net