Results 1 to 10 of 10

Thread: Creating a Rounded Rectangle w/ Transparent Corners.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Question Creating a Rounded Rectangle w/ Transparent Corners.

    I know how to create the Rounded Rectangle, but what i don't know how to do, is make the area outside of the rounded rectangle (the corners) become transparent.

    This is going to be used on a usercontrol, so i really need it to be transparent and not just blend in (ie adapt to the colour of the parent form).

    Thanks.
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    try this:

    vb Code:
    1. Public Class UserControl1
    2.  
    3.     Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         MyBase.Region = New Region(RoundedRec(0, 0, MyBase.Width - 10, MyBase.Height - 10))
    5.     End Sub
    6.  
    7.     Private Function RoundedRec(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As System.Drawing.Drawing2D.GraphicsPath
    8.         ' Make and Draw a path.
    9.         Dim graphics_path As New System.Drawing.Drawing2D.GraphicsPath
    10.         graphics_path.AddLine(X + 10, Y, X + Width, Y) 'add the Top line to the path
    11.  
    12.         'Top Right corner        
    13.         Dim tr() As Point = { _
    14.         New Point(X + Width, Y), _
    15.         New Point((X + Width) + 4, Y + 2), _
    16.         New Point((X + Width) + 8, Y + 6), _
    17.         New Point((X + Width) + 10, Y + 10)}
    18.  
    19.         graphics_path.AddCurve(tr)  'Add the Top right curve to the path
    20.  
    21.         'Bottom right corner
    22.         Dim br() As Point = { _
    23.         New Point((X + Width) + 10, Y + Height), _
    24.         New Point((X + Width) + 8, (Y + Height) + 4), _
    25.         New Point((X + Width) + 4, (Y + Height) + 8), _
    26.         New Point(X + Width, (Y + Height) + 10)}
    27.  
    28.         graphics_path.AddCurve(br)  'Add the Bottom right curve to the path
    29.  
    30.         'Bottom left corner
    31.         Dim bl() As Point = { _
    32.         New Point(X + 10, (Y + Height) + 10), _
    33.         New Point(X + 6, (Y + Height) + 8), _
    34.         New Point(X + 2, (Y + Height) + 4), _
    35.         New Point(X, Y + Height)}
    36.  
    37.         graphics_path.AddCurve(bl)  'Add the Bottom left curve to the path
    38.  
    39.         'Top left corner
    40.         Dim tl() As Point = { _
    41.         New Point(X, Y + 10), _
    42.         New Point(X + 2, Y + 6), _
    43.         New Point(X + 6, Y + 2), _
    44.         New Point(X + 10, Y)}
    45.  
    46.         graphics_path.AddCurve(tl)  'add the Top left curve to the path
    47.  
    48.         Return graphics_path
    49.  
    50.     End Function
    51.  
    52. End Class

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    Thanks for the reply, it does work to an extent, but not quite how i'd like it.
    Is there any other method of getting a similiar result?
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    how does it not work?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    I need the corners to look like this:


    I'm currently editing the code you posted above to suit this need althought it's taking me a while because i'm stupid and can't figure out in which order the x and y are layed out within the points...

    But i will eventually sort that out. but the next part is a need to run a 1 pixel border around this in colour A then fill within that border with colour B.
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    show me your rounded rectangle code + i'll help you convert it + draw a colored border

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    currently do it on a borderless form just so i can test it easier than a usercontrol. Also so far i've only got the top left corner correct, working on the top right corner. i'm currently 1 pixel out.


    Thanks for this dude.

    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.Region = New Region(RoundedRec(0, 0, Me.Width - 3, Me.Height - 3))
        End Sub
    
        Private Function RoundedRec(ByVal X As Integer, ByVal Y As Integer, ByVal Width As Integer, ByVal Height As Integer) As System.Drawing.Drawing2D.GraphicsPath
            ' Make and Draw a path.
            Dim graphics_path As New System.Drawing.Drawing2D.GraphicsPath
            graphics_path.AddLine(X + 3, Y, X + Width, Y) 'add the Top line to the path
    
            'Top Right corner        
            Dim tr() As Point = { _
            New Point(X + Width, Y + 1), _
            New Point((X + Width) + 0, Y + 2), _
            New Point((X + Width) + 0, Y + 0), _
            New Point((X + Width) + 3, Y + 3)}
    
            graphics_path.AddCurve(tr)  'Add the Top right curve to the path
    
            'Bottom right corner 
            Dim br() As Point = { _
            New Point((X + Width) + 10, Y + Height), _
            New Point((X + Width) + 8, (Y + Height) + 4), _
            New Point((X + Width) + 4, (Y + Height) + 8), _
            New Point(X + Width, (Y + Height) + 10)}
    
            graphics_path.AddCurve(br)  'Add the Bottom right curve to the path
    
            'Bottom left corner
            Dim bl() As Point = { _
            New Point(X + 10, (Y + Height) + 10), _
            New Point(X + 6, (Y + Height) + 8), _
            New Point(X + 2, (Y + Height) + 4), _
            New Point(X, Y + Height)}
    
            graphics_path.AddCurve(bl)  'Add the Bottom left curve to the path
    
            'Top left corner
            Dim tl() As Point = { _
            New Point(X, Y + 3), _
            New Point(X + 1, Y + 1), _
            New Point(X + 1, Y + 1), _
            New Point(X + 3, Y)}
    
            graphics_path.AddCurve(tl)  'add the Top left curve to the path
    
            Return graphics_path
    
        End Function
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    try this. used a 10 pixel corner as a 3 pixel corner is hardly noticeable:

    vb Code:
    1. Public Class UserControl1
    2.  
    3.     Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         MyBase.BackColor = Color.Red
    5.         MyBase.Region = New Region(roundedRectangle(0, 0, MyBase.Width - 10, MyBase.Height - 10, 10))
    6.     End Sub
    7.  
    8.     Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    9.         e.Graphics.FillPath(Brushes.White, roundedRectangle(1, 1, MyBase.Width - 12, MyBase.Height - 12, 10))
    10.     End Sub
    11.  
    12.     Public Function roundedRectangle(ByVal X As Integer, ByVal Y As Integer, _
    13.         ByVal Width As Integer, ByVal Height As Integer, ByVal diameter As Integer) As System.Drawing.Drawing2D.GraphicsPath
    14.  
    15.         ''the 'diameter' parameter changes the size of the rounded region
    16.  
    17.         Dim graphics_path As New System.Drawing.Drawing2D.GraphicsPath
    18.  
    19.         Dim BaseRect As New RectangleF(X, Y, Width, Height)
    20.         Dim ArcRect As New RectangleF(BaseRect.Location, New SizeF(diameter, diameter))
    21.  
    22.         'top left Arc
    23.         graphics_path.AddArc(ArcRect, 180, 90)
    24.         graphics_path.AddLine(X + CInt(diameter / 2), _
    25.         Y, X + Width - CInt(diameter / 2), Y)
    26.  
    27.         ' top right arc
    28.         ArcRect.X = BaseRect.Right - diameter
    29.         graphics_path.AddArc(ArcRect, 270, 90)
    30.         graphics_path.AddLine(X + Width, _
    31.         Y + CInt(diameter / 2), X + Width, _
    32.                          Y + Height - CInt(diameter / 2))
    33.  
    34.         ' bottom right arc
    35.         ArcRect.Y = BaseRect.Bottom - diameter
    36.         graphics_path.AddArc(ArcRect, 0, 90)
    37.         graphics_path.AddLine(X + CInt(diameter / 2), _
    38.         Y + Height, X + Width - CInt(diameter / 2), _
    39.                          Y + Height)
    40.  
    41.         ' bottom left arc
    42.         ArcRect.X = BaseRect.Left
    43.         graphics_path.AddArc(ArcRect, 90, 90)
    44.         graphics_path.AddLine(X, Y + CInt(diameter / 2), _
    45.         X, Y + Height - CInt(diameter / 2))
    46.  
    47.         Return graphics_path
    48.  
    49.     End Function
    50.  
    51. End Class

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    here's an improvement. i reduced the size of the corners slightly + it appears more symmetrical:

    vb Code:
    1. Public Class UserControl1
    2.  
    3.     Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         MyBase.BackColor = Color.Red
    5.         MyBase.Region = New Region(roundedRectangle(0, 0, MyBase.Width - 7, MyBase.Height - 7, 5))
    6.     End Sub
    7.  
    8.     Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    9.         e.Graphics.FillPath(Brushes.White, roundedRectangle(1, 1, MyBase.Width - 9, MyBase.Height - 9, 5))
    10.     End Sub
    11.  
    12.     Public Function roundedRectangle(ByVal X As Integer, ByVal Y As Integer, _
    13.         ByVal Width As Integer, ByVal Height As Integer, ByVal diameter As Integer) As System.Drawing.Drawing2D.GraphicsPath
    14.  
    15.         ''the 'diameter' parameter changes the size of the rounded region
    16.  
    17.         Dim graphics_path As New System.Drawing.Drawing2D.GraphicsPath
    18.  
    19.         Dim BaseRect As New RectangleF(X, Y, Width - diameter, Height - diameter)
    20.         Dim ArcRect As New RectangleF(BaseRect.Location, New SizeF(CInt(diameter * Math.PI), CInt(diameter * Math.PI)))
    21.  
    22.         'top left Arc
    23.         graphics_path.AddArc(ArcRect, 180, 90)
    24.  
    25.         ' top right arc
    26.         ArcRect.X = BaseRect.Right - diameter
    27.         graphics_path.AddArc(ArcRect, 270, 90)
    28.  
    29.         ' bottom right arc
    30.         ArcRect.Y = BaseRect.Bottom - diameter
    31.         graphics_path.AddArc(ArcRect, 0, 90)
    32.  
    33.         ' bottom left arc
    34.         ArcRect.X = BaseRect.Left
    35.         graphics_path.AddArc(ArcRect, 90, 90)
    36.  
    37.         Return graphics_path
    38.  
    39.     End Function
    40.  
    41. End Class

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    127

    Re: Creating a Rounded Rectangle w/ Transparent Corners.

    Thanks dude, but the first method is the one that will work because i need it to be 3 pixels. I'm still failing at getting the top right corner to work :/
    EXPERIENCED VB6 CODER
    IF YOU SEE ANY FAILURE IN MY CODING, PLEASE LET ME KNOW AS I'VE ONLY JUST STARTED LEARNING VB.NET AND I WILL APPRECIATE ANY HINTS AND TIPS YOU HAVE TO OFFER!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width