Making A Form Transparent (But with visible controls)
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.
Re: Making A Form Transparent
Re: Making A Form Transparent
SetLayeredWindowAttributes is supported only in Win2K and later.
This code may not work on Win9x/Me. I haven't tested it though. :)
Re: Making A Form Transparent
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.
Re: Making A Form Transparent
Not supported in 98 or ME. Only Windows 200/XP/Vista.
Re: Making A Form Transparent
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
Re: Making A Form Transparent
From your post #4
Quote:
Requires Windows 2000 or later; Win9x/ME: Not supported
Re: Making A Form Transparent
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.
Re: Making A Form Transparent
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.
Re: Making A Form Transparent
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)
Quote:
Originally Posted by Hack
... but the only problem I had with this code on Win98 was with labels.
How ? :confused:
Re: Making A Form Transparent
Error trapping for 453 and handling it so the program would continue to run and not crash?
Re: Making A Form Transparent
Yes. May be. :thumb:
[SLAP]iPrank[/SLAP]
Re: Making A Form Transparent
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!
Re: Making A Form Transparent
Try passing the picturebox handle to the APIs.
Re: Making A Form Transparent (But with visible controls)
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.
Re: Making A Form Transparent (But with visible controls)
Nice Code.
How do i undo it though?
Re: Making A Form Transparent (But with visible controls)
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
Re: Making A Form Transparent (But with visible controls)
changing the form backcolor and just the picture box to cyan did not work, it still goes throught the form also.
1 Attachment(s)
Re: Making A Form Transparent (But with visible controls)
Quote:
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:
Re: Making A Form Transparent (But with visible controls)
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
Re: Making A Form Transparent (But with visible controls)
I want to see to the form background, not through that part of the form also
Re: Making A Form Transparent (But with visible controls)