|
-
Apr 12th, 2006, 12:06 AM
#1
Thread Starter
Frenzied Member
Transparent Form
SetTrans procedure sets a form transparent depending on your passed value
julst call it like this
settrans frm1.hwnd, 100
VB Code:
Option Explicit
Public Const GWL_EXSTYLE As Long = -20
Public Const WS_EX_LAYERED As Long = &H80000
Public Const LWA_ALPHA As Long = &H2
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Public Sub SetTrans(ByVal OB_Hwnd As Long, ByVal OB_Val As Integer)
Dim FTransVal As Long
Dim Attrib As Long
Attrib = GetWindowLong(OB_Hwnd, GWL_EXSTYLE)
SetWindowLong OB_Hwnd, GWL_EXSTYLE, Attrib Or WS_EX_LAYERED
SetLayeredWindowAttributes OB_Hwnd, RGB(0, 0, 0), OB_Val, LWA_ALPHA
FTransVal = OB_Val
Exit Sub
End Sub
Last edited by d3gerald; Apr 17th, 2006 at 09:17 PM.
On error goto Trap
Trap:
in case of emergency, drop the case...
****************************************
If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option. if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar
-
Apr 12th, 2006, 05:27 AM
#2
Re: Transparent Form
The reference to SetParent, the On Error Resume Next and the Exit Sub are all redundant + you can put it all on a couple of lines.
lAlpha: 0 is transparent, 255 is solid, anything inbetween is a grade of translucency. This will only work with top level windows on 2000 and above. See http://www.vbforums.com/showthread.php?t=396385 for transparent form, but visible controls.
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_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const LWA_ALPHA = &H2
Private Sub SetTrans(ByVal lhWnd As Long, ByVal lAlpha As Byte)
SetWindowLong lhWnd, GWL_EXSTYLE, GetWindowLong(lhWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
SetLayeredWindowAttributes lhWnd, 0&, lAlpha, LWA_ALPHA
End Sub
-
Apr 17th, 2006, 09:14 PM
#3
Thread Starter
Frenzied Member
Re: Transparent Form
actually that code is just a snipet from my application. those declarations have some other use in mine but i need not to post them i think.
thanks for the comment bush
On error goto Trap
Trap:
in case of emergency, drop the case...
****************************************
If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option. if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar
-
Apr 17th, 2006, 09:23 PM
#4
Thread Starter
Frenzied Member
Re: Transparent Form
 Originally Posted by bushmobile
The reference to SetParent, the On Error Resume Next and the Exit Sub are all redundant + you can put it all on a couple of lines.
lAlpha: 0 is transparent, 255 is solid, anything inbetween is a grade of translucency. This will only work with top level windows on 2000 and above. See http://www.vbforums.com/showthread.php?t=396385 for transparent form, but visible controls.
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_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000
Private Const LWA_ALPHA = &H2
Private Sub SetTrans(ByVal lhWnd As Long, ByVal lAlpha As Byte)
SetWindowLong lhWnd, GWL_EXSTYLE, GetWindowLong(lhWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
SetLayeredWindowAttributes lhWnd, 0&, lAlpha, LWA_ALPHA
End Sub
i tried your proposed correction but it didnt work coz you forgot to declare the SetLayeredWindowAttributes API. this declaration is needed by that process. this code works really fine now like the first one
VB Code:
Option Explicit
Public Const GWL_EXSTYLE As Long = -20
Public Const WS_EX_LAYERED As Long = &H80000
Public Const LWA_ALPHA As Long = &H2
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Sub SetTrans(ByVal OB_Hwnd As Long, ByVal OB_Val As Integer)
SetWindowLong OB_Hwnd, GWL_EXSTYLE, GetWindowLong(OB_Hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
SetLayeredWindowAttributes OB_Hwnd, RGB(0, 0, 0), OB_Val, LWA_ALPHA
End Sub
On error goto Trap
Trap:
in case of emergency, drop the case...
****************************************
If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option. if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar
-
Apr 18th, 2006, 04:13 AM
#5
Re: Transparent Form
 Originally Posted by d3gerald
i tried your proposed correction but it didnt work coz you forgot to declare the SetLayeredWindowAttributes API
eh? What are you on about? It's there in post #2.
-
Jun 27th, 2008, 01:39 AM
#6
Member
Re: Transparent Form
Thanks Man. May i use this in my app?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|