Since Windows 2000 was released, developers have been able to make windows transparent—a valuable trick for saving screen space in your applications. You can make windows transparent by adjusting their opacity, which makes them more or less see-through from a user’s perspective.

Before you can make a window transparent in Visual Basic 6, you have to set its WS_EX_LAYERED attribute. This is done through the SetWindowLong API. Then, you can use the SetLayeredWindowAttributes API to set the level of transparency, as shown in Listing A.

The code in the Command1_Click event shown in Listing B makes a window transparent.

The Command2_Click event shows how to restore the original state of the window, as shown in Listing C.

Making a window transparent in .NET is much easier than in VB 6. To do so, simply set the Opacity property to a value between 1 and 0: 1 being opaque, 0 being completely transparent.

Use transparent windows to provide information to the user without taking up valuable screen real estate. This is useful for heads-up displays of information that overlay other windows in your application.

Passing mouse events through windows in Windows 2000 and XP
VB and VB.NET include transparency properties that allow a window to pass mouse events to the window below it. This action is useful when you’re using transparent windows. By passing mouse events, the transparent window doesn’t impede the user from interacting with windows beneath it.

In Visual Basic 6, set the window’s WS_EX_TRANSPARENT attribute, which controls the window’s style. This is done through the SetWindowLong API call, as you can see in Listing D.

The code in the Command1_Click event in Listing E shows how to make a window pass through mouse events.

To reverse the transparency, reset the WS_EX_TRANSPARENT attribute as shown in Listing F, which will remove the window.

Be forewarned that setting the WS_EX_TRANSPARENT attribute affects the entire window; the user can’t close the window, select it with the mouse, or select any controls on the form. The application can still close the window programmatically. You could use such a transparent window for a heads-up display of important data.

With .NET, you can use the form’s TransparencyKey property. Set this property to the color you want to be transparent. As shown in Listing G, the code in sets the transparency of the form to its background color.

Using .NET’s TransparencyKey property gives slightly different results than VB 6’s WS_EX_TRANSPARENT style. .NET’s TransparencyKey makes the client area of the window visually transparent as well as transparent to mouse events. However, it leaves controls and the window title bar selectable. Obviously, this level of control has far more applications than simply disabling a window entirely. Setting the window to pass through mouse events enables you to display information to your users while allowing them to select forms below the transparent window.