Creating Transparent Controls That Can Handle Mouse Events

The class TransparentControl provides a control that is fully transparent without loosing the ability to handle the control’s events. This is achieved by adjusting the control’s extended style bits:

Imports System
Imports System.Windows.Forms

Public Class TransparentControl
    Inherits Control

    Public Sub New()
        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.BackColor = Color.Transparent
    End Sub

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
            Const WS_EX_TRANSPARENT As Int32 = &H20
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
            Return cp
        End Get
    End Property

    Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
    End Sub
End Class