You know that .NET includes cool new transparency and " alpha blending" features, and you'd like to try them out.
Windows Forms include a few different transparency features. The simplest are accessible through two properties of each form: Opacity and TransparencyKey. Opacity ranges from 0% to 100% (actually, 0.0 for full transparency and 1.0 for full opacity) and impacts the entire form. Figure 9-14 shows a form set at 50% opacity with this paragraph showing through.
Figure 9-14. A see-through form with 50% opacity
The TRansparencyKey property lets you indicate one form color as the "invisibility" color. When used, anything on the form that appears in the indicated color is rendered invisible. Figure 9-15 shows a form with its transparencyKey property set to Control, the color normally used for the form's background. It appears over this paragraph's text.
Figure 9-15. A see-through form with surface invisibility
A bug in the initial release of Visual Basic 2005 causes some images drawn on a form's surface or on one of its contained controls to ignore the transparencyKey setting, even if that image contains the invisibility color. There is a workaround that uses a third transparency feature of GDI+, the Bitmap object's MakeTransparent() method. The following block of code loads an image from a file, sets the White color as transparent, and draws it on the invisible background from Figure 9-15, producing the results in Figure 9-16:
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim backImage As New Bitmap("c:\logo.bmp") backImage.MakeTransparent(Color.White) Me.BackgroundImage = backImage End Sub
Figure 9-16. A transparent image on a transparent form
A fourth transparency feature involves partially invisible colors. Although the System.Drawing.Color structure includes several predefined colors, you can create your own colors through that structure's FromArgb() method. One variation of this method accepts four arguments: red, green, and blue components, and an "alpha" component that sets the transparency of the color. That value ranges from 0 (fully transparent) to 255 (fully opaque). Another variation accepts just an alpha component and a previously defined color:
' ----- Make a semi-transparent red color. Dim semiRed As Integer = New Color(128, Color.Red) ' ----- Here's another way to do the same thing. Dim semiRed As Integer = New Color(128, 255, 0, 0)
You can then use this color to create pens or brushes as you would with any other color.
Some older systems don't support all methods of transparency. If there is any chance your program will run on such older systems, don't depend on transparency as the sole method of communicating something important to the user.