[VB6] Making a form transparent

Last update on November 19, 2008 07:20 AM by netty5
Published by netty5

[VB6] Making a form transparent





Getting started


Here is a brief explanation on how to use the transparency based upon:
The desired format, including controls, according to a ratio.
Having the bottom of the form be invisible and controls, according to a ratio.

Restriction


Does not work on color having negative sign.
A color palette must be included.

In a module


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 Long, ByVal bDefaut As Byte, ByVal dwFlags As Long) As Long

Private Const GWL_EXSTYLE       As Long = (-20)
Private Const LWA_COLORKEY      As Long = &H1
Private Const LWA_Defaut         As Long = &H2
Private Const WS_EX_LAYERED     As Long = &H80000

'
Public Function Transparency(ByVal hWnd As Long, Optional ByVal Col As Long = vbBlack, _
    Optional ByVal PcTransp As Byte = 255, Optional ByVal TrMode As Boolean = True) As Boolean
' Return : True if there is no error.
' hWnd   : hWnd of the window to make transparent
' Col : Color to make transparent if TrMode=False
' PcTransp  : 0 à 255 >> 0 = transparent  -:- 255 = Opaque
Dim DisplayStyle As Long
    On Error GoTo Exit
    VoirStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
    If DisplayStyle<> (DisplayStyle Or WS_EX_LAYERED) Then
        DisplayStyle = (DisplayStyle Or WS_EX_LAYERED)
        Call SetWindowLong(hWnd, GWL_EXSTYLE, DisplayStyle)
    End If
    Transparency = (SetLayeredWindowAttributes(hWnd, Col, PcTransp, IIf(TrMode, LWA_COLORKEY Or LWA_Defaut, LWA_COLORKEY)) <> 0)
    
Exit:
    If Not Err.Number = 0 Then Err.Clear
End Function

Public Sub ActiveTransparency(M As Form, d As Boolean, F As Boolean, _
     T_Transparency As Integer, Optional Color As Long)
Dim B As Boolean
        If d And F Then
        'Makes color (here the background color of the shape) transparent
        'upon value of T_Transparency
            B = Transparency(M.hWnd, Color, T_Transparency, False)
        ElseIf d Then
            'Makes form, including all components, transparent
            'upon value of T_Transparency
            B = Transparency(M.hWnd, 0, T_Transparency, True)
        Else
            'Restores the form opaque.
            B = Transparency(M.hWnd, , 255, True)
        End If
End Sub

As used in a form


Private Sub Form_Load()
Dim i As Integer
    'Ex: all transparent at ratio 140/255
    'ActiveTransparency Me, True, False, 140, Me.BackColor
    'Ex: Form transparent, visible component at ratio 140/255
    'ActiveTransparency Me, True, True, 140, Me.BackColor
    
    'Example display the form transparency degradation
    ActiveTransparency Me, True, False, 0
    Me.Show
    For i = 0 To 255 Step 3
        ActiveTransparency Me, True, False, i
        Me.Refresh
    Next i
End Sub
Best answers for « Making a form transparent » in :
Create a transparent DIV Show Create a transparent DIV Make the DIV (with the content) transparent Making transparent DIV (without the content) The transparent DIV in CSS To make a transparent DIV, you can make use of the CSS techniques listed below: Make the...
[Latex] [Beamer] Transparent A4 portrait Show [Latex] [Beamer] Transparent A4 portrait Transparencies for A4 portrait with Beamer A small tip , that will be of great use, if you are using Latex, specially with the Beamer class, in order to make transparencies, you may need to...
Make an ISO image ShowMake an ISO image What is an ISO image? How to create an ISO image How to burn an ISO file to a CD-ROM/DVD-ROM What is an ISO image? An ISO image is created by a particular software and is the replica of an existing file system. This...
How to make a video clip ? ShowMaking your video clip Introduction With video With a camcorder Writing the script Recording Acquisition Video composition With the pictures With the pictures Video composition Software to make a slideshow With videos and...
Making calls from your computer ShowIntroduction to VoIP Since the 21st century, the bell telephone first developed by Alexander Graham Bell has made a huge evolution. Nowadays people can even make voice calls from their computer. There are several ways for making calls throughout...
Download Windows Movie Maker ShowDescription: Windows Movie Maker is surprisingly easy and amusing for family realization of films, by allowing you to create, to edit and to share your films on your own computer by of simple slip move. You can add special effects, music and...
Download Theme Maker ShowTo obtain good results, it is necessary to work in the best conditions and in a customized work environment. Theme Maker is a program which allows to customize, create or of change your cursors, your icons and bottom of screen. Modify and customize...
HTML Markup ShowHTML, A Markup Language HTML is not a programming language. Rather, it is used for describing the layout and format of content, written in the form of simple text. An HTML page is a basic text file containing tags for specifying text format,...