Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class RoundRectGroupBox
    Inherits GroupBox

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim graphics As Graphics = e.Graphics
        Dim rectangle As Rectangle = ClientRectangle
        rectangle.Width -= 1
        rectangle.Height -= 1
        Using pen As New Pen(Color.Black)
            pen.Width = 1
            pen.Alignment = PenAlignment.Center
            Using graphicsPath As GraphicsPath = RoundedRectangle(rectangle, 10)
                graphics.DrawPath(pen, graphicsPath)
            End Using
        End Using
    End Sub

    Private Function RoundedRectangle(ByVal rectangle As Rectangle, ByVal radius As Integer) As GraphicsPath
        Dim path As New GraphicsPath()
        path.AddArc(rectangle.X, rectangle.Y, radius * 2, radius * 2, 180, 90)
        path.AddLine(rectangle.X + radius, rectangle.Y, rectangle.Right - radius * 2, rectangle.Y)
        path.AddArc(rectangle.Right - radius * 2, rectangle.Y, radius * 2, radius * 2, 270, 90)
        path.AddLine(rectangle.Right, rectangle.Y + radius * 2, rectangle.Right, rectangle.Bottom - radius * 2)
        path.AddArc(rectangle.Right - radius * 2, rectangle.Bottom - radius * 2, radius * 2, radius * 2, 0, 90)
        path.AddLine(rectangle.Right - radius * 2, rectangle.Bottom, rectangle.X + radius * 2, rectangle.Bottom)
        path.AddArc(rectangle.X, rectangle.Bottom - radius * 2, radius * 2, radius * 2, 90, 90)
        path.AddLine(rectangle.X, rectangle.Bottom - radius * 2, rectangle.X, rectangle.Y + radius * 2)
        path.CloseFigure()
        Return path
    End Function
End Class