为什么.Color无法在.NET中用于TabControl? [英] Why doesn't BackColor work for TabControls in .NET?

查看:61
本文介绍了为什么.Color无法在.NET中用于TabControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将.NET中的标准选项卡控件用于选项卡页面,并尝试稍微改变外观,则可以更改选项卡页面的背景色,但不能更改选项卡控件的背景色。该属性可用,您可以对其进行设置,但无效。如果您更改页面的背景颜色而不是选项卡控件的颜色,则看起来很丑。

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.

我知道微软不希望设置它。 MSDN :'此属性支持。 NET Framework基础结构,不能直接在您的代码中使用。该成员对该控件没有意义。’控件属性仅用于支持.NET基础结构的颜色? ...令人难以置信。

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.

我希望这些年来微软会对其进行更改,但他们没有这样做。我创建了自己的TabControl类,该类重写了paint方法以解决此问题。但这真的是最好的解决方案吗?

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?

不为此控件支持BackColor的原因是什么?您如何解决此问题?有没有比覆盖paint方法更好的解决方案?

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?

推荐答案

Rajesh博客中的解决方案确实很有用,但它为选项卡添加了颜色仅部分控件。就我而言,我在不同的彩色背景上有一个tabcontrol。标签本身是灰色的,这没问题,但是标签右侧的区域显示为灰色条。

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.

要将颜色更改为背景颜色,您需要在DrawItem方法中添加以下代码(如Rajesh解决方案所述)。我正在使用VB.Net:

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.

这篇关于为什么.Color无法在.NET中用于TabControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆