TransparencyKey Property on Forms

2019-08-30 17:48发布

问题:

I would like to swith my form background transparency with Visual C# in a Windows Forms Application.

I used

BackColor = Color.White;
TransparencyKey = Color.White;

Now I want to switch back to "not transparent". How can I accomplish that? Just switching the BackColor makes the elements on the form look strange and it feels ugly. I guess there is a way to reset the property.

回答1:

How about storing the previous values of BackColor and TransparencyKey in local variables, and restoring them when you want to revert to non-transparent? For instance:

private Color _oldBG;
private Color _oldTPKey;

private void MakeTransparent() {
    _oldBG = BackColor;
    _oldTPKey = TransparencyKey;
    BackColor = Color.White;
    TransparencyKey = Color.White;
}

private void MakeNonTransparent() {
    BackColor = _oldBG;
    TransparencyKey = _oldTPKey;
}


回答2:

this is the original value:

this.TransparencyKey = Color.Empty;

You could set this, and then nothing will be transparent.