How to Make Windows Form Transparent using Visual Basic 6/VB6

The following code snippets allows you to make windows form transparent using VB6.

In a standard module, copy and paste the following declarations and function.
[Module1.bas]
Option Explicit

Const LWA_COLORKEY = 1
Const LWA_ALPHA = 2
Const LWA_BOTH = 3
Const WS_EX_LAYERED = &H80000
Const GWL_EXSTYLE = -20

Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal color As Long, ByVal x As Byte, ByVal alpha As Long) As Boolean
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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Type POINTAPI
        x As Long
        y As Long
End Type

Private pPoint As POINTAPI
Private hMouseOverWnd As Long
Public Trans As Integer

Public Function SetTrans(hWnd As Long, Trans As Integer)

    Dim Tcall As Long

    If Trans <= 0 Then
        Exit Function
    Else
        Tcall = GetWindowLong(hWnd, GWL_EXSTYLE)
        SetWindowLong hWnd, GWL_EXSTYLE, Tcall Or WS_EX_LAYERED
        SetLayeredWindowAttributes hWnd, RGB(255, 255, 0), Trans, LWA_ALPHA
    End If

End Function

To apply the transparency, you can call the SetTrans() function on Form_Load() event of the form you want to be transparent.
[Form Code]
Private Sub Form_Load()
SetTrans frmMain.hWnd, 220
End Sub

Comments

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Set Windows Form Always on Top of Other Applications in VB6