WinAPI : Making Forms Translucent
Your Application Window hides everything behind it, which is the default behaviour of all windows. Making the window transparent on the other hand hides the form and all static controls as label over it, to make the background visible. Well there's a third way! using SetLayeredWindowAttributes function. SetLayeredWindowAttributes function enables to set the form's opacity such that the form and the controls contained in it as well as the background are visible. The function is available on Windows 2000 and above versions and works on Layered Windows.
Now what's the Layered Window?
All windows by default are assigned WS_OVERLAPPED style, making it hide everything that's behind it. Contrast to it is the WS_EX_LAYERED style which enables accessing other windows behind it.
Let's get into the details of the SetLayeredWindowAttributes function. The function takes four parameters, first of which is the handle to the window on which function is to be applied. The second and the third parameter are respectively Color key and alpha, only one of which is to be given. The fourth parameter is the flag which specifies whether second or third parameter is to be considered while applying the function and can be one of the constants defined below.
For now we will focus on changing the opacity of the form which is achieved by populating the third parameter bAlpha which takes value from 0 (completely transparent) to 255 (Opaque)
Private Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hWnd As Long, ByVal crKey As Byte, ByVal bAlpha As Byte, _
ByVal dwFlags As Long) As Long
Const LWA_COLORKEY = &H1
Const LWA_ALPHA = &H2
Now as mentioned above the function works only on Layered windows for which we will require to change the Window's Extended style to include WS_EX_LAYERED using GetWindowLong and SetWindowLong as already descibed in pervious posts Modifying Minimize, Maximize buttons during runtime and WinAPI : Making Form Transparent.
Create a new project and make the required function and constant declarations.
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 crKey As Byte, ByVal bAlpha As Byte, _
ByVal dwFlags As Long) As Long
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const LWA_COLORKEY = &H1
Const LWA_ALPHA = &H2
Public lngAlpha As Long
lngAlpha variable as declared above will take values from 0 to 255 according to which the layered window's attributes would be changed. We would set the value of alpha as 150 in the load event of the form. Before applying the alpha we would require to change the window's extended style to include WS_EX_LAYERED style, and then apply the SetLayeredWindowAttributes function.
Private Sub Form_Load()
lngAlpha = 150
Dim lngCurStyle As Long
Dim lngNewStyle As Long
lngCurStyle = GetWindowLong(Form1.hWnd, GWL_EXSTYLE)
lngNewStyle = lngCurStyle Or WS_EX_LAYERED
SetWindowLong Form1.hWnd, GWL_EXSTYLE, lngNewStyle
SetLayeredWindowAttributes Form1.hWnd, 0, lngAlpha, LWA_ALPHA
End Sub

Add a control or two on the form and that's all. Execute to see the effect. Now let's make opacity changes dynamically, say when mouse is moved with Left button and Shift key pressed the opacity increases and similarly decreases with Right button and Shift key.
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Shift = 1 Then
If Button = 2 And lngAlpha > 25 Then
lngAlpha = lngAlpha - 5
End If
If Button = 1 And lngAlpha < 220 Then
lngAlpha = lngAlpha + 5
End If
SetLayeredWindowAttributes Form1.hWnd, 0, lngAlpha, LWA_ALPHA
End If
End Sub
That's all with opacity. Let's take the other option i.e. using color key next time.



