Answered True transparent?

Zephlon

Member
Joined
May 18, 2009
Messages
10
Programming Experience
1-3
I have a picture box with backcolor set to transparent. But it won't show the controls under it. It just shows the form's background as it's background.
 
Last edited:
That's how transparency works in WinForms. To make those controls "show through" you'd have to do it yourself, i.e. create an Image of what's behind the PictureBox and then draw it in the PictureBox yourself.
 
Another option is drawing the image in the parent controls Paint event instead of using a Picturebox.

You can also write WPF client applications, which has support for transparent controls.
 
Another option is drawing the image in the parent controls Paint event instead of using a Picturebox.
The problem with that is that I want the user to click on it.

That's how transparency works in WinForms. To make those controls "show through" you'd have to do it yourself, i.e. create an Image of what's behind the PictureBox and then draw it in the PictureBox yourself.
I'll do that.
 
The problem with that is that I want the user to click on it.
The control you'd paint on also has a MouseClick event where you can do a hittest to see if the picture rectangle was clicked.
VB.NET:
If theRectangle.Contains(e.Location) Then
 
Thanks for your help. If found this code online somewhere (I forgot where), but it makes the picture flash every time it updates. How can I make it check if something changed before it updates?

VB.NET:
    Public Sub SetBackgroundTransparent(ByRef theCtl As Control)

        'This routine makes a control appear to be transparent
        'Transparency in VB2008 is implemented in a bit of an odd way. 
        'When the backcolor is set to Transparent, the control 
        'erases everything behind it except the form
        'so if you have a number of layered controls, 
        'you appear to see a "hole" in the display
        'This routine works by taking a snapshot of the area behind 
        'the control and copying this as a bitmap to the
        'backimage of the control.
        'The control isn't truly transparent, but it appears to be.

        'Incoming variable:  the control to be made transparent

        Dim intSourceX As Integer
        Dim intSourceY As Integer
        Dim intBorderWidth As Integer
        Dim intTitleHeight As Integer
        Dim bmp As Bitmap
        Dim MyGraphics As Graphics

        'get the X and Y corodinates of the control, 
        'allowing for the border and titlebar dimensions
        intBorderWidth = (Me.Width - Me.ClientRectangle.Width) / 2
        intTitleHeight = ((Me.Height - (2 * intBorderWidth)) - _
                           Me.ClientRectangle.Height)

        intSourceX = Me.Left + intBorderWidth + theCtl.Left

        intSourceY = Me.Top + intTitleHeight + intBorderWidth + theCtl.Top

        'Hide the control otherwise we end up getting a snapshot of the control
        theCtl.Visible = False

        'Give the UI some time to hide the control
        Application.DoEvents()

        'Take a snapshot of the screen in the region behind the control 
        bmp = New Bitmap(theCtl.Width, theCtl.Height)
        MyGraphics = Graphics.FromImage(bmp)
        MyGraphics.CopyFromScreen(intSourceX, intSourceY, 0, 0, bmp.Size)
        MyGraphics.Dispose()

        'use this clip as a the back image of the label
        theCtl.BackgroundImageLayout = ImageLayout.None
        theCtl.BackgroundImage = bmp
        'show the control with its new backimage
        theCtl.Visible = True
        'Give the UI some time to show the control
        Application.DoEvents()

    End Sub

Also, Is there a way to make the clicks go through the "transparent" area?
 
Back
Top