Advertisement
netrosly

RoundLabel

Aug 1st, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.92 KB | None | 0 0
  1. Imports System.Drawing.Drawing2D
  2.  
  3. Public Class RoundLabel
  4.     Inherits Control
  5.     Property mainColor As Color = Color.FromArgb(242, 108, 79)
  6.     Property textColor As Color = Color.White
  7.     Property resizeText As Boolean = False
  8. #Region "Round Rectangle"
  9.     Public Shared Function NTRound(rectangle As Rectangle, slope As Integer) As GraphicsPath
  10.         Dim path = New GraphicsPath(FillMode.Winding)
  11.         path.AddArc(rectangle.X - slope, rectangle.Y, slope, slope, 180.0F, 90.0F)
  12.         path.AddArc(rectangle.Right, rectangle.Y, slope, slope, 270.0F, 90.0F)
  13.         path.AddArc(rectangle.Right - slope, rectangle.Bottom - slope, slope, slope, 0.0F, 90.0F)
  14.         path.AddArc(rectangle.X, rectangle.Bottom - slope, slope, slope, 90.0F, 90.0F)
  15.         path.CloseFigure()
  16.         Return path
  17.     End Function
  18.  
  19.     Public Shared Function NTRound(x As Integer, y As Integer, height As Integer, width As Integer, slope As Integer) As GraphicsPath
  20.         Return Round(New Rectangle(x, y, height, width), slope)
  21.     End Function
  22.  
  23.     Public Shared Function Round(rectangle As Rectangle, slope As Integer) As GraphicsPath
  24.         Dim path = New GraphicsPath(FillMode.Winding)
  25.         path.AddArc(rectangle.X, rectangle.Y, slope, slope, 180.0F, 90.0F)
  26.         path.AddArc(rectangle.Right - slope, rectangle.Y, slope, slope, 270.0F, 90.0F)
  27.         path.AddArc(rectangle.Right - slope, rectangle.Bottom - slope, slope, slope, 0.0F, 90.0F)
  28.         path.AddArc(rectangle.X, rectangle.Bottom - slope, slope, slope, 90.0F, 90.0F)
  29.         path.CloseFigure()
  30.         Return path
  31.     End Function
  32.  
  33.     Public Shared Function Round(x As Integer, y As Integer, height As Integer, width As Integer, slope As Integer) As GraphicsPath
  34.         Return Round(New Rectangle(x, y, height, width), slope)
  35.     End Function
  36. #End Region
  37.     Sub New()
  38.         Me.DoubleBuffered = True
  39.         Me.Size = New Size(160, 20)
  40.     End Sub
  41.  
  42.     Private Sub LabelR_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
  43.         Dim g As Graphics = e.Graphics
  44.         g.SmoothingMode = SmoothingMode.AntiAlias
  45.         g.FillPath(New SolidBrush(mainColor), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), Me.Height))
  46.         If resizeText Then
  47.             'Font resizing by Andro72(http://goo.gl/1kS7O0)
  48.             Dim extent As SizeF = g.MeasureString(Me.Text, Me.Font)
  49.             Dim hRatio As Single = Me.Height / extent.Height
  50.             Dim wRatio As Single = Me.Width / extent.Width
  51.             Dim ratio As Single = If((hRatio < wRatio), hRatio, wRatio)
  52.             Dim newSize As Single = Me.Font.Size * ratio
  53.             Me.Font = New Font(Me.Font.FontFamily, newSize, Me.Font.Style)
  54.         End If
  55.         g.DrawString(Me.Text, Me.Font, New SolidBrush(textColor), New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
  56.     End Sub
  57. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement