# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  Making A Form Transparent (But with visible controls)

## bushmobile

To make form transparent, but controls visible:

VB 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 bAlpha As Byte, _
                ByVal dwFlags As Long) As Long
 Private Const GWL_STYLE = (-16)
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const LWA_COLORKEY = &H1
Private Const LWA_ALPHA = &H2
 Private Sub Form_Load()
    Me.BackColor = vbCyan
    SetWindowLong Me.hwnd, GWL_EXSTYLE, GetWindowLong(Me.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
    SetLayeredWindowAttributes Me.hwnd, vbCyan, 0&, LWA_COLORKEY
End Sub
It actually just makes anything coloured vbCyan to become transparent.

----------


## Hack

Moved to the CodeBank

----------


## iPrank

SetLayeredWindowAttributes is supported only in Win2K and later. 
This code may not work on Win9x/Me. I haven't tested it though.  :Smilie:

----------


## Hack

I have no idea about ME (never used it), but the only problem I had with this code on Win98 was with labels.

They didn't show up at all.   I have to replace them with textboxes that looked like they were labels.

----------


## RobDog888

Not supported in 98 or ME. Only Windows 200/XP/Vista.

----------


## iPrank

Justed tested it in Win98.
*Run-time error '453': Can't find DLL entry point SetLayeredWindowAttributes  in user32*

Further reading: http://www.codeproject.com/w2k/QDWndTransparency.asp

----------


## RobDog888

From your post #4



> Requires Windows 2000 or later; Win9x/ME: Not supported

----------


## iPrank

I know. But Hack mentioned the 'only' problem he had on Win98 is with labels.
That means he is able to run this code in Win98. Isn't it ?

PS: I don't have VB in Win98. I run it from compiled exe.

----------


## RobDog888

If you make a very basic exe then you should only need the msvbvmm60.dll on your 98 system and the SetLayeredWindowAttributes  is in th user32 kernal so it should run but give you an error in the user32 dll.

----------


## iPrank

That's exactly my point. 
That code references the API at compiletime. It doesn't uses a LoadLibrary like the link I posted in post #6.

So, Hack should get an error 453 just after doubleclicking the exe.

But he is probably able to run it in Win98. (as he said in post#4)



> ... but the only problem I had with this code on Win98 was with labels.


How ?  :Confused:

----------


## RobDog888

Error trapping for 453 and handling it so the program would continue to run and not crash?

----------


## iPrank

Yes. May be.  :Thumb: 

[SLAP]iPrank[/SLAP]

----------


## lilwupster

i really like it, one question, if i had a picture box i wanted transparent can i make it so the picture box is transparent but the form isn't so i can see whats on the form.  Otherwise its great!

----------


## RobDog888

Try passing the picturebox handle to the APIs.

----------


## bushmobile

This will only work for top-level windows so passing the hWnd of the child won't do anything. If you replace:


VB Code:
Me.BackColor = vbCyan
 ' with
 Picture1.BackColor = vbCyan
Then just the picturebox will be made transparent.

----------


## kazar

Nice Code.
How do i undo it though?

----------


## bushmobile

The easiest way is probably to remove the WindowStyle:


VB Code:
Private Sub Command1_Click()
    Dim lStyle As Long
    lStyle = GetWindowLong(Me.hwnd, GWL_EXSTYLE)
    If (lStyle And WS_EX_LAYERED) = WS_EX_LAYERED Then SetWindowLong Me.hwnd, GWL_EXSTYLE, lStyle Xor WS_EX_LAYERED
    Me.BackColor = vbButtonFace
End Sub

----------


## lilwupster

changing the form backcolor and just the picture box to cyan did not work, it still goes throught the form also.

----------


## bushmobile

> it still goes throught the form also


What do you mean by that?

Do not set the form's backcolor to vbCyan, only set the picturebox's backcolor to vbCyan:

----------


## kazar

Sorry to cross between this thread and vba, but you can use this in vba as well if you use the following code:


VB Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) 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 Declare Function SetLayeredWindowAttributes Lib "user32" ( _
                ByVal hwnd As Long, _
                ByVal crKey As Long, _
                ByVal bAlpha As Byte, _
                ByVal dwFlags As Long) As Long
 Private Const GWL_STYLE = (-16)
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const LWA_COLORKEY = &H1
Private Const LWA_ALPHA = &H2
Dim formhandle as Long
 Private Sub Form_Load()
   formhandle = FindWindow(vbNullString, Me.Caption)
SetWindowLong formhandle, GWL_EXSTYLE, GetWindowLong(formhandle, GWL_EXSTYLE) Or WS_EX_LAYERED
    SetLayeredWindowAttributes formhandle, vbCyan, 0&, LWA_COLORKEY
Me.BackColor = vbCyan
End Sub

----------


## lilwupster

I want to see to the form background, not through that part of the form also

----------


## bushmobile

maybe this might be of use to you then: http://www.vbforums.com/showthread.php?p=1992354

----------

