by Big Al (4 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 2nd May 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Set the transparency of a form and then back to normal again.
API Declarations
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_COLORKEY = &H1
Private Const LWA_ALPHA = &H2
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 crColor As Long, ByVal nAlpha As Byte, ByVal dwFlags As Long) As Long
below 20% because you would not be able to see form to close
'Place code in Module
Public Function Transparent(frm As Form, TransPct As Long)
Dim Alpha As Long
If TransPct < 20 Or TransPct > 100 Then GoTo transErr:
Call SetWindowLong(frm.hwnd, GWL_EXSTYLE, GetWindowLong _
(frm.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
Alpha = 255 * TransPct
Alpha = Alpha * 0.01
If Alpha <> 255 Then
Call SetLayeredWindowAttributes(frm.hwnd, RGB(255, 0, 255), _
Alpha, LWA_ALPHA Or LWA_COLORKEY)
Else
Call SetLayeredWindowAttributes(frm.hwnd, RGB(255, 0, 255), _
255, LWA_COLORKEY)
End If
Exit Function
transErr:
MsgBox "Percentage value must be between 20 - 100.", vbCritical
End Function
'Sample sets transparency to 50%, _
simply set transparency back to 100(100%) _
to return form to normal
'Private Sub Command1_Click()
'Transparent me, 50
'end sub