Results 1 to 6 of 6

Thread: Transparent Form

  1. #1

    Thread Starter
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Transparent Form

    SetTrans procedure sets a form transparent depending on your passed value
    julst call it like this

    settrans frm1.hwnd, 100

    VB Code:
    1. Option Explicit
    2.  
    3. Public Const GWL_EXSTYLE As Long = -20
    4. Public Const WS_EX_LAYERED As Long = &H80000
    5. Public Const LWA_ALPHA As Long = &H2
    6.  
    7. Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    8. 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
    9. Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    10. Public Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    11.  
    12. Public Sub SetTrans(ByVal OB_Hwnd As Long, ByVal OB_Val As Integer)
    13.     Dim FTransVal As Long
    14.     Dim Attrib As Long
    15.     Attrib = GetWindowLong(OB_Hwnd, GWL_EXSTYLE)
    16.     SetWindowLong OB_Hwnd, GWL_EXSTYLE, Attrib Or WS_EX_LAYERED
    17.     SetLayeredWindowAttributes OB_Hwnd, RGB(0, 0, 0), OB_Val, LWA_ALPHA
    18.     FTransVal = OB_Val
    19.     Exit Sub
    20. 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

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    2.                 ByVal hwnd As Long, _
    3.                 ByVal nIndex As Long) As Long
    4.  
    5. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
    6.                 ByVal hwnd As Long, _
    7.                 ByVal nIndex As Long, _
    8.                 ByVal dwNewLong As Long) As Long
    9.                
    10. Private Declare Function SetLayeredWindowAttributes Lib "user32" ( _
    11.                 ByVal hwnd As Long, _
    12.                 ByVal crKey As Long, _
    13.                 ByVal bAlpha As Byte, _
    14.                 ByVal dwFlags As Long) As Long
    15.  
    16. Private Const GWL_EXSTYLE = (-20)
    17. Private Const WS_EX_LAYERED = &H80000
    18. Private Const LWA_ALPHA = &H2
    19.  
    20. Private Sub SetTrans(ByVal lhWnd As Long, ByVal lAlpha As Byte)
    21.     SetWindowLong lhWnd, GWL_EXSTYLE, GetWindowLong(lhWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
    22.     SetLayeredWindowAttributes lhWnd, 0&, lAlpha, LWA_ALPHA
    23. End Sub

  3. #3

    Thread Starter
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    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

  4. #4

    Thread Starter
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: Transparent Form

    Quote 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:
    1. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    2.                 ByVal hwnd As Long, _
    3.                 ByVal nIndex As Long) As Long
    4.  
    5. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
    6.                 ByVal hwnd As Long, _
    7.                 ByVal nIndex As Long, _
    8.                 ByVal dwNewLong As Long) As Long
    9.                
    10. Private Declare Function SetLayeredWindowAttributes Lib "user32" ( _
    11.                 ByVal hwnd As Long, _
    12.                 ByVal crKey As Long, _
    13.                 ByVal bAlpha As Byte, _
    14.                 ByVal dwFlags As Long) As Long
    15.  
    16. Private Const GWL_EXSTYLE = (-20)
    17. Private Const WS_EX_LAYERED = &H80000
    18. Private Const LWA_ALPHA = &H2
    19.  
    20. Private Sub SetTrans(ByVal lhWnd As Long, ByVal lAlpha As Byte)
    21.     SetWindowLong lhWnd, GWL_EXSTYLE, GetWindowLong(lhWnd, GWL_EXSTYLE) Or WS_EX_LAYERED
    22.     SetLayeredWindowAttributes lhWnd, 0&, lAlpha, LWA_ALPHA
    23. 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:
    1. Option Explicit
    2.  
    3. Public Const GWL_EXSTYLE As Long = -20
    4. Public Const WS_EX_LAYERED As Long = &H80000
    5. Public Const LWA_ALPHA As Long = &H2
    6.  
    7. Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    8. 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
    9. Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    10.  
    11. Public Sub SetTrans(ByVal OB_Hwnd As Long, ByVal OB_Val As Integer)
    12.     SetWindowLong OB_Hwnd, GWL_EXSTYLE, GetWindowLong(OB_Hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
    13.     SetLayeredWindowAttributes OB_Hwnd, RGB(0, 0, 0), OB_Val, LWA_ALPHA
    14. 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

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Transparent Form

    Quote 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.

  6. #6
    Member
    Join Date
    Apr 2008
    Posts
    46

    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
  •  



Click Here to Expand Forum to Full Width