'Author: Arman Ghazanchyan
'Created date: 01/27/2007
'Last updated: 01/28/2007
Imports System.Drawing.Drawing2D
Public Class Shape
''' <summary>
''' Rounds a rectangle corners' and returns the graphics path.
''' </summary>
''' <param name="rec">A rectangle whose corners should be rounded.</param>
''' <param name="r">The radius of the rounded corners. This value should be
''' bigger then 0 and less or equal to the (a half of the smallest value
''' of the rectangle�s width and height).</param>
''' <param name="exclude_TopLeft">A value that specifies if the top-left
''' corner of the rectangle should be rounded. If the value is True
''' then the corner is not rounded otherwise it is.</param>
''' <param name="exclude_TopRight">A value that specifies if the top-right
''' corner of the rectangle should be rounded. If the value is True
''' then the corner is not rounded otherwise it is.</param>
''' <param name="exclude_BottomRight">A value that specifies if the bottom-right
''' corner of the rectangle should be rounded. If the value is True
''' then the corner is not rounded otherwise it is.</param>
''' <param name="exclude_BottomLeft">A value that specifies if the bottom-left
''' corner of the rectangle should be rounded. If the value is True
''' then the corner is not rounded otherwise it is.</param>
Public Shared Function RoundedRectangle(ByVal rec As Rectangle, ByVal r As Integer, _
Optional ByVal exclude_TopLeft As Boolean = False, _
Optional ByVal exclude_TopRight As Boolean = False, _
Optional ByVal exclude_BottomRight As Boolean = False, _
Optional ByVal exclude_BottomLeft As Boolean = False) As Drawing2D.GraphicsPath
Dim path As New GraphicsPath
Dim s As Integer = r * 2
'If 's' is less than or equal to zero,
'then return a simple rectangle.
If Not s > 0 Then
path.StartFigure()
path.AddLine(rec.Right, rec.Y, rec.Right, rec.Y)
path.AddLine(rec.Right, rec.Bottom, rec.Right, rec.Bottom)
path.AddLine(rec.X, rec.Bottom, rec.X, rec.Bottom)
path.AddLine(rec.X, rec.Y, rec.X, rec.Y)
path.CloseAllFigures()
Return path
End If
'If 's' is bigger than the smallest value of the size,
'then assign the value to 's'.
If rec.Height <= rec.Width Then
If s > rec.Height Then
s = rec.Height
End If
Else
If s > rec.Width Then
s = rec.Width
End If
End If
path.StartFigure()
'Set top-right corner.
If Not exclude_TopRight Then
path.AddArc(rec.Right - s, rec.Y, s, s, 270, 90)
Else
path.AddLine(rec.Right, rec.Y, rec.Right, rec.Y)
End If
'Set bottom-right corner.
If Not exclude_BottomRight Then
path.AddArc(rec.Right - s, rec.Bottom - s, s, s, 0, 90)
Else
path.AddLine(rec.Right, rec.Bottom, rec.Right, rec.Bottom)
End If
'Set bottom-left corner.
If Not exclude_BottomLeft Then
path.AddArc(rec.X, rec.Bottom - s, s, s, 90, 90)
Else
path.AddLine(rec.X, rec.Bottom, rec.X, rec.Bottom)
End If
'Set top-left corner.
If Not exclude_TopLeft Then
path.AddArc(rec.X, rec.Y, s, s, 180, 90)
Else
path.AddLine(rec.X, rec.Y, rec.X, rec.Y)
End If
path.CloseAllFigures()
Return path
End Function
''' <summary>
''' Rounds the corners of the newly created rectangle-shape region and returns the region.
''' </summary>
''' <param name="rSize">The size of the region.</param>
''' <param name="r">The radius of the rounded corners. This value should be
''' bigger then 0 and less or equal to the (a half of the smallest value
''' of the region�s width and height).</param>
''' <param name="exclude_TopLeft">A value that specifies if the top-left
''' corner of the region should be rounded. If the value is True
''' then the corner is not rounded otherwise it is.</param>
''' <param name="exclude_TopRight">A value that specifies if the top-right
''' corner of the region should be rounded. If the value is True
''' then the corner is not rounded otherwise it is.</param>
''' <param name="exclude_BottomRight">A value that specifies if the bottom-right
''' corner of the region should be rounded. If the value is True
''' then the corner is not rounded otherwise it is.</param>
''' <param name="exclude_BottomLeft">A value that specifies if the bottom-left
''' corner of the region should be rounded. If the value is True
''' then the corner is not rounded otherwise it is.</param>
Public Shared Function RoundedRegion(ByVal rSize As Size, ByVal r As Integer, _
Optional ByVal exclude_TopLeft As Boolean = False, _
Optional ByVal exclude_TopRight As Boolean = False, _
Optional ByVal exclude_BottomRight As Boolean = False, _
Optional ByVal exclude_BottomLeft As Boolean = False) As Region
Dim s As Integer = r * 2
Dim path As New GraphicsPath
'If 's' is less than or equal to zero,
'then return a simple rectangle.
If Not s > 0 Then
path.StartFigure()
path.AddLine(rSize.Width, 0, rSize.Width, 0)
path.AddLine(rSize.Width, rSize.Height, rSize.Width, rSize.Height)
path.AddLine(0, rSize.Height, 0, rSize.Height)
path.AddLine(0, 0, 0, 0)
path.CloseAllFigures()
Return New Region(path)
End If
'If 's' is bigger than the smallest value of the size,
'then assign the value to 's'.
If rSize.Height < rSize.Width Then
If s > rSize.Height Then
s = rSize.Height
End If
Else
If s > rSize.Width Then
s = rSize.Width
End If
End If
path.StartFigure()
'Set top-right corner.
If Not exclude_TopRight Then
path.AddArc(rSize.Width - s, 0, s - 1, s - 1, 270, 90)
Else
path.AddLine(rSize.Width, 0, rSize.Width, 0)
End If
'Set bottom-right corner.
If Not exclude_BottomRight Then
path.AddLine(rSize.Width, r, rSize.Width, rSize.Height - r)
path.AddArc(rSize.Width - s, rSize.Height - s, s - 1, s - 1, 0, 90)
Else
path.AddLine(rSize.Width, rSize.Height, rSize.Width, rSize.Height)
End If
'Set bottom-left corner.
If Not exclude_BottomLeft Then
path.AddLine(rSize.Width - r, rSize.Height, r, rSize.Height)
path.AddArc(0, rSize.Height - s, s - 1, s - 1, 90, 90)
Else
path.AddLine(0, rSize.Height, 0, rSize.Height)
End If
'Set top-left corner.
If Not exclude_TopLeft Then
path.AddArc(0, 0, s - 1, s - 1, 180, 90)
Else
path.AddLine(0, 0, 0, 0)
End If
path.CloseAllFigures()
Return New Region(path)
End Function
End Class