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

来源:互联网 发布:换手机 备份数据 编辑:程序博客网 时间:2024/05/22 05:15
Imports System.Drawing.Drawing2D
Public Class RoundPanel
    
Inherits Panel

    
Private mMatrixRound As Integer = 8
    
Private mBack As Color

    
Public 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() As Integer
        
Get
            
Return mMatrixRound
        
End Get
        
Set(ByVal value As Integer)
            mMatrixRound 
= value
            
MyBase.Refresh()
        
End Set
    
End Property


    
Private Function CreateRound(ByVal rect As Rectangle, ByVal radius As IntegerAs 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, 27090)                        '右上角 
        RoundRect.AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom - radius)                  '右边 
        RoundRect.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 090)                  '右下角 
        RoundRect.AddLine(rect.Right - radius, rect.Bottom, rect.Left + radius, rect.Bottom)                '底边 
        RoundRect.AddArc(rect.Left - 1, rect.Bottom - radius, radius, radius, 9090)                       '左下角 
        RoundRect.AddLine(rect.Left - 1, rect.Top + radius, rect.Left - 1, rect.Bottom - radius)            '左边 
        RoundRect.AddArc(rect.Left - 1, rect.Top - 1, radius, radius, 18090)                              '左上角 
        Return RoundRect
    
End Function


    
Protected Overrides 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(ByVal e As System.EventArgs)
        
MyBase.Refresh()
    
End Sub

End Class
原创粉丝点击