Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to exclude control from a WinForm.Opacity in C#

Tags:

c#

.net

winforms

I have a form that i set it's Opacity as 50% like this:

this.Opacity = 0.5D; <--this==Form

My problem is that everything that on the form is with an Opacity of 50%

I have two buttons on the form and I want them without Opacity.

I know that this.Opacity included all Controls and for some reason the graphics too

My question is, How to Exclude the Opacity of the controls?

Example Image: example 2

Thanks!

like image 867
Ofir Hadad Avatar asked Nov 16 '25 11:11

Ofir Hadad


1 Answers

Since Control doesn't have Opacity property and plus that, most of the controls doesn't support transparent colors, then a working solution can be this:

  1. Create a Form called MainForm and place all the controls you're going to be excluded.

    1.1 Set both of BackColor and TransparencyKey properties of MainForm to the same color, e.g Color.Red

  2. Create another form named TransparentForm and place all controls that must become transparent. Set ShowInTaskbar property to False.

  3. In the MainForm Load event show the TransparentForm and send it to back.

    private void MainForm_Load(object sender, EventArgs e)
    {
        TransparentForm form = new TransparentForm();
        form.Opacity = 0.5D;
        form.Show();
        form.SendToBack(); 
    }
    

    enter image description here

The position of controls in both form must be such that, when combined, it shows the proper user interface.

like image 51
Mohsen Afshin Avatar answered Nov 18 '25 05:11

Mohsen Afshin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!