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

    Private Const WS_EX_TRANSPARENT As Int32 = &H20

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

    Protected Overrides ReadOnly Property CreateParams() As CreateParams
        Get
            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