UserControl / ElementHost透明背景

时间:2015-06-11 10:07:26

标签: wpf vb.net vb.net-2010

我需要一种方法,将ElementHost的背景设置为完全透明,或者甚至不首先渲染。

enter image description here

当前结构

在后台我有一个PictureBox。 在那之外有我的UserControl(您可以在下面下载)。 PictureBox和UserControl的宽度均为150。 如上图所示,UserControl是100%不可见的。 在UserControl中是一个宽度为120的ElementHost,其中有一个宽度为100的WPF内容。
一切都是透明的,除了ElementHost1。

我的代码

用户控件:

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

我也尝试创建 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

有人有想法吗?

2 个答案:

答案 0 :(得分:2)

我知道它已经很久了,因为它很活跃,但是如果你仍然需要它,我(最后)得到了一个代码来绘制控件'背景完全透明。

只需将此代码放入控件的代码中即可。它会覆盖OnPaintBackground事件:

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

答案 1 :(得分:0)

这可能不是最佳解决方案,但您可以尝试使用2008-Feb 类。将此代码放在ButtonRendererOnPaintBackground

OnPaint

If Me.BackColor = Color.Transparent Then Windows.Forms.ButtonRenderer.DrawParentBackground(e.Graphics, Me.ClientRectangle, Me) End If 类用于绘制常规按钮;他们的边界,背景,文字,图像等。

我自己使用上面的代码创建了一个透明背景的自定义控件。 (虽然我现在看到我的控件继承自ButtonRenderer ......但上面的代码仍值得一试。)