Advertisement
netrosly

Switch

Jul 6th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.16 KB | None | 0 0
  1. Imports System.Drawing.Drawing2D
  2.  
  3. Public Class Switch
  4.     Inherits Control
  5.     Property Color1 As Color = Color.FromArgb(84, 197, 175)
  6.     Property Color2 As Color = Color.FromArgb(65, 154, 137)
  7.     Property Color3 As Color = Color.FromArgb(20, 69, 53)
  8.     Property Color4 As Color = Color.FromArgb(10, 26, 23)
  9.     Property Color5 As Color = Color.White
  10.     Property Toggled As Boolean = False
  11.     Property StyleType As _Style = _Style.Classic
  12.     Sub New()
  13.         Me.DoubleBuffered = True
  14.     End Sub
  15.     Enum _Style
  16.         Classic
  17.         RoundedClassic
  18.         Rounded
  19.         NightRounded
  20.     End Enum
  21. #Region "Round Rectangle"
  22.     Public Shared Function NTRound(rectangle As Rectangle, slope As Integer) As GraphicsPath
  23.         Dim path = New GraphicsPath(FillMode.Winding)
  24.         path.AddArc(rectangle.X - slope, rectangle.Y, slope, slope, 180.0F, 90.0F)
  25.         path.AddArc(rectangle.Right, rectangle.Y, slope, slope, 270.0F, 90.0F)
  26.         path.AddArc(rectangle.Right - slope, rectangle.Bottom - slope, slope, slope, 0.0F, 90.0F)
  27.         path.AddArc(rectangle.X, rectangle.Bottom - slope, slope, slope, 90.0F, 90.0F)
  28.         path.CloseFigure()
  29.         Return path
  30.     End Function
  31.  
  32.     Public Shared Function NTRound(x As Integer, y As Integer, height As Integer, width As Integer, slope As Integer) As GraphicsPath
  33.         Return Round(New Rectangle(x, y, height, width), slope)
  34.     End Function
  35.  
  36.     Public Shared Function Round(rectangle As Rectangle, slope As Integer) As GraphicsPath
  37.         Dim path = New GraphicsPath(FillMode.Winding)
  38.         path.AddArc(rectangle.X, rectangle.Y, slope, slope, 180.0F, 90.0F)
  39.         path.AddArc(rectangle.Right - slope, rectangle.Y, slope, slope, 270.0F, 90.0F)
  40.         path.AddArc(rectangle.Right - slope, rectangle.Bottom - slope, slope, slope, 0.0F, 90.0F)
  41.         path.AddArc(rectangle.X, rectangle.Bottom - slope, slope, slope, 90.0F, 90.0F)
  42.         path.CloseFigure()
  43.         Return path
  44.     End Function
  45.  
  46.     Public Shared Function Round(x As Integer, y As Integer, height As Integer, width As Integer, slope As Integer) As GraphicsPath
  47.         Return Round(New Rectangle(x, y, height, width), slope)
  48.     End Function
  49. #End Region
  50.     Event ToggledChange(toggle As Boolean)
  51.     Private Sub Switch_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
  52.         If e.Button = Windows.Forms.MouseButtons.Left Then
  53.             Select Case StyleType
  54.                 Case _Style.Classic
  55.                     Me.Size = New Size(70, 30)
  56.                     If Toggled Then
  57.                         If New Rectangle(Me.Width - 34, 7, 40, 20).Contains(e.X, e.Y) Then
  58.                             Toggled = Not Toggled
  59.                             RaiseEvent ToggledChange(Toggled)
  60.                         End If
  61.                     Else
  62.                         If New Rectangle(8, 6, 40, 20).Contains(e.X, e.Y) Then
  63.                             Toggled = Not Toggled
  64.                             RaiseEvent ToggledChange(Toggled)
  65.                         End If
  66.                     End If
  67.                 Case _Style.RoundedClassic
  68.                     If Toggled Then
  69.                         If New Rectangle(5, 4, 32, 31).Contains(e.X, e.Y) Then
  70.                             Toggled = Not Toggled
  71.                             RaiseEvent ToggledChange(Toggled)
  72.  
  73.                         End If
  74.                     Else
  75.                         If New Rectangle(Me.Width - 37, 4, 32, 31).Contains(e.X, e.Y) Then
  76.                             Toggled = Not Toggled
  77.                             RaiseEvent ToggledChange(Toggled)
  78.                         End If
  79.                     End If
  80.                 Case _Style.Rounded
  81.                     If Toggled Then
  82.                         If New Rectangle(Me.Width - 37, 4, 32, 31).Contains(e.X, e.Y) Then
  83.                             Toggled = Not Toggled
  84.                             RaiseEvent ToggledChange(Toggled)
  85.  
  86.                         End If
  87.                     Else
  88.                         If New Rectangle(5, 4, 32, 31).Contains(e.X, e.Y) Then
  89.                             Toggled = Not Toggled
  90.                             RaiseEvent ToggledChange(Toggled)
  91.                         End If
  92.                     End If
  93.                 Case _Style.NightRounded
  94.                     If Toggled Then
  95.                         If New Rectangle(5, 4, 32, 31).Contains(e.X, e.Y) Then
  96.                             Toggled = Not Toggled
  97.                             RaiseEvent ToggledChange(Toggled)
  98.  
  99.                         End If
  100.                     Else
  101.                         If New Rectangle(Me.Width - 37, 4, 32, 31).Contains(e.X, e.Y) Then
  102.                             Toggled = Not Toggled
  103.                             RaiseEvent ToggledChange(Toggled)
  104.                         End If
  105.                     End If
  106.             End Select
  107.             Me.Refresh()
  108.         End If
  109.     End Sub
  110.  
  111.     Private Sub Switch_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
  112.         Select Case StyleType
  113.             Case _Style.Classic
  114.                 Me.Size = New Size(70, 30)
  115.                 If Toggled Then
  116.                     If New Rectangle(Me.Width - 34, 7, 40, 20).Contains(e.X, e.Y) Then
  117.                         Cursor = Cursors.Hand
  118.                     Else
  119.                         Cursor = Cursors.Arrow
  120.                     End If
  121.                 Else
  122.                     If New Rectangle(8, 6, 40, 20).Contains(e.X, e.Y) Then
  123.                         Cursor = Cursors.Hand
  124.                     Else
  125.                         Cursor = Cursors.Arrow
  126.                     End If
  127.                 End If
  128.             Case _Style.RoundedClassic
  129.                 If Toggled Then
  130.                     If New Rectangle(5, 4, 32, 31).Contains(e.X, e.Y) Then
  131.                         Cursor = Cursors.Hand
  132.                     Else
  133.                         Cursor = Cursors.Arrow
  134.                     End If
  135.                 Else
  136.                     If New Rectangle(Me.Width - 37, 4, 32, 31).Contains(e.X, e.Y) Then
  137.                         Cursor = Cursors.Hand
  138.                     Else
  139.                         Cursor = Cursors.Arrow
  140.                     End If
  141.                 End If
  142.             Case _Style.Rounded
  143.                 If Toggled Then
  144.                     If New Rectangle(Me.Width - 37, 4, 32, 31).Contains(e.X, e.Y) Then
  145.                         Cursor = Cursors.Hand
  146.                     Else
  147.                         Cursor = Cursors.Arrow
  148.                     End If
  149.                 Else
  150.                     If New Rectangle(5, 4, 32, 31).Contains(e.X, e.Y) Then
  151.                         Cursor = Cursors.Hand
  152.                     Else
  153.                         Cursor = Cursors.Arrow
  154.                     End If
  155.                 End If
  156.             Case _Style.NightRounded
  157.                 If Toggled Then
  158.                     If New Rectangle(5, 4, 32, 31).Contains(e.X, e.Y) Then
  159.                         Cursor = Cursors.Hand
  160.                     Else
  161.                         Cursor = Cursors.Arrow
  162.                     End If
  163.                 Else
  164.                     If New Rectangle(Me.Width - 37, 4, 32, 31).Contains(e.X, e.Y) Then
  165.                         Cursor = Cursors.Hand
  166.                     Else
  167.                         Cursor = Cursors.Arrow
  168.                     End If
  169.                 End If
  170.         End Select
  171.     End Sub
  172.     Private Sub Switch_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
  173.         Dim g As Graphics = e.Graphics
  174.         g.SmoothingMode = SmoothingMode.HighQuality
  175.         Select Case StyleType
  176.             Case _Style.Classic
  177.                 Me.Size = New Size(70, 30)
  178.                 If Toggled Then
  179.                     g.FillPath(New SolidBrush(Color1), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 20))
  180.                     g.FillPath(New SolidBrush(Color5), Round(New Rectangle(4, 4, 30, 20), 20))
  181.                     g.DrawString("OFF", New Font("Arial", 10, FontStyle.Bold), New SolidBrush(Color5), New Rectangle(Me.Width - 34, 7, 40, 20))
  182.                 Else
  183.                     g.FillPath(New SolidBrush(Color2), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 20))
  184.                     g.FillPath(New SolidBrush(Color5), Round(New Rectangle(Me.Width - 34, 4, 30, 20), 20))
  185.                     g.DrawString("ON", New Font("Arial", 10, FontStyle.Bold), New SolidBrush(Color5), New Rectangle(8, 6, 40, 20))
  186.                 End If
  187.             Case _Style.RoundedClassic
  188.                 Me.Size = New Size(75, 40)
  189.                 If Toggled Then
  190.                     g.FillPath(New SolidBrush(Color1), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 40))
  191.                     g.FillEllipse(New SolidBrush(Color5), New Rectangle(5, 4, 32, 31))
  192.                     g.DrawString("NO", New Font("Arial", 10, FontStyle.Bold), New SolidBrush(Color5), New Rectangle(Me.Width - 34, 12, 40, 20))
  193.                 Else
  194.                     g.FillPath(New SolidBrush(Color2), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 40))
  195.                     g.FillEllipse(New SolidBrush(Color5), New Rectangle(Me.Width - 37, 4, 32, 31))
  196.                     g.DrawString("YES", New Font("Arial", 9, FontStyle.Bold), New SolidBrush(Color5), New Rectangle(8, 12, 40, 20))
  197.                 End If
  198.             Case _Style.Rounded
  199.                 Me.Size = New Size(75, 40)
  200.                 If Toggled Then
  201.                     g.FillPath(New SolidBrush(Color1), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 40))
  202.                     g.FillEllipse(New SolidBrush(Color5), New Rectangle(Me.Width - 37, 4, 32, 31))
  203.                     g.DrawLine(New Pen((Color4), 2), New Point(17, 14), New Point(30, 27))
  204.                     g.DrawLine(New Pen((Color4), 2), New Point(17, 27), New Point(30, 14))
  205.                 Else
  206.                     g.FillPath(New SolidBrush(Color2), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 40))
  207.                     g.FillEllipse(New SolidBrush(Color5), New Rectangle(5, 4, 32, 31))
  208.                     g.DrawLine(New Pen((Color4), 3), New Point(Me.Width - 13, 12), New Point(Me.Width - 25, 27))
  209.                     g.DrawLine(New Pen((Color4), 3), New Point(Me.Width - 25, 27), New Point(Me.Width - 30, 20))
  210.                 End If
  211.             Case _Style.NightRounded
  212.                 If Toggled Then
  213.                     g.FillPath(New SolidBrush(Color3), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 40))
  214.                     g.FillEllipse(New SolidBrush(Color1), New Rectangle(5, 4, 32, 31))
  215.                     g.DrawLine(New Pen((Color5), 2), New Point(Me.Width - 17, 14), New Point(Me.Width - 30, 27))
  216.                     g.DrawLine(New Pen((Color5), 2), New Point(Me.Width - 17, 27), New Point(Me.Width - 30, 14))
  217.                 Else
  218.                     g.FillPath(New SolidBrush(Color1), Round(New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 40))
  219.                     g.FillEllipse(New SolidBrush(Color2), New Rectangle(Me.Width - 37, 4, 32, 31))
  220.                     g.DrawLine(New Pen((Color4), 3), New Point(30, 10), New Point(18, 27))
  221.                     g.DrawLine(New Pen((Color4), 3), New Point(18, 27), New Point(13, 20))
  222.                 End If
  223.         End Select
  224.     End Sub
  225. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement