Results 1 to 3 of 3

Thread: How to make a transparent form in VB 6.0

  1. #1
    Join Date
    Nov 2008
    Posts
    68

    How to make a transparent form in VB 6.0

    Hello everyone!

    I am making a project in VB 6.0. What I want to know is how to make a form transparent ?

  2. #2
    Join Date
    Feb 2008
    Posts
    194

    Re: How to make a transparent form in VB 6.0

    In Module:

    Code:
    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
    In form:

    Code:
    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

  3. #3
    Join Date
    May 2008
    Posts
    271

    Code for Transparent or Opaque Forms in VB 6

    Here is the code to make a form either Transparent or Opaque in VB 6.0

    Code:
    Option Explicit
    
    Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
    Private Declare Function UpdateLayeredWindow Lib "user32" (ByVal hwnd As Long, ByVal hdcDst As Long, pptDst As Any, psize As Any, ByVal hdcSrc As Long, pptSrc As Any, crKey As Long, ByVal pblend As Long, ByVal dwFlags 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 SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Private Const GWL_EXSTYLE = (-20)
    Private Const LWA_COLORKEY = &H1
    Private Const LWA_ALPHA = &H2
    Private Const ULW_COLORKEY = &H1
    Private Const ULW_ALPHA = &H2
    Private Const ULW_OPAQUE = &H4
    Private Const WS_EX_LAYERED = &H80000
    
    Public Function isTransparent(ByVal hwnd As Long) As Boolean
    On Error Resume Next
    Dim Msg As Long
    Msg = GetWindowLong(hwnd, GWL_EXSTYLE)
    If (Msg And WS_EX_LAYERED) = WS_EX_LAYERED Then
      isTransparent = True
    Else
      isTransparent = False
    End If
    If Err Then
      isTransparent = False
    End If
    End Function
    
    Public Function MakeTransparent(ByVal hwnd As Long, Perc As Integer) As Long
    Dim Msg As Long
    On Error Resume Next
    If Perc < 0 Or Perc > 255 Then
      MakeTransparent = 1
    Else
      Msg = GetWindowLong(hwnd, GWL_EXSTYLE)
      Msg = Msg Or WS_EX_LAYERED
      SetWindowLong hwnd, GWL_EXSTYLE, Msg
      SetLayeredWindowAttributes hwnd, 0, Perc, LWA_ALPHA
      MakeTransparent = 0
    End If
    If Err Then
      MakeTransparent = 2
    End If
    End Function
    
    Public Function MakeOpaque(ByVal hwnd As Long) As Long
    Dim Msg As Long
    On Error Resume Next
    Msg = GetWindowLong(hwnd, GWL_EXSTYLE)
    Msg = Msg And Not WS_EX_LAYERED
    SetWindowLong hwnd, GWL_EXSTYLE, Msg
    SetLayeredWindowAttributes hwnd, 0, 0, LWA_ALPHA
    MakeOpaque = 0
    If Err Then
      MakeOpaque = 2
    End If
    End Function

Similar Threads

  1. How To Make Image Transparent in GIMP?
    By Landry in forum Customize Desktop
    Replies: 6
    Last Post: 22-08-2011, 01:20 PM
  2. How to make bmp image transparent
    By SMG in forum Windows Software
    Replies: 7
    Last Post: 27-08-2010, 05:17 PM
  3. transparent background for form in access 2003
    By noscut in forum Windows Software
    Replies: 4
    Last Post: 02-09-2009, 05:23 PM
  4. How to make an IFrame transparent?
    By Sachit in forum Software Development
    Replies: 3
    Last Post: 13-02-2009, 08:44 PM
  5. How to make transparent buttons in C#
    By AmdUser in forum Software Development
    Replies: 5
    Last Post: 01-12-2008, 01:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,107,632.92388 seconds with 17 queries