Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
One Step Further: Changing Form Transparency
Interested in one last special effect? With GDI+, you can do things that are difficult or even impossible in earlier versions of Visual Basic. For example, you can make a form partially transparent so that you can see through it. Let's say you're designing a photo-display program that includes a separate form with various options to manipulate the photos. You can make the option form partially transparent so that the user can see any photos beneath it while still having access to the options.
In the following exercise, you'll change the transparency of a form by changing the value of the Opacity property.
Set the Opacity property
On the File menu, click the Close Project command.
Create a new project named My Transparent Form.
Display the form, click the Button control in the Toolbox, and then draw two buttons on the form.
Set the following properties for the two buttons and the form:
Object
Property
Setting
Button1
Text
“Set Opacity”
Button2
Text
“Restore”
Form1
Text
“Transparent Form”
Double-click the Set Opacity button on the form.
Type the following program code in the Button1_Click event procedure:
Me.Opacity = 0.75
Opacity is specified as a percentage, so it has a range of 0 to 1. This line sets the Opacity of Form1 (Me) to 75 percent.
Display the form again, double-click the Restore button, and then enter the following program code in the Button2_Click event procedure:
Me.Opacity = 1
This line restores the opacity to 100 percent.
Click the Save All button, and save the project in the c:\vb05sbs\chap15 folder.
TIP
The complete Transparent Form program is located in the c:\vb05sbs\chap15\transparent form folder.
Click the Start Debugging button to run the program.
Click the Set Opacity button.
Notice how you can see through the form, as shown here:
Click the Restore button.
The transparency effect is removed.
When you're done testing the transparency effect, click the Close button to quit the program.
The program stops, and the development environment returns.