Windows Forms: Transparent Containers


Introduction

Looking to create a truly transparent user control or subclass an existing control.

C#

public class TransparentPanel : Panel
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return cp;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle);
    }
}

 

VB.Net

Public Class TransparentPanel
Inherits Panel
    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
        Get
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or &H20 ''#WS_EX_TRANSPARENT
            Return cp
        End Get
    End Property
    Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
    ''#MyBase.OnPaintBackground(e)
    End Sub
End Class

 

Article Rating:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...