您的位置:首页 > 编程语言 > VB

VB.NET构建圆角平滑的矩形控件(示例Panel)

2007-11-14 02:58 1371 查看

Imports System.Drawing.Drawing2D




Public Class RoundPanelClass RoundPanel


    Inherits Panel




    Private mMatrixRound As Integer = 8


    Private mBack As Color






    Public Property Back()Property Back() As Color


        Get


            Return mBack


        End Get


        Set(ByVal value As Color)


            If value = Nothing Then


                mBack = Control.DefaultBackColor


            Else


                mBack = value


            End If


            MyBase.Refresh()


        End Set


    End Property






    Public Property MatrixRound()Property MatrixRound() As Integer


        Get


            Return mMatrixRound


        End Get


        Set(ByVal value As Integer)


            mMatrixRound = value


            MyBase.Refresh()


        End Set


    End Property






    Private Function CreateRound()Function CreateRound(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath


        Dim RoundRect As New GraphicsPath


        RoundRect.AddLine(rect.Left + radius - 1, rect.Top - 1, rect.Right - radius, rect.Top - 1)          '顶端 


        RoundRect.AddArc(rect.Right - radius, rect.Top - 1, radius, radius, 270, 90)                        '右上角 


        RoundRect.AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom - radius)                  '右边 


        RoundRect.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90)                  '右下角 


        RoundRect.AddLine(rect.Right - radius, rect.Bottom, rect.Left + radius, rect.Bottom)                '底边 


        RoundRect.AddArc(rect.Left - 1, rect.Bottom - radius, radius, radius, 90, 90)                       '左下角 


        RoundRect.AddLine(rect.Left - 1, rect.Top + radius, rect.Left - 1, rect.Bottom - radius)            '左边 


        RoundRect.AddArc(rect.Left - 1, rect.Top - 1, radius, radius, 180, 90)                              '左上角 


        Return RoundRect


    End Function






    Protected Overrides Sub OnPaint()Sub OnPaint(ByVal e As PaintEventArgs)


        Dim W As Integer = MyBase.Width - MyBase.Margin.Left - MyBase.Margin.Right


        Dim H As Integer = MyBase.Height - MyBase.Margin.Top - MyBase.Margin.Bottom


        Dim Rec As New Rectangle(MyBase.Margin.Left, MyBase.Margin.Top, W, H)


        Dim Round As GraphicsPath = CreateRound(Rec, mMatrixRound)


        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias


        e.Graphics.FillPath(DirectCast(New SolidBrush(mBack), Brush), Round)


    End Sub






    Protected Overrides Sub OnResize()Sub OnResize(ByVal e As System.EventArgs)


        MyBase.Refresh()


    End Sub


End Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息