Francesco Geri

Il blog di Francesco Geri
posts - 94, comments - 165, trackbacks - 2

Disegnare un rettangolo con angoli arrotondati in VB.NET (GDI+)

Di seguito una funzione che consente di disegnare un rettangolo con angoli arrotondati, sfruttando la GDI+ in VB.NET.

 

    1 ''' <summary>

    2 ''' Disegna un rettangolo con i bordi arrotondati

    3 ''' </summary>

    4 ''' <param name="g">Oggetto Graphics per il disegno</param>

    5 ''' <param name="pen">Oggetto <see cref="Pen"></see> con cui

    6 ''' disegnare il rettangolo</param>

    7 ''' <param name="roundedRectangle">Rettangolo da disegnare</param>

    8 ''' <param name="cornerSize">Dimensioni dell'angolo arrotondato</param>

    9 Public Shared Sub DrawRoundedRectangle(ByVal g As Graphics, ByVal pen As Pen, _

   10 ByVal roundedRectangle As Rectangle, ByVal cornerSize As Size)

   11   Dim points(7) As Point

   12   Dim rX As Single = roundedRectangle.Location.X

   13   Dim rY As Single = roundedRectangle.Location.Y

   14   Dim rW As Single = roundedRectangle.Width

   15   Dim rH As Single = roundedRectangle.Height

   16   Dim cW As Single = cornerSize.Width

   17   Dim cH As Single = cornerSize.Height

   18 

   19   ' Prepara i punti per le linee

   20   points(0) = New Point(rX + cW / 2, rY)

   21   points(1) = New Point(rX + rW - cW / 2, rY)

   22   points(2) = New Point(rX + rW, rY + cH / 2)

   23   points(3) = New Point(rX + rW, rY + rH - cH / 2)

   24   points(4) = New Point(rX + rW - cW / 2, rY + rH)

   25   points(5) = New Point(rX + cW / 2, rY + rH)

   26   points(6) = New Point(rX, rY + rH - cH / 2)

   27   points(7) = New Point(rX, rY + cH / 2)

   28 

   29   Dim rectPath As New GraphicsPath()

   30   rectPath.StartFigure()

   31   rectPath.AddLine(points(0), points(1))

   32   rectPath.AddArc(rX + rW - cW, rY, cW, cH, CType(270, Single), CType(90, Single))

   33   rectPath.AddLine(points(2), points(3))

   34   rectPath.AddArc(rX + rW - cW, rY + rH - cH, cW, cH, CType(0, Single), CType(90, Single))

   35   rectPath.AddLine(points(4), points(5))

   36   rectPath.AddArc(rX, rY + rH - cH, cW, cH, CType(90, Single), CType(90, Single))

   37   rectPath.AddLine(points(6), points(7))

   38   rectPath.AddArc(rX, rY, cW, cH, CType(180, Single), CType(90, Single))

   39   rectPath.CloseFigure()

   40 

   41   g.DrawPath(pen, rectPath)

   42 

   43 End Sub

 

 

 

... e questo è un esempio di cosa si ottiene:

 

image

Print | posted on martedì 2 settembre 2008 22:48 | Filed Under [ .Net GDI+ ]

Powered by:
Powered By Subtext Powered By ASP.NET