Covers how to change the opacity of a form (making a form transparent) and making only portions of the form transparent using TransparencyKey.
Making a form transparent is farely simple, you only need to set one property.
Add to buttons to a form (btnIncrease and btnDecrease), one will increase the opacity of the form and one will decrease it, thus making it more transparent.
Also if you want to check out the current opacity you can add a TextBox (txtCurrOp) that will display it.
Now double click each of the buttons. For the Click event of the btnDecrease button use the following code:
// Decrease the opacity of the form by 0.01
this.Opacity -= 0.01;
// Update the TextBox
txtCurrOp.Text = this.Opacity.ToString();
And for the btnIncrease Click event:
// Increase the opacity of the form by 0.01
this.Opacity += 0.01;
// Update the TextBox
txtCurrOp.Text = this.Opacity.ToString();
Here’s how the form looks with the opacity 0.75 (that means 75%):
Making portions of the form transparent (TransparencyKey)
But there’s more than this, you can set the opacity for different portions of the form using the TransparencyKey property. By setting this property to a specified color any part of the form that has that color will be transparent.
Let’s test this, double click the form to get to the Form1_Load event. Paste the following code there:
this.TransparencyKey = Color.Black;
This will make everything that’s black on the form, transparent. The text on the buttons, label and TextBox is black, so are the Minimize, Maximize and Close buttons, thus they will be transparent. Here is the form on top of a photo (clouds):
But there’s another way of using TransparencyKey. Add a Panel (Panel1) to the form with the BackColor property set to Red.
Inside the Load event of Form1 use the following:
this.TransparencyKey = Color.Red;
As you might expect, Panel1, being red will actually be transparent:
Also by using TransparencyKey you can create custom shaped forms, but this will be covered in a soon to come tutorial.