Saturday, 18 June 2011

Transparent VBA Userform

We can set the transparency level (alpha) to the VBA Userform. Please find the code below. copy paste to the userform module.


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _

    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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&

Public hwnd As Long

Private Sub UserForm_Initialize()
    Dim bytOpacity As Byte
    bytOpacity = 192 ' variable keeping opacity setting
    hwnd = FindWindow("ThunderDFrame", Me.Caption)
    Call SetWindowLong(Me.hwnd, GWL_EXSTYLE, GetWindowLong(Me.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
    Call SetLayeredWindowAttributes(Me.hwnd, 0, bytOpacity, LWA_ALPHA)
End Sub

No comments:

Post a Comment