netrosly

Blink made this ;P - PhotoSlider

Dec 11th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Strict On
  2.  
  3. Imports System.Drawing.Drawing2D
  4. Public Class PhotoSlider
  5.     Inherits Control
  6.  
  7.     '########## Credits #########
  8.    '# GDI PhotoSlider          #
  9.    '# Created by: Blink        #
  10.    '# Released on: 27 dec 2012 #
  11.    '############################
  12.    'You can contact me at:
  13.    'http://www.hackforums.net/member.php?action=profile&uid=551863
  14.  
  15. #Region "Properties"
  16.     Public Property Images As ImageList
  17.  
  18.     Private _intArrowSize As Integer = 10
  19.     Public Property ArrowSize As Integer
  20.         Get
  21.             Return _intArrowSize
  22.         End Get
  23.         Set(ByVal value As Integer)
  24.             _intArrowSize = value
  25.             Invalidate()
  26.         End Set
  27.     End Property
  28.  
  29.     Private _intHoverOutset As Integer = 10
  30.     Public Property HoverOutset As Integer
  31.         Get
  32.             Return _intHoverOutset
  33.         End Get
  34.         Set(ByVal value As Integer)
  35.             _intHoverOutset = value
  36.             Invalidate()
  37.         End Set
  38.     End Property
  39.  
  40.     Private _intOverlap As Integer = 5
  41.     Public Property Overlap As Integer
  42.         Get
  43.             Return _intOverlap
  44.         End Get
  45.         Set(ByVal value As Integer)
  46.             _intOverlap = value
  47.             Invalidate()
  48.         End Set
  49.     End Property
  50.  
  51.     Private _intFocused As Integer = 0
  52.     Public Property FocusedIndex As Integer
  53.         Get
  54.             Return _intFocused
  55.         End Get
  56.         Set(ByVal value As Integer)
  57.             _intFocused = value
  58.             Invalidate()
  59.         End Set
  60.     End Property
  61.  
  62.     Private _blnDrawOutline As Boolean = True
  63.     Public Property DrawOutline As Boolean
  64.         Get
  65.             Return _blnDrawOutline
  66.         End Get
  67.         Set(ByVal value As Boolean)
  68.             _blnDrawOutline = value
  69.             Invalidate()
  70.         End Set
  71.     End Property
  72. #End Region
  73.  
  74.     Sub New()
  75.         Size = New Size(600, 150)
  76.         DoubleBuffered = True
  77.     End Sub
  78.  
  79.     Public Sub MoveForward()
  80.         If FocusedIndex + 1 > Images.Images.Count - 1 Then
  81.             FocusedIndex = 0
  82.         Else
  83.             FocusedIndex += 1
  84.         End If
  85.     End Sub
  86.  
  87.     Public Sub MoveBackward()
  88.         If FocusedIndex - 1 < 0 Then
  89.             FocusedIndex = Images.Images.Count - 1
  90.         Else
  91.             FocusedIndex -= 1
  92.         End If
  93.     End Sub
  94.  
  95.     'Gets index relative to the focused index
  96.    Private Function GetIndex(ByVal intOff As Integer) As Integer
  97.         If FocusedIndex + intOff = Images.Images.Count Then
  98.             Return 0
  99.         ElseIf FocusedIndex + intOff = Images.Images.Count + 1 Then
  100.             Return 1
  101.         ElseIf FocusedIndex + intOff = -1 Then
  102.             Return Images.Images.Count - 1
  103.         ElseIf FocusedIndex + intOff = -2 Then
  104.             Return Images.Images.Count - 2
  105.         Else
  106.             Return FocusedIndex + intOff
  107.         End If
  108.     End Function
  109.  
  110.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  111.         Dim G As Graphics = e.Graphics
  112.  
  113.         'Order is important for Overlap
  114.        For i As Integer = -2 To 0
  115.             If i <> HoveredIndex Then
  116.                 DrawPicture(G, i)
  117.             End If
  118.             If i <> Math.Abs(i) And Math.Abs(i) <> HoveredIndex Then
  119.                 DrawPicture(G, Math.Abs(i))
  120.             End If
  121.         Next
  122.  
  123.         'Draw hovered last for Overlap
  124.        If HoveredIndex <> -3 Then
  125.             DrawPicture(G, HoveredIndex)
  126.         End If
  127.  
  128.         DrawArrow(G, 0) 'Left
  129.        DrawArrow(G, 1) 'Right
  130.  
  131.         MyBase.OnPaint(e)
  132.     End Sub
  133.  
  134.     Sub DrawArrow(ByVal G As Graphics, ByVal intArrowIndex As Integer)
  135.         Dim rctBounds As Rectangle = GetArrowBounds(intArrowIndex)
  136.         Dim intHalf As Integer = CInt(ArrowSize / 2)
  137.         Dim intAlfa As Integer
  138.         If intArrowIndex = HoveredArrow And blnMouseIsDown Then
  139.             intAlfa = 255
  140.         ElseIf intArrowIndex = HoveredArrow Then
  141.             intAlfa = 200
  142.         Else
  143.             intAlfa = 150
  144.         End If
  145.         G.SmoothingMode = SmoothingMode.AntiAlias
  146.         Select Case intArrowIndex
  147.             Case 0 'Left
  148.                Dim gpLeftArrow As New GraphicsPath
  149.                 gpLeftArrow.AddLine(rctBounds.X + intHalf, rctBounds.Y + 0, rctBounds.X + ArrowSize, rctBounds.Y + 0)
  150.                 gpLeftArrow.AddLine(rctBounds.X + ArrowSize, rctBounds.Y + 0, rctBounds.X + intHalf, rctBounds.Y + intHalf)
  151.                 gpLeftArrow.AddLine(rctBounds.X + intHalf, rctBounds.Y + intHalf, rctBounds.X + ArrowSize, rctBounds.Y + ArrowSize)
  152.                 gpLeftArrow.AddLine(rctBounds.X + ArrowSize, rctBounds.Y + ArrowSize, rctBounds.X + intHalf, rctBounds.Y + ArrowSize)
  153.                 gpLeftArrow.AddLine(rctBounds.X + intHalf, rctBounds.Y + ArrowSize, rctBounds.X + 0, rctBounds.Y + intHalf)
  154.                 gpLeftArrow.AddLine(rctBounds.X + 0, rctBounds.Y + intHalf, rctBounds.X + intHalf, rctBounds.Y + 0)
  155.  
  156.                 G.FillPath(New SolidBrush(Color.FromArgb(intAlfa, 75, 75, 75)), gpLeftArrow)
  157.             Case 1 'Right
  158.                Dim gpRightArrow As New GraphicsPath
  159.                 gpRightArrow.AddLine(rctBounds.X + 0, rctBounds.Y + 0, rctBounds.X + intHalf, rctBounds.Y + 0)
  160.                 gpRightArrow.AddLine(rctBounds.X + intHalf, rctBounds.Y + 0, rctBounds.X + ArrowSize, rctBounds.Y + intHalf)
  161.                 gpRightArrow.AddLine(rctBounds.X + ArrowSize, rctBounds.Y + intHalf, rctBounds.X + intHalf, rctBounds.Y + ArrowSize)
  162.                 gpRightArrow.AddLine(rctBounds.X + intHalf, rctBounds.Y + ArrowSize, rctBounds.X + 0, rctBounds.Y + ArrowSize)
  163.                 gpRightArrow.AddLine(rctBounds.X + 0, rctBounds.Y + ArrowSize, rctBounds.X + intHalf, rctBounds.Y + intHalf)
  164.                 gpRightArrow.AddLine(rctBounds.X + intHalf, rctBounds.Y + intHalf, rctBounds.X + 0, rctBounds.Y + 0)
  165.  
  166.                 G.FillPath(New SolidBrush(Color.FromArgb(intAlfa, 75, 75, 75)), gpRightArrow)
  167.         End Select
  168.     End Sub
  169.  
  170.     Sub DrawPicture(ByVal G As Graphics, ByVal index As Integer)
  171.         G.FillRectangle(Brushes.White, GetPictureBounds(index))
  172.         Try
  173.             G.DrawImage(Images.Images(GetIndex(index)), GetPictureBounds(index))
  174.         Catch ex As Exception
  175.             G.FillRectangle(Brushes.CornflowerBlue, GetPictureBounds(index))
  176.         End Try
  177.         If DrawOutline Then G.DrawRectangle(Pens.Black, GetPictureBounds(index))
  178.     End Sub
  179.  
  180.     'Returns the bounds of the requested index, inclusive the hover
  181.    Private Function GetPictureBounds(ByVal Index As Integer) As Rectangle
  182.         If Not Index = HoveredIndex Then
  183.             Return GetNormalPictureBounds(Index)
  184.         Else
  185.             Dim rctNormal As Rectangle = GetNormalPictureBounds(Index)
  186.             Return New Rectangle(rctNormal.X - HoverOutset, rctNormal.Y - HoverOutset, rctNormal.Width + 2 * HoverOutset, rctNormal.Height + 2 * HoverOutset)
  187.         End If
  188.     End Function
  189.  
  190.     'Returns the bounds of the requested index, exclusive the hover
  191.    Private Function GetNormalPictureBounds(ByVal Index As Integer) As Rectangle
  192.         Select Case Index
  193.             Case -2
  194.                 Dim rctNext As Rectangle = GetNormalPictureBounds(Index + 1)
  195.                 Return New Rectangle(rctNext.X + (Overlap * 3) - rctNext.Width, rctNext.Y + Overlap, rctNext.Width - Overlap * 2, rctNext.Height - 2 * Overlap)
  196.             Case -1
  197.                 Dim rctNext As Rectangle = GetNormalPictureBounds(Index + 1)
  198.                 Return New Rectangle(rctNext.X + (Overlap * 3) - rctNext.Width, rctNext.Y + Overlap, rctNext.Width - Overlap * 2, rctNext.Height - 2 * Overlap)
  199.             Case 0
  200.                 Return New Rectangle(CInt(Width / 2) - CInt((Height - ArrowSize) / 2 - HoverOutset), HoverOutset, (Height - ArrowSize) - HoverOutset * 2, (Height - ArrowSize) - HoverOutset * 2 - 1)
  201.             Case 1
  202.                 Dim rctPrevious As Rectangle = GetNormalPictureBounds(Index - 1)
  203.                 Return New Rectangle(rctPrevious.X + rctPrevious.Width - Overlap, rctPrevious.Y + Overlap, rctPrevious.Width - Overlap * 2, rctPrevious.Height - Overlap * 2)
  204.             Case 2
  205.                 Dim rctPrevious As Rectangle = GetNormalPictureBounds(Index - 1)
  206.                 Return New Rectangle(rctPrevious.X + rctPrevious.Width - Overlap, rctPrevious.Y + Overlap, rctPrevious.Width - Overlap * 2, rctPrevious.Height - Overlap * 2)
  207.         End Select
  208.     End Function
  209.  
  210.     Private HoveredIndex As Integer = -3
  211.     Private HoveredArrow As Integer = -1
  212.     Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
  213.         Dim intNewHovered As Integer = GetHoveredIndex(e.Location)
  214.         If intNewHovered <> HoveredIndex Then
  215.             HoveredIndex = intNewHovered
  216.             Invalidate()
  217.         End If
  218.         Dim intNewHoveredArrow As Integer = GetHoveredArrowIndex(e.Location)
  219.         If intNewHoveredArrow <> HoveredArrow Then
  220.             HoveredArrow = intNewHoveredArrow
  221.             Invalidate()
  222.         End If
  223.         MyBase.OnMouseMove(e)
  224.     End Sub
  225.  
  226.     Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
  227.         HoveredIndex = -3
  228.         Invalidate()
  229.         MyBase.OnMouseLeave(e)
  230.     End Sub
  231.  
  232.     Private blnMouseIsDown As Boolean = False
  233.     Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
  234.         blnMouseIsDown = True
  235.         Invalidate()
  236.         MyBase.OnMouseDown(e)
  237.     End Sub
  238.  
  239.     Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
  240.         blnMouseIsDown = False
  241.         Dim intIndex As Integer = GetHoveredIndex(e.Location)
  242.         If intIndex <> -3 Then
  243.             FocusedIndex = GetIndex(intIndex)
  244.         End If
  245.         Dim intArrow As Integer = GetHoveredArrowIndex(e.Location)
  246.         If intArrow = 0 Then
  247.             MoveBackward()
  248.         ElseIf intArrow = 1 Then
  249.             MoveForward()
  250.         End If
  251.         MyBase.OnMouseUp(e)
  252.     End Sub
  253.  
  254.     Private Function GetHoveredIndex(ByVal pntLoc As Point) As Integer
  255.         For i As Integer = 0 To -2 Step -1
  256.             If GetPictureBounds(i).Contains(pntLoc) Then
  257.                 Return i
  258.             ElseIf GetPictureBounds(Math.Abs(i)).Contains(pntLoc) Then
  259.                 Return Math.Abs(i)
  260.             End If
  261.         Next
  262.         Return -3
  263.     End Function
  264.  
  265.     Private Function GetArrowBounds(ByVal intIndex As Integer) As Rectangle
  266.         Select Case intIndex
  267.             Case 0 'Left
  268.                Return New Rectangle(CInt(Width / 2) - ArrowSize - 1, Height - 1 - ArrowSize, ArrowSize, ArrowSize)
  269.             Case 1 'Right
  270.                Return New Rectangle(CInt(Width / 2) + 1, Height - 1 - ArrowSize, ArrowSize, ArrowSize)
  271.         End Select
  272.     End Function
  273.  
  274.     Private Function GetHoveredArrowIndex(ByVal pntLoc As Point) As Integer
  275.         For i As Integer = 0 To 1
  276.             If GetArrowBounds(i).Contains(pntLoc) Then
  277.                 Return i
  278.             End If
  279.         Next
  280.         Return -1
  281.     End Function
  282. End Class
Advertisement
Add Comment
Please, Sign In to add comment