DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Changing User Control - Transparency

  1. #1
    Join Date
    Jun 2008

    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 ShapeTypeValue
        Private Const WS_EX_TRANSPARENT As Int32 = &H20
        Public Enum Display
        End Enum
        Public Property ShapeType() As Display
                ShapeType = ShapeTypeValue
            End Get
            Set(ByVal value As Display)
                ShapeTypeValue = value
            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
                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.
            ' Add any initialization after the InitializeComponent() call.
            SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            Me.BackColor = Color.Transparent
        End Sub
    End Class
    Attached Images Attached Images

Similar Threads

  1. Replies: 6
    Last Post: 05-31-2010, 07:19 PM
  2. Replies: 0
    Last Post: 03-10-2007, 04:08 AM
  3. User Control Focus Problem.....Plz. help
    By meerraj in forum ASP.NET
    Replies: 2
    Last Post: 03-01-2007, 02:59 AM
  4. User control reusability across web-applications
    By srinivasc_it in forum ASP.NET
    Replies: 0
    Last Post: 09-26-2006, 08:47 PM
  5. Replies: 0
    Last Post: 06-15-2005, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center