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:
Code:Public Class Shape Private ShapeTypeValue Private Const WS_EX_TRANSPARENT As Int32 = &H20 Public Enum Display None Rectangle Elipse End Enum Public Property ShapeType() As Display Get ShapeType = ShapeTypeValue End Get Set(ByVal value As Display) ShapeTypeValue = value Me.Refresh() End Set End Property 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) End If 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 Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. SetStyle(ControlStyles.SupportsTransparentBackColor, True) Me.UpdateStyles() Me.BackColor = Color.Transparent End Sub End Class


Reply With Quote


Bookmarks