Advertisement
TJ45

Botão com bordas arredondadas [VB NET] [HERANÇA]

Mar 22nd, 2017
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.14 KB | None | 0 0
  1. Imports System.Drawing.Drawing2D
  2. Imports System.ComponentModel
  3.  
  4. Public Class BotaoRedondo
  5.  
  6.     Inherits Control
  7.  
  8.     Private MouseState As Integer
  9.     Private Shape As GraphicsPath
  10.     Private InactiveGB, PressedGB As LinearGradientBrush
  11.     Private R1 As Rectangle
  12.     Private P1, P3 As Pen
  13.     Private _Image As Image
  14.     Private _ImagemSize As Size
  15.     Private _TextAlignment As StringAlignment = StringAlignment.Center
  16.     Private _TextColor As Color = Color.FromArgb(51, 51, 51)
  17.     Private _ImageAlign As ContentAlignment = ContentAlignment.MiddleLeft
  18.  
  19.     Private Shared Function ImageLocation(ByVal SF As StringFormat, ByVal Area As SizeF, ByVal ImageArea As SizeF) As PointF
  20.         Dim MyPoint As PointF
  21.         Select Case SF.Alignment
  22.             Case StringAlignment.Center
  23.                 MyPoint.X = CSng((Area.Width - ImageArea.Width) / 2)
  24.             Case StringAlignment.Near
  25.                 MyPoint.Y = 2
  26.             Case StringAlignment.Far
  27.                 MyPoint.Y = Area.Width - ImageArea.Width - 2
  28.         End Select
  29.  
  30.         Select Case SF.LineAlignment
  31.             Case StringAlignment.Center
  32.                 MyPoint.Y = CSng((Area.Height - ImageArea.Height) / 2)
  33.             Case StringAlignment.Near
  34.                 MyPoint.Y = 2
  35.             Case StringAlignment.Far
  36.                 MyPoint.Y = Area.Height - ImageArea.Height - 2
  37.         End Select
  38.     End Function
  39.  
  40.     Private Function GetStringFormat(ByVal _ContectAlignment As ContentAlignment) As StringFormat
  41.         Dim SF As StringFormat = New StringFormat()
  42.         Select Case _ContectAlignment
  43.             Case ContentAlignment.MiddleCenter
  44.                 SF.LineAlignment = StringAlignment.Center
  45.                 SF.Alignment = StringAlignment.Center
  46.             Case ContentAlignment.MiddleLeft
  47.                 SF.LineAlignment = StringAlignment.Center
  48.                 SF.Alignment = StringAlignment.Near
  49.             Case ContentAlignment.MiddleRight
  50.                 SF.LineAlignment = StringAlignment.Center
  51.                 SF.Alignment = StringAlignment.Far
  52.             Case ContentAlignment.TopCenter
  53.                 SF.LineAlignment = StringAlignment.Near
  54.                 SF.Alignment = StringAlignment.Center
  55.             Case ContentAlignment.TopLeft
  56.                 SF.LineAlignment = StringAlignment.Near
  57.                 SF.Alignment = StringAlignment.Near
  58.             Case ContentAlignment.TopRight
  59.                 SF.LineAlignment = StringAlignment.Near
  60.                 SF.Alignment = StringAlignment.Far
  61.             Case ContentAlignment.BottomCenter
  62.                 SF.LineAlignment = StringAlignment.Far
  63.                 SF.Alignment = StringAlignment.Center
  64.             Case ContentAlignment.BottomLeft
  65.                 SF.LineAlignment = StringAlignment.Far
  66.                 SF.Alignment = StringAlignment.Near
  67.             Case ContentAlignment.BottomRight
  68.                 SF.LineAlignment = StringAlignment.Far
  69.                 SF.Alignment = StringAlignment.Far
  70.         End Select
  71.         Return SF
  72.     End Function
  73.  
  74.     Property Image() As Image
  75.         Get
  76.             Return _Image
  77.         End Get
  78.         Set(ByVal value As Image)
  79.             If value Is Nothing Then
  80.                 _ImagemSize = Size.Empty
  81.             Else
  82.                 _ImagemSize = value.Size
  83.             End If
  84.  
  85.             _Image = value
  86.             Invalidate()
  87.         End Set
  88.     End Property
  89.  
  90.     Protected ReadOnly Property ImageSize() As Size
  91.         Get
  92.             Return _ImagemSize
  93.         End Get
  94.     End Property
  95.  
  96.  
  97.     Public Property ImageAlign() As ContentAlignment
  98.         Get
  99.             Return _ImageAlign
  100.         End Get
  101.         Set(ByVal Value As ContentAlignment)
  102.             _ImageAlign = Value
  103.             Invalidate()
  104.         End Set
  105.     End Property
  106.  
  107.     Public Property TextAlignment As StringAlignment
  108.         Get
  109.             Return Me._TextAlignment
  110.         End Get
  111.         Set(ByVal value As StringAlignment)
  112.             Me._TextAlignment = value
  113.             Me.Invalidate()
  114.         End Set
  115.     End Property
  116.  
  117.     Public Overrides Property ForeColor As Color
  118.         Get
  119.             Return Me._TextColor
  120.         End Get
  121.         Set(ByVal value As Color)
  122.             Me._TextColor = value
  123.             Me.Invalidate()
  124.         End Set
  125.     End Property
  126.  
  127.     Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
  128.         MouseState = 1
  129.         Focus()
  130.         Invalidate()
  131.         MyBase.OnMouseDown(e)
  132.     End Sub
  133.  
  134.  
  135.     Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
  136.         MouseState = 1
  137.         Focus()
  138.         Invalidate()
  139.         MyBase.OnMouseDown(e)
  140.     End Sub
  141.  
  142.     Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
  143.         MouseState = 0
  144.         Invalidate()
  145.         MyBase.OnMouseLeave(e)
  146.     End Sub
  147.  
  148.     Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
  149.         Invalidate()
  150.         MyBase.OnTextChanged(e)
  151.     End Sub
  152.  
  153.     Sub New()
  154.         SetStyle(ControlStyles.AllPaintingInWmPaint Or _
  155.                  ControlStyles.OptimizedDoubleBuffer Or _
  156.                  ControlStyles.ResizeRedraw Or _
  157.                  ControlStyles.SupportsTransparentBackColor Or _
  158.                  ControlStyles.UserPaint, True)
  159.  
  160.  
  161.         BackColor = Color.Transparent
  162.         DoubleBuffered = True
  163.         Font = New Font("Segoe UI", 9)
  164.         ForeColor = Color.FromArgb(255, 255, 255)
  165.         Size = New Size(119, 36)
  166.         _TextAlignment = StringAlignment.Center
  167.         P1 = New Pen(Color.FromArgb(98, 98, 98)) 'cor da borda
  168.         P3 = New Pen(Color.FromArgb(98, 98, 98)) 'cor da borda selecionada
  169.  
  170.     End Sub
  171.  
  172.     Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
  173.         MyBase.OnResize(e)
  174.         If Width > 0 AndAlso Height > 0 Then
  175.  
  176.             Shape = New GraphicsPath
  177.             R1 = New Rectangle(0, 0, Width, Height)
  178.  
  179.             InactiveGB = New LinearGradientBrush(New Rectangle(0, 0, Width, Height), Color.FromArgb(26, 117, 255), Color.FromArgb(26, 117, 255), 90.0F)
  180.             PressedGB = New LinearGradientBrush(New Rectangle(0, 0, Width, Height), Color.FromArgb(77, 148, 255), Color.FromArgb(77, 148, 255), 90.0F)
  181.         End If
  182.  
  183.         With Shape
  184.             .AddArc(0, 0, 10, 10, 180, 90)
  185.             .AddArc(Width - 11, 0, 10, 10, -90, 90)
  186.             .AddArc(Width - 11, Height - 11, 10, 10, 0, 90)
  187.             .AddArc(0, Height - 11, 10, 10, 90, 90)
  188.             .CloseAllFigures()
  189.         End With
  190.         Invalidate()
  191.     End Sub
  192.  
  193.     Protected Overrides Sub OnPaint(e As PaintEventArgs)
  194.         With e.Graphics
  195.             .SmoothingMode = SmoothingMode.HighQuality
  196.             Dim ipt As PointF = ImageLocation(GetStringFormat(ImageAlign), Size, ImageSize)
  197.  
  198.             Select Case MouseState
  199.                 Case 0 'inativo
  200.                     .FillPath(InactiveGB, Shape) 'cor inativa do botao
  201.                     .DrawPath(P1, Shape) 'borda do botao
  202.                     If IsNothing(Image) Then
  203.                         .DrawString(Text, Font, New SolidBrush(ForeColor), R1, New StringFormat() With {.Alignment = _TextAlignment, .LineAlignment = StringAlignment.Center})
  204.                     Else
  205.                         .DrawImage(_Image, ipt.X, ipt.Y, ImageSize.Width, ImageSize.Height)
  206.                         .DrawString(Text, Font, New SolidBrush(ForeColor), R1, New StringFormat() With {.Alignment = _TextAlignment, .LineAlignment = StringAlignment.Center})
  207.                     End If
  208.                 Case 1 'pressionado
  209.                     .FillPath(PressedGB, Shape)
  210.                     .DrawPath(P3, Shape)
  211.  
  212.                     If IsNothing(Image) Then
  213.                         .DrawString(Text, Font, New SolidBrush(ForeColor), R1, New StringFormat() With {.Alignment = _TextAlignment, .LineAlignment = StringAlignment.Center})
  214.                     Else
  215.                         .DrawImage(_Image, ipt.X, ipt.Y, ImageSize.Width, ImageSize.Height)
  216.                         .DrawString(Text, Font, New SolidBrush(ForeColor), R1, New StringFormat() With {.Alignment = _TextAlignment, .LineAlignment = StringAlignment.Center})
  217.                     End If
  218.             End Select
  219.         End With
  220.     End Sub
  221.  
  222. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement