Advertisement
netrosly

PokeButton

Aug 1st, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.07 KB | None | 0 0
  1. Imports System.Drawing.Drawing2D
  2.  
  3. Public Class PokeButton
  4.     Inherits Control
  5.     Sub New()
  6.         Me.DoubleBuffered = True
  7.         Me.Size = New Size(160, 40)
  8.     End Sub
  9.     Property Shadow As Boolean = False
  10.     Property Color1 As Color = Color.FromArgb(169, 219, 156)
  11.     Property Color2 As Color = Color.FromArgb(29, 209, 165)
  12.     Property BorderColor As Color = Color.FromArgb(29, 209, 165)
  13.     Property TextColor As Color = Color.White
  14.     Property Curve As Integer = 40
  15.     Dim mHover As Boolean = False
  16.     Dim mClick As Boolean = False
  17. #Region "Round Rectangle"
  18.     Public Shared Function NTRound(rectangle As Rectangle, slope As Integer) As GraphicsPath
  19.         Dim path = New GraphicsPath(FillMode.Winding)
  20.         path.AddArc(rectangle.X - slope, rectangle.Y, slope, slope, 180.0F, 90.0F)
  21.         path.AddArc(rectangle.Right, rectangle.Y, slope, slope, 270.0F, 90.0F)
  22.         path.AddArc(rectangle.Right - slope, rectangle.Bottom - slope, slope, slope, 0.0F, 90.0F)
  23.         path.AddArc(rectangle.X, rectangle.Bottom - slope, slope, slope, 90.0F, 90.0F)
  24.         path.CloseFigure()
  25.         Return path
  26.     End Function
  27.  
  28.     Public Shared Function NTRound(x As Integer, y As Integer, height As Integer, width As Integer, slope As Integer) As GraphicsPath
  29.         Return Round(New Rectangle(x, y, height, width), slope)
  30.     End Function
  31.  
  32.     Public Shared Function Round(rectangle As Rectangle, slope As Integer) As GraphicsPath
  33.         Dim path = New GraphicsPath(FillMode.Winding)
  34.         path.AddArc(rectangle.X, rectangle.Y, slope, slope, 180.0F, 90.0F)
  35.         path.AddArc(rectangle.Right - slope, rectangle.Y, slope, slope, 270.0F, 90.0F)
  36.         path.AddArc(rectangle.Right - slope, rectangle.Bottom - slope, slope, slope, 0.0F, 90.0F)
  37.         path.AddArc(rectangle.X, rectangle.Bottom - slope, slope, slope, 90.0F, 90.0F)
  38.         path.CloseFigure()
  39.         Return path
  40.     End Function
  41.  
  42.     Public Shared Function Round(x As Integer, y As Integer, height As Integer, width As Integer, slope As Integer) As GraphicsPath
  43.         Return Round(New Rectangle(x, y, height, width), slope)
  44.     End Function
  45. #End Region
  46.     Public Function gb(e As Graphics, r As Rectangle, c1 As Color, c2 As Color) As LinearGradientBrush
  47.         Dim g As Graphics = e
  48.         Dim p1 As Point = r.Location
  49.         Dim p2 As Point = New Point(r.Right, r.Bottom)
  50.         Dim brsGradient As New System.Drawing.Drawing2D.LinearGradientBrush(p1, p2, c1, c2)
  51.         Return brsGradient
  52.     End Function
  53.     Protected Overrides Sub OnPaint(e As PaintEventArgs)
  54.         Dim b As New Bitmap(Width, Height)
  55.         Dim g As Graphics = Graphics.FromImage(b)
  56.         g.Clear(BackColor)
  57.         g.SmoothingMode = SmoothingMode.AntiAlias
  58.         If Shadow Then
  59.             g.FillPath(New SolidBrush(Color.FromArgb(120, Color.Gray)), Round(New Rectangle(8, 8, Me.Width - 10, Me.Height - 10), Curve))
  60.             g.FillPath(New SolidBrush(BorderColor), Round(New Rectangle(0, 0, Me.Width - 10, Me.Height - 10), Curve + 2))
  61.             g.FillPath(gb(g, New Rectangle(1, 1, Me.Width - 12, Me.Height - 12), Color1, Color2), Round(New Rectangle(1, 1, Me.Width - 12, Me.Height - 12), Curve))
  62.             g.DrawString(Text, New Font("Arial", 10, FontStyle.Bold), New SolidBrush(TextColor), New Rectangle(1, 1, Me.Width - 12, Me.Height - 12), New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
  63.         Else
  64.             g.FillPath(New SolidBrush(BorderColor), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), Curve + 2))
  65.             g.FillPath(gb(g, New Rectangle(1, 1, Me.Width - 3, Me.Height - 3), Color1, Color2), Round(New Rectangle(1, 1, Me.Width - 3, Me.Height - 3), Curve))
  66.             g.DrawString(Text, New Font("Arial", 10, FontStyle.Bold), New SolidBrush(TextColor), New Rectangle(1, 1, Me.Width - 3, Me.Height - 3), New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center})
  67.         End If
  68.         If mHover And mClick = False Then
  69.             g.FillPath(New SolidBrush(Color.FromArgb(60, Color.Gray)), Round(New Rectangle(1, 1, Me.Width - 3, Me.Height - 3), Curve))
  70.         ElseIf mClick = True Then
  71.             g.FillPath(New SolidBrush(Color.FromArgb(60, Color.Black)), Round(New Rectangle(1, 1, Me.Width - 3, Me.Height - 3), Curve))
  72.  
  73.         End If
  74.      
  75.         e.Graphics.DrawImage(b.Clone, 0, 0)
  76.         g.Dispose()
  77.         b.Dispose()
  78.     End Sub
  79.  
  80.     Private Sub PokeButton_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
  81.         If e.Button = Windows.Forms.MouseButtons.Left Then
  82.             If Shadow Then
  83.                 If New Rectangle(1, 1, Me.Width - 12, Me.Height - 12).Contains(e.X, e.Y) Then
  84.                     mClick = True
  85.  
  86.                     Cursor = Cursors.Hand
  87.                     Me.Refresh()
  88.                 End If
  89.             Else
  90.                 If New Rectangle(1, 1, Me.Width - 3, Me.Height - 3).Contains(e.X, e.Y) Then
  91.                     mClick = True
  92.                     Cursor = Cursors.Hand
  93.                     Me.Refresh()
  94.                 End If
  95.             End If
  96.         End If
  97.     End Sub
  98.  
  99.     Private Sub PokeButton_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
  100.         mHover = False
  101.         Me.Refresh()
  102.     End Sub
  103.  
  104.     Private Sub PokeButton_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
  105.         If Shadow Then
  106.             If New Rectangle(1, 1, Me.Width - 12, Me.Height - 12).Contains(e.X, e.Y) Then
  107.                 mHover = True
  108.                 Cursor = Cursors.Hand
  109.                 Me.Refresh()
  110.             End If
  111.         Else
  112.             If New Rectangle(1, 1, Me.Width - 3, Me.Height - 3).Contains(e.X, e.Y) Then
  113.                 mHover = True
  114.                 Cursor = Cursors.Hand
  115.                 Me.Refresh()
  116.             End If
  117.         End If
  118.  
  119.     End Sub
  120. Event Clicked()
  121.     Private Sub PokeButton_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
  122.         RaiseEvent Clicked()
  123.         mHover = False
  124.         mClick = False
  125.         Me.Refresh()
  126.     End Sub
  127. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement