CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Transparent Forms

    At the moment my toolbar uses a faux trasnparent look. It blits th image from the DC of the Desktop and whacks it on the form, this look really effective! The problem is it cannot be updated as this form apears in the DC of the desktop so when the blit routine is run again no change is registered.

    Is there anyway to make a form transparent in another way so that for example when you drag your mouse across the desktop you get that drag box and this can been seen under the form. I do not what to the toolbar to be always on the top of the screen.

    Rich.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Resolved Re: Transparent Forms

    Quote Originally Posted by Rich2189
    Is there anyway to make a form transparent in another way so that for example when you drag your mouse across the desktop you get that drag box and this can been seen under the form. I do not what to the toolbar to be always on the top of the screen.

    Rich.
    You know ... I saw some code to do this last month.. but cant remember if it was on CG or Planet Source Code.. will search a bit more and let you know..

    ---------- EDIT --------------

    here it is on PSC. This is some cool code. Enjoy...


    Gremmy
    Last edited by GremlinSA; November 25th, 2005 at 02:56 PM. Reason: Added Link
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Transparent Forms

    That is some cool code, but it make the whole form including its controls transparent, sorry i didn't make my self clear. Attached below is my desktop with the toobar running. The form needs to be transparent but the buttons and text need to be opaque.


    Rich
    Attached Images Attached Images

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Lightbulb Re: Transparent Forms

    Okay I might have a solution for you ... It might take some code to implement properly (And some trial and error...)

    Use the forms transparent property..
    VB Books online
    Shape Control with Transparent BackStyle
    If one of the constituent controls on the UserControl is a Shape whose BackStyle property has also been set to Transparent, Visual Basic will clip around the shape. This allows your control to appear to be circular, for example. Mouse clicks that fall on the transparent background will be passed through to the container.
    You can draw on the visible surface of the Shape control using the graphics methods of the UserControl object, such as Line, Circle, and PaintPicture. Visual Basic will clip the drawing to the visible part of the Shape control.
    Note Drawing should not be done in the Paint event, because the Shape control draws after the Paint event, and thus will obscure your drawing.

    Bitmaps with Transparent Backgrounds
    If you assign a bitmap to the UserControl’s MaskPicture property, and set the MaskColor property of the UserControl equal to the background color of the bitmap, Visual Basic will clip around the parts of the bitmap that are not equal to the MaskColor — even if those parts are not contiguous.
    As with the Shape control, you can draw on the bitmap. Visual Basic will clip your drawing to the visible parts of the bitmap.
    Note This feature is supported for image-type bitmaps, such as GIF, JPEG, and DIB. It is not supported for Windows metafiles, icons, or cursors.

    Labels with Transparent BackStyle
    If one of the constituent controls on the UserControl is a Label whose BackStyle property has also been set to Transparent, and whose Font property specifies a TrueType font, Visual Basic will clip around the font. In addition, mouse clicks that fall in the spaces between letters will be passed through to the container.
    So make your form transparent around the controls and Labels..
    Then use the code to make the controls only softly opaque (75%)..

    This will take some time but i think to get that 'REAL PRO' look it will be well worth it.. I'm playing with this to see how effective this is..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Transparent Forms

    I put this little project together using code modules from a few sources.. all from PSC..

    I think this is the sort of look your looking for ..

    Gremmy..

    PS i havent added code to keep the form on top.
    Attached Files Attached Files
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Jan 2001
    Posts
    486

    Re: Transparent Forms

    Can't remember where I got this code to give credit, but it makes the entire form transparent EXCEPT for any controls placed on it. Project I used to test it had a label, a textbox and 2 command buttons [the second of which can be used to close the form].

    Code:
    Option Explicit
    Private Declare Function CreateRectRgn Lib _
        "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, _
        ByVal X2 As Long, ByVal Y2 As Long) As Long
    Private Declare Function CombineRgn Lib _
        "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, _
        ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
    Private Declare Function SetWindowRgn Lib _
        "user32" (ByVal hWnd As Long, ByVal hRgn As Long, _
        ByVal bRedraw As Boolean) As Long
    Private Declare Function DeleteObject Lib _
        "gdi32" (ByVal hObject As Long) As Long
    
    ' Constants used by the CombineRgn function
    Private Const RGN_AND = 1
    Private Const RGN_OR = 2
    Private Const RGN_XOR = 3
    Private Const RGN_DIFF = 4
    Private Const RGN_COPY = 5
    
    Private Sub Form_Activate()
        Dim rgnForm As Long, rgnCombined As Long
        Dim rgnControl As Long, x As Long
        Dim formWidth As Single, formHeight As Single
        Dim borderWidth As Single, titleHeight As Single
        Dim ctlLeft As Single, ctlTop As Single
        Dim ctlWidth As Single, ctlHeight As Single
        Dim ctl As Control
    
        ' Calculate the form area
        borderWidth = (Me.Width - Me.ScaleWidth) / 2
        titleHeight = Me.Height - Me.ScaleHeight - borderWidth
        ' Convert to Pixels
        borderWidth = ScaleX(borderWidth, vbTwips, vbPixels)
        titleHeight = ScaleY(titleHeight, vbTwips, vbPixels)
        formWidth = ScaleX(Me.Width, vbTwips, vbPixels)
        formHeight = ScaleY(Me.Height, vbTwips, vbPixels)
        
        ' Create a region for the whole form
        rgnForm = CreateRectRgn(0, 0, formWidth, formHeight)
        
        rgnCombined = CreateRectRgn(0, 0, 0, 0)
        ' Make the graphical area transparent by combining the two regions
        x = CombineRgn(rgnCombined, rgnForm, rgnForm, RGN_DIFF)
    
        ' Make the controls visible
        For Each ctl In Controls
            ' Make the regions of controls whose container is the form visible
            If TypeOf ctl.Container Is Form Then
                ctlLeft = ScaleX(ctl.Left, vbTwips, vbPixels) + borderWidth
                ctlTop = ScaleX(ctl.Top, vbTwips, vbPixels) + titleHeight
                ctlWidth = ScaleX(ctl.Width, vbTwips, vbPixels) + ctlLeft
                ctlHeight = ScaleX(ctl.Height, vbTwips, vbPixels) + ctlTop
                rgnControl = CreateRectRgn(ctlLeft, ctlTop, ctlWidth, ctlHeight)
                x = CombineRgn(rgnCombined, rgnCombined, rgnControl, RGN_OR)
            End If
        Next ctl
        
    
        ' Set the clipping area of the window using the resulting region
        SetWindowRgn hWnd, rgnCombined, True
        ' Tidy up
        x = DeleteObject(rgnCombined)
        x = DeleteObject(rgnControl)
        x = DeleteObject(rgnForm)
    End Sub
    Private Sub Command2_Click()
    Unload Me
    End Sub
    If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren

    Do canibals not eat clowns because they taste funny?

  7. #7
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Transparent Forms

    Exactamundo!, that code is exactly what ive been looking for. Perfect Thanks.

    Rich

  8. #8
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Transparent Forms

    The form is transparent but the captions when set to transparent are not i tried:

    Code:
    For Each ctl In Controls
            ' Make the regions of controls whose container is the form visible
            
            If TypeOf ctl.Container Is Label Then 'for labels
                ctlLeft = ScaleX(ctl.Left, vbTwips, vbPixels) + borderWidth
                ctlTop = ScaleX(ctl.Top, vbTwips, vbPixels) + titleHeight
                ctlWidth = ScaleX(ctl.Width, vbTwips, vbPixels) + ctlLeft
                ctlHeight = ScaleX(ctl.Height, vbTwips, vbPixels) + ctlTop
                rgnControl = CreateRectRgn(ctlLeft, ctlTop, ctlWidth, ctlHeight)
                x = CombineRgn(rgnCombined, rgnCombined, rgnControl, RGN_OR)
                
            ElseIf TypeOf ctl.Container Is Form Then
                ctlLeft = ScaleX(ctl.Left, vbTwips, vbPixels) + borderWidth
                ctlTop = ScaleX(ctl.Top, vbTwips, vbPixels) + titleHeight
                ctlWidth = ScaleX(ctl.Width, vbTwips, vbPixels) + ctlLeft
                ctlHeight = ScaleX(ctl.Height, vbTwips, vbPixels) + ctlTop
                rgnControl = CreateRectRgn(ctlLeft, ctlTop, ctlWidth, ctlHeight)
                x = CombineRgn(rgnCombined, rgnCombined, rgnControl, RGN_OR)
            End If
        Next ctl

    This had no effect at all. Any suggestions

    Rich

  9. #9
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Transparent Forms

    I know of something that works to your specifications. It's a user control that makes anything that has the same back color as it transparent (as in not there, not translucent, which is the you can see through it, but you can still see it). Like what you're looking for.

    I found it on PSC awhile ago, but I don't remember the name. I'll look right now, and I'll have a link for you by the end of today.


    Found it:
    http://www.planet-source-code.com/vb...50160&lngWId=1

    It only works with 2000/XP, but put that User Control on your form, set the color of whatever control to the TransColor of that control, and it should be transparent on run.

    Be wary, though, you have to press on the text (or edges, if it's 3D) to get a button to be pressed. Same with other controls.
    Last edited by ChaosTheEternal; November 29th, 2005 at 12:47 PM.

  10. #10
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Transparent Forms

    That is magical thanks chaos, what i dont understand is where is all the code? the form and the user control is virtually empty! is it really that simple?

    Rich

  11. #11
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Transparent Forms

    That's it. It apparently is just that simple.

    Code:
    TC = &HC000C0
    Ret = GetWindowLong(UserControl.Parent.hWnd, G_E)
    Ret = Ret Or W_E
    SetWindowLong UserControl.Parent.hWnd, G_E, Ret
    SetLayeredWindowAttributes UserControl.Parent.hWnd, TC, 0, LW_KEY
    I'm guessing these are the important lines.


    BUT!
    To my surprise, someone posted a much easier method in response to that code:
    Code:
    Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
    
    Private Sub SetTransparency(piHwnd As Long, piColor As OLE_COLOR)
        Const G_E = &HFFEC
        Const W_E = &H80000
        Const LW_KEY = &H1
        SetWindowLong piHwnd, G_E, GetWindowLong(piHwnd, G_E) Or W_E
        SetLayeredWindowAttributes piHwnd, piColor, 0, LW_KEY
    End Sub
    
    Private Sub Form_Initialize()
        Call SetTransparency(Me.hwnd, Me.BackColor)
    End Sub
    Just make sure you don't keep the default BackColor for the form (doesn't seem to work) or use any of the colors in the System tab of the color picker (they also cause it to not work), and don't use a color other controls might use (trying white with XP made it affect the caption bar and buttons, as shown).

    It works just as well, without the user control.
    Attached Images Attached Images
    Last edited by ChaosTheEternal; July 27th, 2006 at 10:20 AM. Reason: Including the declarations for the code, and clarifying color selection.

  12. #12
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Transparent Forms

    The bliting method is so complex and tacky compared to this code. I noticed if you try to set the colour with a long say -32030403, instead of the hexdecimal it doesnt work. My toolbar look amazing now, i just need to add some drive information to it and a CPU monitor, now thats a challenge. Might be easier to do it in C++ and stuff it in a Dll.

    Rich

  13. #13
    Join Date
    Mar 2005
    Location
    Nottingham, UK
    Posts
    665

    Re: Transparent Forms

    The code shown here is really cool!

    I googled the SetLayeredWindowAttributes API because its not listed in VB's API viewer, and I discovered that there is an excellent two part article here on CodeGuru, detailing exactly how to use this API to set the translucency of the entire form and to use a particular colour to set any region containing that colour totally transparent. V Cool indeed!

    Part 1
    Part 2

    Have absolutely no use for this at all, but it is very cool and have made a sample project for my code library - you never know when something like this may come in handy! (Use a scroll bar with max set to 250 and call the SetTranslucent function in the scroll event, passing 255 minus the scrollbar's value to see how the translucency changes - its great!)

    ...

  14. #14
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Transparent Forms

    works perfect check out the picture, what do you guys think? any design ideas? Im going to add an MSN mode and the computer information will be made more detailed.

    Rich
    Attached Images Attached Images

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

Featured