How do u make objects transparent?

I need to make an object transparent.  I need it so the person using my program can set the transparentcy of the List box.  How would I do that?
Gamemaster328Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
planetULTRAConnect With a Mentor Commented:
0
 
DeAnCommented:
ListBox doesn't have a BackStyle property to set Transparent.  do you mean invisible?
0
 
DeAnCommented:
if so, this will toggle visibility:

List1.Visible = Not List1.Visible
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
WarLordCommented:
???

i would use list1.visible = false
0
 
Gamemaster328Author Commented:
oh no. not that kind of transparent.  like a piece of plastic you can see through it.  that kind of transparent.
0
 
DeAnCommented:
I said it toggles the visibility.

0
 
planetULTRACommented:
Create a new Classmodule and call it clsAlpha

Then copy&paste the following code


''''''''''''''''''''''''''''''''''''''''''


Option Explicit

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 RedrawWindow Lib "user32" _
    (ByVal hwnd As Long, _
    lprcUpdate As RECT, _
    ByVal hrgnUpdate As Long, _
    ByVal fuRedraw As Long) As Long

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 Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_ALPHA = &H2

Private Const RDW_ALLCHILDREN = &H80
Private Const RDW_ERASE = &H4
Private Const RDW_FRAME = &H400
Private Const RDW_INVALIDATE = &H1

Friend Sub SetLayered(ByVal hwnd As Long, ByVal bolSetAs As Boolean, ByVal bAlpha As Byte)
   
    Dim nullRect As RECT
    Dim lret As Long
   
    lret = GetWindowLong(hwnd, GWL_EXSTYLE)

    If bolSetAs = True Then
        lret = lret Or WS_EX_LAYERED
    Else
        lret = lret And Not WS_EX_LAYERED
    End If
   
    SetWindowLong hwnd, GWL_EXSTYLE, lret
   
    If bolSetAs Then
        SetLayeredWindowAttributes hwnd, 0, bAlpha, LWA_ALPHA
    Else
        RedrawWindow hwnd, nullRect, 0&, RDW_ALLCHILDREN Or RDW_ERASE Or RDW_FRAME Or RDW_INVALIDATE
    End If
End Sub

Friend Sub ReleaseDisplay(ByVal hwnd As Long)
    SetLayered hwnd, False, 255
End Sub


''''''''''''''''''''''''''''''''''''''''''

I think the code is easy to understand. All you need is
the handle (hwnd) of your form/control.

For example, on your form create a scrollbar.
Set its maximum to 255 and minimum to 0.

In its "Scroll" event insert the following code:

''''''''''''''''''''''''''''''''''''
   objAlpha.SetLayered listview.hwnd, True, CByte(scrollbar.Value)
   Me.Refresh
   scrollbar.Refresh
'''''''''''''''''''''''''''''''''''''

WARNING!!: This code only works under Win2K/XP
Any windows versions below Windows 2000 don't have the
SetLayeredWindowAttributes API implemented yet.
So transparency below win2k is impossible.
0
 
Gamemaster328Author Commented:
it doesn't work. It keeps saying there is something wrong with this one:

objAlpha.SetLayered listview.hwnd, True, CByte(scrollbar.Value)
0
 
Gamemaster328Author Commented:
since im new at programing this kind of thing?  what does hwnd mean?  what do i have to replace with it?
0
 
planetULTRACommented:
of course you have to replace the listview.hwnd
with NAMEOFYOURCONTROL.hwnd

and the scrollbar.value replace with NAMEOFYOURSCROLLBAR.value

A hwnd, or handle, is a long number that windows gives every
control. You can communicate with such controls by using
its hwnd(handle number).

Try replacing the listview.hwnd with me.hwnd. Then the
whole form should become transparent (me.hwnd gives the
handle of your form)
0
 
Gamemaster328Author Commented:
it still doesn't work. :-(

i did what u said but i didn't what to make the form that the scroll bar was in transparent. so i changed it.

Private Sub form4_transparent_Scroll()
    objAlpha.SetLayered Form1.hwnd, True, CByte(form4_transparent.Value)
    Me.Refresh
    form4_transparent.Refresh
End Sub

i know that there is something wrong with this so plz dont kill me!

the scrollbar's name is form4_transparent
the form that the scrollbar is in is form4
and the form im trying to make transparent is form1
0
 
planetULTRACommented:
AAAAH I'm so stupid! Please forgive me!

At first instead of me.refresh type form1.refresh.

Then I forgot to post some declarations:

In the form which contains the scrollbar (you can also
use a slider) paste (or type) the following code at the
beginning


''''''''''''''''''''''''''''''''''''''

Option Explicit

' Variable to hold the instance of our class
Dim objAlpha As clsAlpha

Private Sub Form_Load()
    ' Load an instance of the class object into the variable.

    Set objAlpha = New clsAlpha
End sub

'''''''''''''''''''''''''''''''''''''''

It was my fault! Please excuse me!
Now it should work
0
 
Gamemaster328Author Commented:
oh ok that you let me see if it works
0
 
Gamemaster328Author Commented:
now its saying that there is something wrong with this code:

Dim objAlpha As clsAlpha

(the objAlpha As clsAlpha is wrong it says.)
0
 
planetULTRACommented:
Then you forgot to call the classmodule clsAlpha
0
 
Gamemaster328Author Commented:
o ok
let me see if it works this time
0
 
Gamemaster328Author Commented:
still not working

    objAlpha.SetLayered clsAlpha.hwnd, True, CByte(form4_transparent.Value)
    Form1.Refresh
    Me.Refresh
0
 
Gamemaster328Author Commented:
runtime error 404

object required
0
 
planetULTRACommented:
Are you sure you have at least Windows 2000?
0
 
Gamemaster328Author Commented:
yes
0
 
Gamemaster328Author Commented:
IT WORKS!!!!  thanks.  i dont know who to give the points to though.

if i could i would like to split it up but i cant. (i dont want to add more)

how should i settle this?

i guess im gonna have to give it to planetUltra.
0
 
planetULTRACommented:
I'm glad that I could help you!
0
 
bzzCommented:
planetUltra: I spent a couple of hours finding a solution for making a single control transparent, and came across this proposed solution. It does not appear to work, and from what I've seen on similar topics it will not. As far as I can tell, Visual Basic only supports transparency for an entire window, not for individual controls.

I would gladly be proven wrong, since the solution I'm using now is more elaborate.

PS: The link to the example is not valid.
0
All Courses

From novice to tech pro — start learning today.