by May 5, 2003 0 comments

With Windows 2000 onwards, programmers have a neat feature that allows them to make any window transparent. This is useful in saving screen space, especially for apps that always need to be visible. You can easily do this in VB 6 by setting the WS_EX_LAYERED attribute through the SetWindowLong API. Plus, you can use the SetLayeredWindowAttributes API to set the level of transparency. Let’s understand this with an example. 

Make transparent
windows while creating programs

The following code needs to be put in the general declaration of your main form or the form (window) that you want to make visually transparent. You can start your project as a standard EXE. The code for creating a transparent window is as follows:

Private Declare Function GetWindowLong Lib “user32” Alias “GetWindowLongA” (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib “user32” Alias “SetWindowLongA” (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Declare Function SetLayeredWindowAttributes Lib “user32” (ByVal hwnd As Long, ByVal crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const LWA_ALPHA = &H2&

In this, the various function declarations or API calls are being called from the user32.dll. This dll contains user interface APIs for Win32. The WS_EX_LAYERED constant is used for specifying that this window is going to be a layered (transparent) window. The constant GWL_EXSTYLE is used to set the extended window style necessary for such transparent windows.

Extended window style is used for creating non-standard windows i.e. those that don’t have a rectangular shape or have special characteristics, which in this case is transparency. Finally, LWA_ALPHA is used to indicate that the third argument in the SetLayeredWindowAttributes Function call in the event mentioned below is to be used as the opacity level. 

After initialization, the various function calls declared in the above code can be put in an event, which in this case will be a click event of a command button. The code for this event is as follows: 

Private Sub Command1_Click()
Dim Level As Byte
Level = 128
Call SetWindowLong(Me.hwnd, GWL_EXSTYLE, GetWindowLong(Me.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
Call SetLayeredWindowAttributes(Me.hwnd, 0, level, LWA_ALPHA)
End Sub

Here, the Level variable is of type byte and is used to set the opacity level. In this case it is set to 128, i.e. 50% opacity. To revert back to full opaqueness use the above API with the variable Level set to 255 or 100% opaque.

You can even attach the code to a scroll bar event for an interactive opacity setting. When using, the transparency setting can be done easily from the properties tab by changing the value of the “opacity” setting. You can also change the setting manually using the Opacity property, as in, Form1.Opacity=.50, where Form1 is the name of the form in question, and .50 is an opacity level of 50%. This can vary between 0 and 1.

Ankit Khare

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.