see:
WinForms: How to create a control transparent to other controls
Edit: When I originally posted, I had just glanced at that code that accompanies the MS article. After looking at it, I realized that it has some implementation errors (go figure for a MS example). However, even if the coding errors are fixed, the technique used is in error. It does not paint the non-client area of obscured controls.
Here is a simplistic transparent panel control. Note that while in the designer, the panel will not be transparent. The reason for this is that because of the method used, it is difficult to edit the panel or its contents if it transparent. It may be possible to write a control designer to allow for easy editing while allowing it to be transparent, but I do not have the time right now to investigate this and I also dislike writing designers. :)
This control obtains the transparent effect by limiting the allowed region into which the control can paint. If you don't paint over it, you can see it.
Public Class TransparentPanel
Inherits Panel
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
SetPaintableRegion()
MyBase.OnPaint(e)
End Sub
Private Sub SetPaintableRegion()
' intially set the paintable region as the entire control area
Dim PaintableRegion As Region = New Region(New Rectangle(0, 0, Me.Width, Me.Height))
' if in design, do not do transparent
If Not Me.DesignMode Then
' build a rectangle that represents the client area
Dim clientrect As Rectangle = Me.ClientRectangle
' need to set the location of clientrect relative to Control.Location
Dim offset As Point = ClientOriginRelativeToLocation(Me)
clientrect.X = offset.X
clientrect.Y = offset.Y
' subtract clientrect from PaintableRegion
PaintableRegion.Xor(clientrect)
' now need to add in areas for child controls
For Each c As Control In Me.Controls
Dim r As Rectangle = c.Bounds
r.X += offset.X
r.Y += offset.Y
PaintableRegion.Union(r)
Next
End If 'Not Me.DesignMode
If Me.Region IsNot Nothing Then
Me.Region.Dispose()
Else
Me.Region = PaintableRegion
End If
End Sub 'SetPaintableRegion
Public Shared Function ClientOriginRelativeToLocation(ByVal c As Control) As Point
Dim clientorigin As Point = c.PointToScreen(Point.Empty)
Dim parentorigin As Point = Point.Empty
If c.Parent IsNot Nothing Then
parentorigin = c.Parent.PointToScreen(c.Location)
End If
Return New Point((clientorigin.X - parentorigin.X), (clientorigin.Y - parentorigin.Y))
End Function 'ClientOriginRelativeToLocation
End Class ' TransparentPanel