Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-12-2008
Member
 
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 ?
Reply With Quote
  #2  
Old 24-12-2008
Member
 
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
Reply With Quote
  #3  
Old 24-12-2008
XSI XSI is offline
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 08:07 AM.