VBA TIPS
Transparent Userform

This code makes an userform semitransparently. Place the following controls on an Userform.
  • CommandButton1
  • ScrollBar1
  • Label1

Please note, the userform will be invisible when you move scroll bar to 100%. It means you cannot control that userform anymore.

 


Private Declare Function GetActiveWindow Lib "USER32" () As Long

Private Declare Function SetWindowLong Lib "USER32" _
                                       Alias "SetWindowLongA" ( _
                                                               ByVal hWnd As LongByVal lngWinIdx As Long, _
                                                               ByVal dwNewLong As LongAs Long

Private Declare Function GetWindowLong Lib "USER32" _
                                       Alias "GetWindowLongA" ( _
                                                               ByVal hWnd As LongByVal lngWinIdx As LongAs Long

Private Declare Function SetLayeredWindowAttributes Lib "USER32" ( _
                                                                  ByVal hWnd As LongByVal crKey As Integer, _
                                                                  ByVal bAlpha As IntegerByVal dwFlags As LongAs Long

Private Const WS_EX_LAYERED = &H80000
Private Const LWA_COLORKEY = &H1
Private Const LWA_ALPHA = &H2
Private Const GWL_EXSTYLE = &HFFEC
Dim hWnd As Long


Private Sub CommandButton1_Click()
    Unload Me
End Sub


Private Sub ScrollBar1_Change()
    Call Semitransparent(Me.ScrollBar1.Value)
End Sub


Private Sub UserForm_Activate()
    Me.ScrollBar1.Value = 50
End Sub


Private Sub Semitransparent(ByVal intLevel As Integer)
    Dim lngWinIdx As Long
    hWnd = GetActiveWindow
    lngWinIdx = GetWindowLong(hWnd, GWL_EXSTYLE)
    SetWindowLong hWnd, GWL_EXSTYLE, lngWinIdx Or WS_EX_LAYERED
    SetLayeredWindowAttributes hWnd, 0, (255 * intLevel) / 100, LWA_ALPHA
    Label1.Caption = "Semitransparent level is ..." & (100 - intLevel) & "%"
End Sub


| HOME |
Copyright © cellmasters.net - colo's junk room All Right Reserved
ABOUT
WORKS
THE CELL MASTERS
CONTACT
LINKS
Tips and Information about Microsoft Excel|Masaru Kaji aka Colo