简体   繁体   English

如何将不可见的背景应用于面板上的控件? (C#)

[英]How can i apply invisible background to a control on panel? (C#)

TransparencyKey is not working when applied to a Control which is on a panel, the panel's invisible background is working. 当将TransparencyKey应用于面板上的控件时,该面板不可见的背景正在工作,TransparencyKey不起作用。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.TransparencyKey = Color.FromArgb(0, 0, 1);
        panel1.BackColor = Color.FromArgb(0, 0, 1);
        button1.BackColor = Color.FromArgb(0, 0, 1);
    }
}

button1 is the Control on panel1. button1是panel1上的Control。 button1 still has its original backcolor (30,30,30) button1仍具有其原始背景色(30,30,30)

According to the documentation for Color.FromArgb you are currently calling the method using the "RGB" overload - the values you're specifying are only populating the "RGB" part of the colour and ignoring the "A" or "alpha" part. 根据Color.FromArgb的文档,您当前正在使用“ RGB”重载调用该方法-您指定的值仅填充颜色的“ RGB”部分,而忽略了“ A”或“ alpha”部分。 You need to use the overload that accepts four arguments: 您需要使用接受四个参数的重载:

button1.BackColor = Color.FromArgb(0, 0, 0, 1);

Notice the 0 at the begining - this is the alpha property, setting it to 0 makes the colour transparent. 请注意开头处的0这是alpha属性,将其设置为0将使颜色透明。 The clue is in the method name - "ARGB" - which denotes the order in which to specify the arguments. 线索位于方法名称“ ARGB”中,该名称表示指定参数的顺序。

From MS Docs: 从MS Docs:

FromArgb(Int32, Int32, Int32, Int32)

Creates a Color structure from the four ARGB component (alpha, red, green, and blue) values. 根据四个ARGB分量(alpha,红色,绿色和蓝色)值创建Color结构。 Although this method allows a 32-bit value to be passed for each component, the value of each component is limited to 8 bits. 尽管此方法允许为每个组件传递32位值,但每个组件的值限制为8位。

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

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