Results 1 to 22 of 22

Thread: Making A Form Transparent (But with visible controls)

  1. #1

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

    Making A Form Transparent (But with visible controls)

    To make form transparent, but controls visible:
    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_STYLE = (-16)
    17. Private Const GWL_EXSTYLE = (-20)
    18. Private Const WS_EX_LAYERED = &H80000
    19. Private Const LWA_COLORKEY = &H1
    20. Private Const LWA_ALPHA = &H2
    21.  
    22. Private Sub Form_Load()
    23.     Me.BackColor = vbCyan
    24.     SetWindowLong Me.hwnd, GWL_EXSTYLE, GetWindowLong(Me.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED
    25.     SetLayeredWindowAttributes Me.hwnd, vbCyan, 0&, LWA_COLORKEY
    26. End Sub
    It actually just makes anything coloured vbCyan to become transparent.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Making A Form Transparent

    Moved to the CodeBank

  3. #3
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    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.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yodaâ„¢
    Posts
    60,710

    Re: Making A Form Transparent

    Not supported in 98 or ME. Only Windows 200/XP/Vista.
    VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    • Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    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
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yodaâ„¢
    Posts
    60,710

    Re: Making A Form Transparent

    From your post #4
    Requires Windows 2000 or later; Win9x/ME: Not supported
    VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    • Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    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.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yodaâ„¢
    Posts
    60,710

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    • Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    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 ?
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yodaâ„¢
    Posts
    60,710

    Re: Making A Form Transparent

    Error trapping for 453 and handling it so the program would continue to run and not crash?
    VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    • Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    Re: Making A Form Transparent

    Yes. May be.

    [SLAP]iPrank[/SLAP]
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  13. #13
    Addicted Member
    Join Date
    Feb 2006
    Posts
    129

    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!
    Resizing
    System Balloons

    If your question is answered, please mark the thread resolved by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.

    Remember to rate a post if you find it helpful

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yodaâ„¢
    Posts
    60,710

    Re: Making A Form Transparent

    Try passing the picturebox handle to the APIs.
    VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    • Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

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

    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:
    1. Me.BackColor = vbCyan
    2.  
    3. ' with
    4.  
    5. Picture1.BackColor = vbCyan
    Then just the picturebox will be made transparent.

  16. #16
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Making A Form Transparent (But with visible controls)

    Nice Code.
    How do i undo it though?
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  17. #17

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

    Re: Making A Form Transparent (But with visible controls)

    The easiest way is probably to remove the WindowStyle:

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

  18. #18
    Addicted Member
    Join Date
    Feb 2006
    Posts
    129

    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.
    Resizing
    System Balloons

    If your question is answered, please mark the thread resolved by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.

    Remember to rate a post if you find it helpful

  19. #19

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

    Re: Making A Form Transparent (But with visible controls)

    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:
    Attached Images Attached Images  

  20. #20
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    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:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    2. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3.  
    4. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    5.                 ByVal hwnd As Long, _
    6.                 ByVal nIndex As Long) As Long
    7.  
    8. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
    9.                 ByVal hwnd As Long, _
    10.                 ByVal nIndex As Long, _
    11.                 ByVal dwNewLong As Long) As Long
    12.                
    13. Private Declare Function SetLayeredWindowAttributes Lib "user32" ( _
    14.                 ByVal hwnd As Long, _
    15.                 ByVal crKey As Long, _
    16.                 ByVal bAlpha As Byte, _
    17.                 ByVal dwFlags As Long) As Long
    18.  
    19. Private Const GWL_STYLE = (-16)
    20. Private Const GWL_EXSTYLE = (-20)
    21. Private Const WS_EX_LAYERED = &H80000
    22. Private Const LWA_COLORKEY = &H1
    23. Private Const LWA_ALPHA = &H2
    24. Dim formhandle as Long
    25.  
    26. Private Sub Form_Load()
    27.    formhandle = FindWindow(vbNullString, Me.Caption)
    28. SetWindowLong formhandle, GWL_EXSTYLE, GetWindowLong(formhandle, GWL_EXSTYLE) Or WS_EX_LAYERED
    29.     SetLayeredWindowAttributes formhandle, vbCyan, 0&, LWA_COLORKEY
    30. Me.BackColor = vbCyan
    31. End Sub
    Last edited by kazar; Apr 13th, 2006 at 06:40 AM.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  21. #21
    Addicted Member
    Join Date
    Feb 2006
    Posts
    129

    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
    Resizing
    System Balloons

    If your question is answered, please mark the thread resolved by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.

    Remember to rate a post if you find it helpful

  22. #22

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

    Re: Making A Form Transparent (But with visible controls)

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

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