Adptation vb.net du source de sebmafate sur www.csharpfr.com :
http://www.csharpfr.com/code.aspx?ID=35392
Permet de créer des rectangles arrondis avec des dégradés de couleur
(tout le mérite est pour lui)
Source / Exemple :
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing.Drawing2D
Imports System.Drawing
Public Class clsRoundedCorner
''' <summary>
''' Enregistre les informations n??cessaires ?? la cr??ation d'un Rectangle dont
''' les coins sont arrondis
''' </summary>
Public Structure RoundedRectangle
Private x_ As Integer
Private y_ As Integer
Private width_ As Integer
Private height_ As Integer
Private radius_ As Single
Private roundedCorner_ As RoundedCorner
Public Shared Empty As RoundedRectangle
''' <summary>
''' Initialise un rectangle avec les coins arrondis dont le point sup??rieur gauche
''' aura pour coordonn??es (<i>x_</i>,<i>y_</i>), comme largeur/hauteur <i>width_</i> et <i>height_</i>, des arrondis
''' aux coins <i>rc</i> et enfin les arrondis auront comme rayon <i>radius_</i>
''' </summary>
''' <param name="x_">Abscisse du point sup??rieur gauche</param>
''' <param name="y_">Ordonn??e du point sup??rieur gauche</param>
''' <param name="width_">Largeur du rectangle</param>
''' <param name="height_">Hauteur du rectangle</param>
''' <param name="rc">Coins arrondis</param>
''' <param name="radius_">Rayon des arrondis</param>
Public Sub New(ByVal x_ As Integer, ByVal y_ As Integer, ByVal width_ As Integer, ByVal height_ As Integer, _
ByVal rc As RoundedCorner, ByVal radius_ As Single)
Me.x_ = x_
Me.y_ = y_
Me.width_ = width_
Me.height_ = height_
Me.radius_ = radius_
Me.roundedCorner_ = rc
End Sub
''' <summary>
''' Initialise un rectangle ?? partir du Rectangle <i>rect</i>, des arrondis
''' aux coins <i>rc</i> et enfin les arrondis auront comme rayon <i>radius_</i>
''' </summary>
''' <param name="rect">Rectangle de base</param>
''' <param name="rc">Coins arrondis</param>
''' <param name="radius_">Rayon des arrondis</param>
Public Sub New(ByVal rect As Rectangle, ByVal rc As RoundedCorner, ByVal radius_ As Single)
Me.x_ = rect.X
Me.y_ = rect.Y
Me.width_ = rect.Width
Me.height_ = rect.Height
Me.radius_ = radius_
Me.roundedCorner_ = rc
End Sub
''' <summary>
''' Initialise un rectangle avec les coins arrondis dont le point sup??rieur gauche
''' aura pour coordonn??es <i>location</i>, comme taille <i>size</i>, des arrondis
''' aux coins <i>rc</i> et enfin les arrondis auront comme rayon <i>radius_</i>
''' </summary>
''' <param name="location"></param>
''' <param name="size"></param>
''' <param name="rc">Coins arrondis</param>
''' <param name="radius_">Rayon des arrondis</param>
Public Sub New(ByVal location As Point, ByVal size As Size, ByVal rc As RoundedCorner, _
ByVal radius_ As Single)
Me.x_ = location.X
Me.y_ = location.Y
Me.width_ = size.Width
Me.height_ = size.Height
Me.radius_ = radius_
Me.roundedCorner_ = rc
End Sub
''' <summary>
''' Obtient ou d??fini une enum??ration indiquant les coins arrondis
''' </summary>
Public Property RoundedCorners() As RoundedCorner
Get
Return Me.roundedCorner_
End Get
Set(ByVal value As RoundedCorner)
Me.roundedCorner_ = value
End Set
End Property
''' <summary>
''' Obtient ou d??fini la taille du rectangle
''' </summary>
Public Property Size() As Size
Get
Return New Size(Me.width_, Me.height_)
End Get
Set(ByVal value As Size)
Me.width_ = value.Width
Me.height_ = value.Height
End Set
End Property
''' <summary>
''' Obtient ou d??fini l'abscisse du coin sup??rieur gauche du rectangle
''' </summary>
Public Property X() As Integer
Get
Return Me.x_
End Get
Set(ByVal value As Integer)
Me.x_ = value
End Set
End Property
''' <summary>
''' Obtient ou d??fini l'ordonn??e du coin sup??rieur gauche du rectangle
''' </summary>
Public Property Y() As Integer
Get
Return Me.y_
End Get
Set(ByVal value As Integer)
Me.y_ = value
End Set
End Property
''' <summary>
''' Obtient ou d??fini la largeur du rectangle
''' </summary>
Public Property Width() As Integer
Get
Return Me.width_
End Get
Set(ByVal value As Integer)
Me.width_ = value
End Set
End Property
''' <summary>
''' Obtient ou d??fini la hauteur du rectangle
''' </summary>
Public Property Height() As Integer
Get
Return Me.height_
End Get
Set(ByVal value As Integer)
Me.height_ = value
End Set
End Property
''' <summary>
''' Obtient l'abscisse des coins de gauche
''' </summary>
Public ReadOnly Property Left() As Integer
Get
Return Me.x_
End Get
End Property
''' <summary>
''' Obtient l'abscisse des coins de droit
''' </summary>
Public ReadOnly Property Right() As Integer
Get
Return (Me.x_ + Me.width_)
End Get
End Property
''' <summary>
''' Obtient l'ordonn??e des coins du haut
''' </summary>
Public ReadOnly Property Top() As Integer
Get
Return Me.y_
End Get
End Property
''' <summary>
''' Obtient l'ordonn??e des coins du bas
''' </summary>
Public ReadOnly Property Bottom() As Integer
Get
Return (Me.y_ + Me.height_)
End Get
End Property
''' <summary>
''' Obtient ou d??fini l'emplacement du coin sup??rieur gauche
''' </summary>
Public Property Location() As Point
Get
Return New Point(Me.x_, Me.y_)
End Get
Set(ByVal value As Point)
Me.x_ = value.X
Me.y_ = value.Y
End Set
End Property
''' <summary>
''' Obtient ou d??fini le rayon de courbure des coins
''' </summary>
Public Property Radius() As Single
Get
Return radius_
End Get
Set(ByVal value As Single)
radius_ = value
End Set
End Property
''' <summary>
''' Agrandi le rectangle de la taille sp??cifi??e
''' </summary>
''' <param name="width_">largeur</param>
''' <param name="height_">hauteur</param>
Public Overloads Sub Inflate(ByVal width_ As Integer, ByVal height_ As Integer)
Me.x_ = (Me.x_ - width_)
Me.y_ = (Me.y_ - height_)
Me.width_ = (Me.width_ + (2 * width_))
Me.height_ = (Me.height_ + (2 * height_))
End Sub
''' <summary>
''' Agrandi le rectangle de la taille sp??cifi??e
''' </summary>
''' <param name="size">taille</param>
Public Overloads Sub Inflate(ByVal size As Size)
Me.Inflate(size.Width, size.Height)
End Sub
''' <summary>
''' Cr??e et retourne une copie RoundedRectangle agrandi de la taille sp??cifi??.
''' </summary>
''' <param name="rrect">RoundedRectangle</param>
''' <param name="width_">Largeur</param>
''' <param name="height_">Hauteur</param>
''' <returns>Une copie du RoundedRectangle agrandie</returns>
Public Overloads Shared Function Inflate(ByVal rrect As RoundedRectangle, _
ByVal width_ As Integer, ByVal height_ As Integer) As RoundedRectangle
Dim r As RoundedRectangle = rrect
r.Inflate(width_, height_)
Return r
End Function
''' <summary>
''' Retourne un RoundRectangle correspondant ?? l'intersection des RoundRectangles a et b
''' </summary>
''' <param name="a">RoundedRectangle</param>
''' <param name="b">RoundedRectangle</param>
''' <param name="rc">Coins ?? arrondir</param>
''' <param name="radius_">Rayon de coubure des arrondis</param>
''' <returns></returns>
''' <remarks>Si aucune intersection n'est trouv??e entre les RoundRectangles, un RoundedRectangle est retourn??</remarks>
Public Overloads Shared Function Intersect(ByVal a As RoundedRectangle, _
ByVal b As RoundedRectangle, ByVal rc As RoundedCorner, _
ByVal radius_ As Single) As RoundedRectangle
Dim left As Integer = Math.Max(a.Left, b.Left)
Dim right As Integer = Math.Min(a.Right, b.Right)
Dim top As Integer = Math.Max(a.Top, b.Top)
Dim bottom As Integer = Math.Min(a.Bottom, b.Bottom)
If ((left <= right) _
AndAlso (top <= bottom)) Then
Return New RoundedRectangle(left, (right - left), top, (bottom - top), rc, radius_)
End If
Return RoundedRectangle.Empty
End Function
''' <summary>
''' Replace ce RoundedRectangle par l'intersection de celui-ci de rrect
''' </summary>
''' <param name="rrect">RoundedRectangle</param>
Public Overloads Sub Intersect(ByVal rrect As RoundedRectangle)
Dim r As RoundedRectangle = RoundedRectangle.Intersect(rrect, Me, _
Me.roundedCorner_, Me.radius_)
Me.x_ = r.x_
Me.y_ = r.y_
Me.width_ = r.width_
Me.height_ = r.height_
End Sub
''' <summary>
''' Retourne <i>true</i> si ce RoundedRectangle poss??de une intersection avec rrect, <i>false</i> sinon
''' </summary>
''' <param name="rrect">RoundedRectangle</param>
''' <returns><i>true</i> si intersection, <i>false</i> sinon</returns>
Public Function IntersectWith(ByVal rrect As RoundedRectangle) As Boolean
Return (((rrect.Left < Me.Right) _
AndAlso (Me.Left < rrect.Right)) _
AndAlso ((rrect.Top < Me.Bottom) _
AndAlso (Me.Top < rrect.Bottom)))
'if (((rect.X < (this.X + this.Width)) && (this.X < (rect.X + rect.Width))) && (rect.Y < (this.Y + this.Height)))
'{
' return (this.Y < (rect.Y + rect.Height));
'}
'return false;
End Function
''' <summary>
''' Retourne un RoundedRectangle qui correspond ?? l'intersection des RoundedRectangle a et b et dont les
''' coins <i>rc</i> sont arrondis d'un rayon de <i>radius_</i>
''' </summary>
''' <param name="a">RoundedRectangle</param>
''' <param name="b">RoundedRectangle</param>
''' <param name="rc">Coins ?? arrondir</param>
''' <param name="radius_">Rayon de courbure des arrondis</param>
''' <returns>RoundedRectangle de l'union</returns>
Public Shared Function Union(ByVal a As RoundedRectangle, _
ByVal b As RoundedRectangle, ByVal rc As RoundedCorner, _
ByVal radius_ As Single) As RoundedRectangle
Dim left As Integer = Math.Min(a.Left, b.Left)
Dim right As Integer = Math.Max(a.Right, b.Right)
Dim top As Integer = Math.Min(a.Top, b.Top)
Dim bottom As Integer = Math.Max(a.Bottom, b.Bottom)
Return New RoundedRectangle(left, (right - left), top, (bottom - top), rc, radius_)
End Function
''' <summary>
''' D??place le RoundedRectangle d'apr??s les coordonn??es sp??cifi??s
''' </summary>
''' <param name="x_">D??placement sur l'axe des abscisses</param>
''' <param name="y_">D??placement sur l'axe des ordon??es</param>
Public Overloads Sub Offset(ByVal x_ As Integer, ByVal y_ As Integer)
Me.x_ = (Me.x_ + x_)
Me.y_ = (Me.y_ + y_)
End Sub
''' <summary>
''' D??place le RoundedRectangle d'apr??s les ccordonn??es sp??cifi??es
''' </summary>
''' <param name="point">Point contenant les informations de d??placements</param>
Public Overloads Sub Offset(ByVal point As Point)
Me.x_ = (Me.x_ + point.X)
Me.y_ = (Me.y_ + point.Y)
End Sub
''' <summary>
''' Retourne un bool??en indiquant si le point sp??cifi?? fait partie du RoundedRectangle
''' </summary>
''' <param name="x_">Abscisse</param>
''' <param name="y_">Ordonn??e</param>
''' <returns><i>true</i> si le point se trouve ?? l'int??rieur du RoundedRectangle, <i>false</i> sinon</returns>
Public Overloads Function Contains(ByVal x_ As Integer, ByVal y_ As Integer) As Boolean
Return ((Me.x_ <= x_) _
AndAlso ((x_ <= Me.Right) _
AndAlso ((Me.y_ <= y_) _
AndAlso (y_ <= Me.Bottom))))
End Function
''' <summary>
''' Retourne un bool??en indiquant si le point sp??cifi?? fait partie du RoundedRectangle
''' </summary>
''' <param name="pt">Point ?? v??rifier</param>
''' <returns><i>true</i> si le point se trouve ?? l'int??rieur du RoundedRectangle, <i>false</i> sinon</returns>
Public Overloads Function Contains(ByVal pt As Point) As Boolean
Return Contains(pt.X, pt.Y)
End Function
''' <summary>
''' Retourne un <see cref="GraphicsPath"/> repr??sentant le RoundedRectangle.
''' </summary>
''' <returns><see cref="GraphicsPath"/></returns>
''' <seealso cref="GraphicsPath"/>
Public Function ToGraphicsPath() As GraphicsPath
Dim gp As GraphicsPath
'Dim temp As PointF
gp = New GraphicsPath
'Rectangle baseRect = new Rectangle(this.Location, this.Size);
' si le rayon est inf??rieur ou ??gal ?? 0
' on retourne le rectangle de base
If (radius_ <= 0.0!) Then
gp.AddRectangle(New Rectangle(Me.Location, Me.Size))
gp.CloseFigure()
Return gp
End If
Dim diameter As Single = (radius_ * 2.0!)
If (Me.height_ <= Me.width_) Then
If ((diameter >= Me.height_) _
AndAlso (((Me.roundedCorner_ _
And (RoundedCorner.BottomLeft Or RoundedCorner.TopLeft)) _
= (RoundedCorner.BottomLeft Or RoundedCorner.TopLeft)) _
OrElse ((Me.roundedCorner_ _
And (RoundedCorner.BottomRight Or RoundedCorner.TopRight)) _
= (RoundedCorner.BottomRight Or RoundedCorner.TopRight)))) Then
diameter = Me.height_
End If
ElseIf ((diameter >= Me.width_) _
AndAlso (((Me.roundedCorner_ _
And (RoundedCorner.BottomLeft Or RoundedCorner.BottomRight)) _
= (RoundedCorner.BottomLeft Or RoundedCorner.BottomRight)) _
OrElse ((Me.roundedCorner_ _
And (RoundedCorner.TopLeft Or RoundedCorner.TopRight)) _
= (RoundedCorner.TopLeft Or RoundedCorner.TopRight)))) Then
diameter = Me.width_
End If
Dim size As SizeF = New SizeF(diameter, diameter)
Dim arc As RectangleF = New RectangleF(Me.Location, size)
' arc en haut ?? gauche
If ((Me.roundedCorner_ And RoundedCorner.TopLeft) _
= RoundedCorner.TopLeft) Then
gp.AddArc(arc, 180, 90)
Else
gp.AddLine(New PointF(arc.Left, arc.Top), New PointF(arc.Left, arc.Top))
End If
' arc en haut ?? droite
arc.X = (CType(Me.Right, Single) - diameter)
If ((Me.roundedCorner_ And RoundedCorner.TopRight) _
= RoundedCorner.TopRight) Then
gp.AddArc(arc, 270, 90)
Else
gp.AddLine(New PointF(arc.Right, arc.Top), New PointF(arc.Right, arc.Top))
End If
arc.Y = (CType(Me.Bottom, Single) - diameter)
' arc en bas ?? droite
If ((Me.roundedCorner_ And RoundedCorner.BottomRight) _
= RoundedCorner.BottomRight) Then
gp.AddArc(arc, 0, 90)
Else
gp.AddLine(New PointF(arc.Right, arc.Bottom), New PointF(arc.Right, arc.Bottom))
End If
' arc en bas ?? gauche
arc.X = CType(Me.Left, Single)
If ((Me.roundedCorner_ And RoundedCorner.BottomLeft) _
= RoundedCorner.BottomLeft) Then
gp.AddArc(arc, 90, 90)
Else
gp.AddLine(New PointF(arc.Left, arc.Bottom), New PointF(arc.Left, arc.Bottom))
End If
gp.CloseFigure()
Return gp
End Function
''' <summary>
''' Retourne un <see cref="Rectangle"/>
''' </summary>
''' <returns><see cref="Rectangle"/></returns>
Public Function ToRectangle() As Rectangle
Return New Rectangle(Me.Location, Me.Size)
End Function
#Region " overrides "
Public Overrides Function ToString() As String
Return String.Format("{{X={0},Y={1},Width={2},Height={3},Radius={4}}}", Me.x_, _
Me.y_, Me.width_, Me.height_, Me.radius_)
End Function
Public Shared Operator =(ByVal a As RoundedRectangle, ByVal b As RoundedRectangle) As Boolean
Return (a.X = b.X And a.Y = b.Y And a.Width = b.Width And a.Height = b.Height)
End Operator
Public Shared Operator <>(ByVal a As RoundedRectangle, ByVal b As RoundedRectangle) As Boolean
Return Not (a = b)
End Operator
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Dim ret As Boolean = False
If (obj.GetType.Name = GetType(RoundedRectangle).Name) Then
Dim r As RoundedRectangle = obj
ret = (r.X = Me.X And r.Y = Me.Y And r.Width = Me.Width And r.Height = Me.Height)
End If
Return ret
End Function
Public Overrides Function GetHashCode() As Integer
Return Me.GetHashCode()
End Function
#End Region
End Structure
<Flags()> _
Public Enum RoundedCorner
TopLeft = 2
TopRight = 4
BottomLeft = 8
BottomRight = 16
All = TopLeft Or TopRight Or BottomLeft Or BottomRight
End Enum
End Class
Conclusion :
(J'ai trouvé util de le traduire pour l'intégrer à mes applications)
''''Exemple d'utilisation
Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
sbDesign(e)
End Sub
Private Sub sbDesign(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim rect As New Rectangle(0, 0, Me.Width, Me.Height)
Dim gc As Graphics = Me.CreateGraphics()
gc.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
Dim rr As clsRoundedCorner.RoundedRectangle = _
New clsRoundedCorner.RoundedRectangle(0, 0, Width, Height, RoundedCorner.All, 16)
rr.RoundedCorners = RoundedCorner.All
gc.FillPath(New Drawing2D.LinearGradientBrush(rr.ToRectangle(), _
Color.Honeydew, Color.LightGreen, _
Drawing2D.LinearGradientMode.ForwardDiagonal), rr.ToGraphicsPath())
End Sub
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.