Changing User Control - Transparency
I am trying to create a user control that has a transparent background. I have this working, kind of. My problem is that I need this control to be able to change shape on the fly. This is what is happening.
The control is a simple filled rectangle or a simple elipse, based on a parameter. If I change the shape from a rectangle to an elipse, because the background is not being painted, the shape ends up with a retangle with an elipse on top (see attatched shape.jpg). If I put the form behind another app then bring it back to the front it looks fine.
How can I fix this so that it is tranparent but allows me to change the shape on the fly and repaint the entire control so the previous pixels don't remain and muck up the works?
Here is my code:
Public Class Shape
Private Const WS_EX_TRANSPARENT As Int32 = &H20
Public Enum Display
Public Property ShapeType() As Display
ShapeType = ShapeTypeValue
Set(ByVal value As Display)
ShapeTypeValue = value
Private Sub Shape_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If ShapeTypeValue = Display.Rectangle Then
e.Graphics.DrawRectangle(Pens.Black, 0, 0, Me.Size.Width - 1, Me.Size.Height - 1)
e.Graphics.FillRectangle(Brushes.LightGreen, 1, 1, Me.Size.Width - 2, Me.Size.Height - 2)
ElseIf ShapeTypeValue = Display.Elipse Then
e.Graphics.DrawEllipse(Pens.Black, 0, 0, Me.Size.Width - 1, Me.Size.Height - 1)
e.Graphics.FillEllipse(Brushes.LightGreen, 1, 1, Me.Size.Width - 3, Me.Size.Height - 3)
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
Public Sub New()
' This call is required by the Windows Form Designer.
' Add any initialization after the InitializeComponent() call.
Me.BackColor = Color.Transparent