简体   繁体   English

如何同步更改标签页和标签的颜色

[英]How do i change the colour of a tabpage and label in sync

I was wondering how do I change the colours of my tabpage and label back colour in sync. 我想知道如何更改标签页的颜色并同步标记背景色。

The code im using in a timer is 我在计时器中使用的代码是

Random rand = new Random();
int A = rand.Next(0, 255);
int R = rand.Next(0, 255);
int G = rand.Next(0, 255);
int B = rand.Next(0, 255);
tabPage1.BackColor = Color.FromArgb(A, R, G, B)

This is what happens normally: without changing the label back colour. 这是通常发生的情况: 无需更改标签底色。 changing the back colour. 改变背景色。

The reason is that you are changing both the RBG channels and also the alpha channel. 原因是您同时更改了RBG通道和alpha通道。

If you keep alpha at 255 the effect goes away..: 如果将alpha保持在255,则效果消失。

int A = 255;

To understand you must consider that a semi-transparent color is not an independent entity. 要理解,您必须考虑半透明的颜色不是独立的实体。

Instead it lets the background shine though to some degree so its looks change with the environment. 相反,它可以使背景发光到某种程度,因此其外观会随环境而变化。

But that background is not the same for the tabpage and the label: the label's background is the tabpage but the tabpage's background is (probably) the form. 但是选项卡页和标签的背景不同:标签的背景是标签页,但标签页的背景是(可能是)表单。

If you add a Panel and another Label in that Panel you can see that the Panel will have the same shade as the 1st Label but the 2nd Label, in the Panel, has an even darker shade as now even more of the rgb colors are being used. 如果在该面板中添加一个面板和另一个标签,则可以看到该面板的阴影与第一个标签相同,但是该面板中的第二个标签的阴影更暗,因为现在越来越多的rgb颜色被使用。用过的。

Note that the reason behind the whole problem is a) the fishy way Winforms implements its 'tranparency' and b) the 'ambient property rule.' 请注意,整个问题背后的原因是:a)Winforms实现其“透明度”的可恶方式,以及b) “环境财产规则”。

Now it copies the background from the parent to supply the child control with info on how to paint itself. 现在,它从父级复制背景,以向子级控件提供有关如何绘制自身的信息。 So the 'transparent' Label actually the gets semi-transparent color of the TabPage as its parent and then combines it with the same color which it gets from the same Parent as the ambient color. 因此,“透明”标签实际上是将TabPage的半透明颜色作为其父级,然后将其与从同一父级中获得的相同颜色组合为环境颜色。

This means the color is stacked upon itself; 这意味着颜色会堆叠在一起。 this is the same effect as painting with a semi-transparent brush: The more strokes you overlay the more of the color shows and tranparency goes away.. 这与使用半透明笔刷绘画的效果相同:叠加的笔触越多,彩色显示就越多,透明度就会消失。

Final note, aside: The way stacking semi-transparency is implemented is not a given, natural choice. 除了最后的注释:实现堆叠半透明的方法不是给定的自然选择。 If you look at photoshop layer modes you can see a large variety of possible ways and if you imaginge painting onto an opaque color with a semi-transparent brush one could also define the 'right' result should involve adding some transparency, but this is not what gdi+ does; 如果您查看Photoshop图层模式,则可以看到多种可能的方式,并且如果使用半透明笔刷将图像成像到不透明的颜色上,则还可以定义“正确”的结果应该包括增加一些透明度,但这不是gdi +会做什么? nor the default ('normal') PS layer mode; 也没有默认的(“正常”)PS层模式; nor the Winforms control painting.. 也不是Winforms控制画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM