UserControl/ElementHost Transparent Background

1k views Asked by At

I need a method that sets the background of the ElementHost completely transparent or so that it does not even render in the first place.

enter image description here

Current Structure

In the background I have a PictureBox. Over that there is my UserControl (Which you can Download below). Both the PictureBox and the UserControl have a Width of 150. As you can see in the Picture above, the UserControl is 100% Invisible. In the UserControl is an ElementHost with a Width of 120, within that there is a WPF-Content with a Width of 100.
Everything is Transparent, except the ElementHost1.

My Code

UserControl:

Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
    Get
        Dim cp As CreateParams = MyBase.CreateParams
        cp.ExStyle = &H20
        Return cp
    End Get
End Property

Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
End Sub

Public Overrides Sub Refresh()
    Parent.Invalidate(New Rectangle(Me.Location, Me.Size), True)
End Sub

Public Sub New()
    InitializeComponent()

    Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    Me.BackColor = System.Drawing.Color.Transparent

    ElementHost1.BackColor = System.Drawing.Color.Transparent
    ElementHost1.BackColorTransparent = True
End Sub

I´ve also tried to create a Custom ElementHost:

Public Class TransElementHost
    Inherits ElementHost

    Public Sub TransElementHost()
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.BackColorTransparent = True
        'Me.BackColor = System.Drawing.Color.FromArgb(0, 0, 0, 0)
    End Sub

    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = &H20
            Return cp
        End Get
    End Property

    Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
    End Sub

    Public Overrides Sub Refresh()
        Parent.Invalidate(New Rectangle(Me.Location, Me.Size), True)
    End Sub
End Class

My SVGTest-UserControl

Does anybody have an idea?

2

There are 2 answers

3
Visual Vincent On

It's probably not the best fix, but you could try using the ButtonRenderer class. Put this code either in OnPaintBackground or OnPaint.

If Me.BackColor = Color.Transparent Then
    Windows.Forms.ButtonRenderer.DrawParentBackground(e.Graphics, Me.ClientRectangle, Me)
End If

The ButtonRenderer class is used to draw the regular buttons; their border, background, text, image etc.

I used the above code myself to create a custom control with transparent background. (Though I see now that my control inherits from ButtonBase... But the above code is still worth a try).

3
Visual Vincent On

I know it's been really long since this was active, but if you still need it I've (finally) got a code that will draw controls' backgrounds fully transparent.

Just put this code in the control's code. It overrides the OnPaintBackground event:

Protected Overrides Sub OnPaintBackground(e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaintBackground(e)

    If Parent IsNot Nothing Then
        Dim index As Integer = Parent.Controls.GetChildIndex(Me)

        For i As Integer = Parent.Controls.Count - 1 To index + 1 Step -1
            Dim c As Control = Parent.Controls(i)
            If c.Bounds.IntersectsWith(Bounds) AndAlso c.Visible = True Then
                Dim bmp As New Bitmap(c.Width, c.Height, e.Graphics)
                c.DrawToBitmap(bmp, c.ClientRectangle)
                e.Graphics.TranslateTransform(c.Left - Left, c.Top - Top)
                e.Graphics.DrawImageUnscaled(bmp, Point.Empty)
                e.Graphics.TranslateTransform(Left - c.Left, Top - c.Top)
                bmp.Dispose()
            End If
        Next
    End If
End Sub