Click here to Skip to main content
15,896,118 members
Articles / Multimedia / GDI+

A Reflective and Translucent Glass Panel

Rate me:
Please Sign up or sign in to vote.
4.50/5 (13 votes)
1 Oct 2008CPOL 81.2K   6.3K   50  
Component derived from Panel that acts like a window (glass)
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Design

Public Module RoundedCorners

    Public Function RoundCorners(ByVal Rectangle As RectangleF, Optional ByVal Radius As Integer = 5, Optional ByVal Corners As Corner = Corner.All) As Drawing2D.GraphicsPath
        Dim p As New Drawing2D.GraphicsPath
        Dim x As Single = Rectangle.X
        Dim y As Single = Rectangle.Y
        Dim w As Single = Rectangle.Width
        Dim h As Single = Rectangle.Height
        Dim r As Integer = Radius

        p.StartFigure()
        'top left arc
        If CBool(Corners And Corner.TopLeft) Then
            p.AddArc(New RectangleF(x, y, 2 * r, 2 * r), 180, 90)
        Else
            p.AddLine(New PointF(x, y + r), New PointF(x, y))
            p.AddLine(New PointF(x, y), New PointF(x + r, y))
        End If

        'top line
        p.AddLine(New PointF(x + r, y), New PointF(x + w - r, y))

        'top right arc
        If CBool(Corners And Corner.TopRight) Then
            p.AddArc(New RectangleF(x + w - 2 * r, y, 2 * r, 2 * r), 270, 90)
        Else
            p.AddLine(New PointF(x + w - r, y), New PointF(x + w, y))
            p.AddLine(New PointF(x + w, y), New PointF(x + w, y + r))
        End If

        'right line
        p.AddLine(New PointF(x + w, y + r), New PointF(x + w, y + h - r))

        'bottom right arc
        If CBool(Corners And Corner.BottomRight) Then
            p.AddArc(New RectangleF(x + w - 2 * r, y + h - 2 * r, 2 * r, 2 * r), 0, 90)
        Else
            p.AddLine(New PointF(x + w, y + h - r), New PointF(x + w, y + h))
            p.AddLine(New PointF(x + w, y + h), New PointF(x + w - r, y + h))
        End If

        'bottom line
        p.AddLine(New PointF(x + w - r, y + h), New PointF(x + r, y + h))

        'bottom left arc
        If CBool(Corners And Corner.BottomLeft) Then
            p.AddArc(New RectangleF(x, y + h - 2 * r, 2 * r, 2 * r), 90, 90)
        Else
            p.AddLine(New PointF(x + r, y + h), New PointF(x, y + h))
            p.AddLine(New PointF(x, y + h), New PointF(x, y + h - r))
        End If

        'left line
        p.AddLine(New PointF(x, y + h - r), New PointF(x, y + r))

        'close figure...
        p.CloseFigure()

        Return p
    End Function

    <Flags()> _
    Public Enum Corner
        None = 0
        TopLeft = 1
        TopRight = 2
        BottomLeft = 4
        BottomRight = 8
        All = TopLeft Or TopRight Or BottomLeft Or BottomRight
        AllTop = TopLeft Or TopRight
        AllLeft = TopLeft Or BottomLeft
        AllRight = TopRight Or BottomRight
        AllBottom = BottomLeft Or BottomRight
    End Enum

End Module


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions