Advertisement
Guest User

ElektroListView

a guest
Jul 23rd, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 37.14 KB | None | 0 0
  1.  
  2. '  /*                  *\
  3. ' |#* Elektro ListView *#|
  4. '  \*                  */
  5. '
  6. ' // By Elektro H@cker
  7. '
  8. ' -----------
  9. ' Properties:
  10. ' -----------
  11. '
  12. ' Double_Buffer
  13. ' Disable_Flickering
  14. ' ----------------
  15. ' UndoRedo_Enabled
  16. ' ----------------
  17. ' UseDefaultGridLines
  18. ' GridLineColor
  19. ' ----------------------
  20. ' ColumnHeader_Backcolor
  21. ' ColumnHeader_ForeColor
  22. ' ColumnHeader_Separator_Color
  23. ' ----------------------------
  24. ' ItemHighlightColor
  25. ' ItemNotFocusedHighlighColor
  26. ' ---------------------------
  27. ' ProgressBar_Enabled
  28. ' Progressbar_Column
  29. ' ProgressBar_BackColor
  30. ' ProgressBar_BorderColor
  31. ' ProgressBar_FillColor1
  32. ' ProgressBar_FillColor2
  33. ' ProgressPercent
  34. ' ProgressPercent_Decimal
  35. ' ProgressPercent_Text
  36. ' ProgressPercent_Text_Allignment
  37. ' ProgressPercent_Text_Trimming
  38. ' ProgressPercent_Font
  39. ' ProgressPercent_Forecolor
  40. '
  41. ' -------
  42. ' Events:
  43. ' -------
  44. '
  45. ' ItemAdded
  46. ' ItemRemoved
  47. ' UndoRedo_IsPerformed
  48. ' UndoRedo_StackSizeChanged
  49. '
  50. ' --------
  51. ' Methods:
  52. ' --------
  53. ' AddItem
  54. ' AddItems
  55. ' RemoveItem
  56. ' RemoveItems
  57. ' Undo
  58. ' Redo
  59.  
  60. Public Class ElektroListView : Inherits ListView
  61.  
  62. #Region " Members "
  63.  
  64. #Region " Variables "
  65.  
  66.     ''' <summary>
  67.     ''' Tries to avoid any flickering effect on this ListView.
  68.     ''' </summary>
  69.     Private _Disable_Flickering As Boolean = True
  70.  
  71. #End Region
  72.  
  73. #Region " Properties "
  74.  
  75.     ''' <summary>
  76.     ''' Set the Double Buffer.
  77.     ''' </summary>
  78.     Public Property Double_Buffer() As Boolean
  79.         Get
  80.             Return Me.DoubleBuffered
  81.         End Get
  82.         Set(ByVal Value As Boolean)
  83.             Me.DoubleBuffered = Value
  84.         End Set
  85.     End Property
  86.  
  87.     ''' <summary>
  88.     ''' Tries to avoid any flickering effect on this ListView.
  89.     '''
  90.     ''' This property turns off any Flicker effect on the ListView
  91.     ''' ...but also reduces the performance (speed) of the ListView about 30% slower.
  92.     ''' This don't affect to the performance of the application itself, only to the performance of this control.
  93.     ''' </summary>
  94.     Public Property Disable_Flickering() As Boolean
  95.         Get
  96.             Return _Disable_Flickering
  97.         End Get
  98.         Set(ByVal Value As Boolean)
  99.             Me._Disable_Flickering = Value
  100.         End Set
  101.     End Property
  102.  
  103.     ''' <summary>
  104.     ''' Enable/Disable any flickering effect on the ListView.
  105.     ''' </summary>
  106.     Protected Overrides ReadOnly Property CreateParams() As CreateParams
  107.         Get
  108.             If _Disable_Flickering Then
  109.                 Dim cp As CreateParams = MyBase.CreateParams
  110.                 cp.ExStyle = cp.ExStyle Or &H2000000
  111.                 Return cp
  112.             Else
  113.                 Return MyBase.CreateParams
  114.             End If
  115.         End Get
  116.     End Property
  117.  
  118. #End Region
  119.  
  120. #Region " Events "
  121.  
  122.     ''' <summary>
  123.     ''' Event is raised when an Item is added into the ListView.
  124.     ''' </summary>
  125.     Public Event ItemAdded As EventHandler(Of ItemAddedEventArgs)
  126.     Public Class ItemAddedEventArgs : Inherits EventArgs
  127.         Public Property Item As ListViewItem
  128.     End Class
  129.  
  130.     ''' <summary>
  131.     ''' Event is raised when an Item is removed from the ListView.
  132.     ''' </summary>
  133.     Public Event ItemRemoved As EventHandler(Of ItemRemovedEventArgs)
  134.     Public Class ItemRemovedEventArgs : Inherits EventArgs
  135.         Public Property Item As ListViewItem
  136.     End Class
  137.  
  138. #End Region
  139.  
  140. #End Region
  141.  
  142. #Region " Constructor "
  143.  
  144.     Public Sub New()
  145.  
  146.         Me.Name = "ElektroListView"
  147.         Me.DoubleBuffered = True
  148.         Me.UseDefaultGridLines = True
  149.  
  150.         Me.GridLines = True
  151.         Me.FullRowSelect = True
  152.         Me.MultiSelect = True
  153.         Me.View = View.Details
  154.  
  155.         ' Set Listview OwnerDraw to True, so we can draw the items and/or the progressbar inside.
  156.         Me.OwnerDraw = True
  157.  
  158.     End Sub
  159.  
  160. #End Region
  161.  
  162. #Region " Public Methods "
  163.  
  164.     ''' <summary>
  165.     ''' Adds an Item to the ListView,
  166.     ''' to monitor when an Item is added to the ListView.
  167.     ''' </summary>
  168.     Public Function AddItem(ByVal Item As ListViewItem) As ListViewItem
  169.         Me.Items.Add(Item)
  170.         RaiseEvent ItemAdded(Me, New ItemAddedEventArgs With {.Item = Item})
  171.         Return Item
  172.     End Function
  173.  
  174.     ''' <summary>
  175.     ''' Adds an Item to the ListView,
  176.     ''' to monitor when an Item is added to the ListView.
  177.     ''' </summary>
  178.     Public Function AddItem(ByVal Text As String) As ListViewItem
  179.         Dim NewItem As New ListViewItem(Text)
  180.         Me.Items.Add(NewItem)
  181.         RaiseEvent ItemAdded(Me, New ItemAddedEventArgs With {.Item = NewItem})
  182.         Return NewItem
  183.     End Function
  184.  
  185.     ''' <summary>
  186.     ''' Removes an Item from the ListView
  187.     ''' to monitor when an Item is removed from the ListView.
  188.     ''' </summary>
  189.     Public Sub RemoveItem(ByVal Item As ListViewItem)
  190.         Me.Items.Remove(Item)
  191.         RaiseEvent ItemRemoved(Me, New ItemRemovedEventArgs With {.Item = Item})
  192.     End Sub
  193.  
  194.     ''' <summary>
  195.     ''' Removes an Item from the ListView at given Index
  196.     ''' to monitor when an Item is removed from the ListView.
  197.     ''' </summary>
  198.     Public Sub RemoveItem_At(ByVal Index As Integer)
  199.         RemoveItem(Me.Items.Item(Index))
  200.     End Sub
  201.  
  202.     ''' <summary>
  203.     ''' Removes an Item from the ListView at given Index
  204.     ''' to monitor when an Item is removed from the ListView.
  205.     ''' </summary>
  206.     Public Sub RemoveItems_At(ByVal Indexes As Integer())
  207.         Array.Sort(Indexes)
  208.         Array.Reverse(Indexes)
  209.         For Each Index As Integer In Indexes
  210.             RemoveItem(Me.Items.Item(Index))
  211.         Next
  212.     End Sub
  213.  
  214.     ''' <summary>
  215.     ''' Adds a range of Items to the ListView,
  216.     ''' to monitor when an Item is added to the ListView.
  217.     ''' </summary>
  218.     Public Sub AddItems(ByVal Items As ListViewItem())
  219.         For Each item As ListViewItem In Items
  220.             AddItem(item)
  221.         Next
  222.     End Sub
  223.  
  224.     ''' <summary>
  225.     ''' Adds a range of Items to the ListView,
  226.     ''' to monitor when an Item is added to the ListView.
  227.     ''' </summary>
  228.     Public Sub AddItems(ByVal Items As ListViewItemCollection)
  229.         For Each item As ListViewItem In Items
  230.             AddItem(item)
  231.         Next
  232.     End Sub
  233.  
  234.     ''' <summary>
  235.     ''' Removes a range of Items from the ListView
  236.     ''' to monitor when an Item is removed from the ListView.
  237.     ''' </summary>
  238.     Public Sub RemoveItems(ByVal Items As ListViewItem())
  239.         For Each item As ListViewItem In Items
  240.             RemoveItem(item)
  241.         Next
  242.     End Sub
  243.  
  244.     ''' <summary>
  245.     ''' Removes a range of Items from the ListView
  246.     ''' to monitor when an Item is removed from the ListView.
  247.     ''' </summary>
  248.     Public Sub RemoveItems(ByVal Items As ListViewItemCollection)
  249.         For Each item As ListViewItem In Items
  250.             RemoveItem(item)
  251.         Next
  252.     End Sub
  253.  
  254.     ''' <summary>
  255.     ''' Removes a range of Items from the ListView
  256.     ''' to monitor when an Item is removed from the ListView.
  257.     ''' </summary>
  258.     Public Sub RemoveItems(ByVal Items As SelectedListViewItemCollection)
  259.         For Each item As ListViewItem In Items
  260.             RemoveItem(item)
  261.         Next
  262.     End Sub
  263.  
  264. #End Region
  265.  
  266. #Region " Features "
  267.  
  268. #Region " Column/Item/Grid Paintings "
  269.  
  270. #Region " Members "
  271.  
  272. #Region " Variables "
  273.  
  274.     ''' <summary>
  275.     ''' Indicates the Backcolor of the Column headers.
  276.     ''' </summary>
  277.     Private _columnheader_backcolor As SolidBrush = New SolidBrush(Color.FromArgb(51, 50, 49))
  278.  
  279.     ''' <summary>
  280.     ''' Indicates the ForeColor of the Column headers.
  281.     ''' </summary>
  282.     Private _columnheader_forecolor As SolidBrush = New SolidBrush(Color.Silver)
  283.  
  284.     ''' <summary>
  285.     ''' Indicates the ForeColor of the Column headers separators.
  286.     ''' </summary>
  287.     Private _columnheader_separator_color As Pen = SystemPens.ControlLightLight
  288.  
  289.     ''' <summary>
  290.     ''' Indicates the color to use to highlight the items.
  291.     ''' </summary>
  292.     Private _itemHighlightColor As Color = Color.FromKnownColor(KnownColor.Highlight)
  293.  
  294.     ''' <summary>
  295.     ''' Indicates the color to use when the item is not focused.
  296.     ''' </summary>
  297.     Private _itemNotFocusedHighlighColor As Color = Color.FromKnownColor(KnownColor.MenuBar)
  298.  
  299.     ''' <summary>
  300.     ''' Indicates whether the ListView should use GridLines.
  301.     ''' </summary>
  302.     Private _gridLines As Boolean = False
  303.  
  304.     ''' <summary>
  305.     ''' Indicates whether the ListView should draw default gridlines.
  306.     ''' </summary>
  307.     Private _useDefaultGridLines As Boolean = False
  308.  
  309.     ''' <summary>
  310.     ''' Indicates the GridLines color.
  311.     ''' </summary>
  312.     Private _gridLineColor As Color = Color.Black
  313.  
  314. #End Region
  315.  
  316. #Region " Properties "
  317.  
  318.     ''' <summary>
  319.     ''' Indicates the Backcolor of the Column headers.
  320.     ''' </summary>
  321.     Public Property ColumnHeader_Backcolor As Color
  322.         Get
  323.             Return _columnheader_backcolor.Color
  324.         End Get
  325.         Set(ByVal value As Color)
  326.             If Not _columnheader_backcolor.Color = value Then
  327.                 _columnheader_backcolor = New SolidBrush(value)
  328.                 Me.Invalidate(False)
  329.             End If
  330.         End Set
  331.     End Property
  332.  
  333.     ''' <summary>
  334.     ''' Indicates the ForeColor of the Column headers.
  335.     ''' </summary>
  336.     Public Property ColumnHeader_ForeColor As Color
  337.         Get
  338.             Return _columnheader_forecolor.Color
  339.         End Get
  340.         Set(ByVal value As Color)
  341.             If Not _columnheader_forecolor.Color = value Then
  342.                 _columnheader_forecolor = New SolidBrush(value)
  343.                 Me.Invalidate(False)
  344.             End If
  345.         End Set
  346.     End Property
  347.  
  348.     ''' <summary>
  349.     ''' Indicates the ForeColor of the Column headers separators.
  350.     ''' </summary>
  351.     Public Property ColumnHeader_Separator_Color As Color
  352.         Get
  353.             Return _columnheader_separator_color.Color
  354.         End Get
  355.         Set(ByVal value As Color)
  356.             If Not _columnheader_separator_color.Color = value Then
  357.                 _columnheader_separator_color = New Pen(value)
  358.                 Me.Invalidate(False)
  359.             End If
  360.         End Set
  361.     End Property
  362.  
  363.     ''' <summary>
  364.     ''' Indicates the color to use to highlight the items.
  365.     ''' </summary>
  366.     Public Property ItemHighlightColor() As Color
  367.         Get
  368.             Return _itemHighlightColor
  369.         End Get
  370.         Set(ByVal value As Color)
  371.             If Not _itemHighlightColor = value Then
  372.                 _itemHighlightColor = value
  373.                 Me.Invalidate(False)
  374.             End If
  375.         End Set
  376.     End Property
  377.  
  378.     ''' <summary>
  379.     ''' Indicates the color to use when the item is not focused.
  380.     ''' </summary>
  381.     Public Property ItemNotFocusedHighlighColor() As Color
  382.         Get
  383.             Return _itemNotFocusedHighlighColor
  384.         End Get
  385.         Set(ByVal value As Color)
  386.             If Not _itemNotFocusedHighlighColor = value Then
  387.                 _itemNotFocusedHighlighColor = value
  388.                 Me.Invalidate(False)
  389.             End If
  390.         End Set
  391.     End Property
  392.  
  393.     ''' <summary>
  394.     ''' Indicates whether the ListView should use GridLines.
  395.     ''' </summary>
  396.     Public Shadows Property GridLines() As Boolean
  397.         Get
  398.             Return _gridLines
  399.         End Get
  400.         Set(ByVal value As Boolean)
  401.             _gridLines = value
  402.         End Set
  403.     End Property
  404.  
  405.     ''' <summary>
  406.     ''' Indicates whether the ListView should draw default gridlines.
  407.     ''' </summary>
  408.     Public Property UseDefaultGridLines() As Boolean
  409.         Get
  410.             Return _useDefaultGridLines
  411.         End Get
  412.         Set(ByVal value As Boolean)
  413.             If Not _useDefaultGridLines = value Then
  414.                 _useDefaultGridLines = value
  415.                 Me.Invalidate(False)
  416.             End If
  417.             MyBase.GridLines = value
  418.         End Set
  419.     End Property
  420.  
  421.     ''' <summary>
  422.     ''' Indicates the GridLines color.
  423.     ''' </summary>
  424.     Public Property GridLineColor() As Color
  425.         Get
  426.             Return _gridLineColor
  427.         End Get
  428.         Set(ByVal value As Color)
  429.             If Not _gridLineColor = value Then
  430.                 _gridLineColor = value
  431.                 If _gridLines Then
  432.                     Me.Invalidate(False)
  433.                 End If
  434.             End If
  435.         End Set
  436.     End Property
  437.  
  438. #End Region
  439.  
  440. #End Region
  441.  
  442. #Region " Event Handlers "
  443.  
  444.     ' Me [DrawColumnHeader]
  445.     Public Sub Me_DrawColumnHeader(ByVal sender As Object, ByVal e As DrawListViewColumnHeaderEventArgs) _
  446.     Handles Me.DrawColumnHeader
  447.  
  448.         e.DrawDefault = False  ' Set ownerdraw.
  449.  
  450.         'BACKGROUND
  451.         Using brush As Brush = New SolidBrush(Me.ColumnHeader_Backcolor)
  452.             e.Graphics.FillRectangle(brush, e.Bounds)
  453.         End Using
  454.  
  455.         Dim bounds As Rectangle = e.Bounds
  456.  
  457.         bounds.Width -= 1
  458.         bounds.Height -= 1
  459.  
  460.         e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, bounds)
  461.  
  462.         ' bounds.Width -= 1
  463.         ' bounds.Height -= 1
  464.  
  465.         'e.Graphics.DrawLine(SystemPens.ControlLightLight, bounds.X, bounds.Y, bounds.Right, bounds.Y)
  466.         'e.Graphics.DrawLine(SystemPens.ControlLightLight, bounds.X, bounds.Y, bounds.X, bounds.Bottom)
  467.         ' e.Graphics.DrawLine(SystemPens.ControlDark, (bounds.X + 1), bounds.Bottom, bounds.Right, bounds.Bottom)
  468.         e.Graphics.DrawLine(_columnheader_separator_color, bounds.Right, (bounds.Y), bounds.Right, bounds.Bottom)
  469.  
  470.         'TEXT
  471.         Dim textAlign As HorizontalAlignment = e.Header.TextAlign
  472.         Dim flags As TextFormatFlags = If((textAlign = HorizontalAlignment.Left), TextFormatFlags.GlyphOverhangPadding, If((textAlign = HorizontalAlignment.Center), TextFormatFlags.HorizontalCenter, TextFormatFlags.Right))
  473.  
  474.         flags = (flags Or TextFormatFlags.VerticalCenter)
  475.  
  476.         Dim text As String = e.Header.Text
  477.         Dim width As Integer = TextRenderer.MeasureText(" ", e.Font).Width - 8
  478.         bounds = Rectangle.Inflate(e.Bounds, -width, 0)
  479.         TextRenderer.DrawText(e.Graphics, [text], e.Font, bounds, Me.ColumnHeader_ForeColor, flags)
  480.  
  481.     End Sub
  482.  
  483.     ' Me [DrawItem]
  484.     Public Sub Me_DrawItem(ByVal sender As Object, ByVal e As DrawListViewItemEventArgs) _
  485.     Handles Me.DrawItem
  486.  
  487.         e.DrawDefault = False ' Set OwnerDraw.
  488.  
  489.     End Sub
  490.  
  491.     Protected Overrides Sub OnDrawColumnHeader(ByVal e As DrawListViewColumnHeaderEventArgs)
  492.         e.DrawDefault = True
  493.         MyBase.OnDrawColumnHeader(e)
  494.     End Sub
  495.  
  496.     Protected Overrides Sub OnLostFocus(ByVal e As EventArgs)
  497.         For Each selectedIndex As Integer In MyBase.SelectedIndices
  498.             MyBase.RedrawItems(selectedIndex, selectedIndex, False)
  499.         Next
  500.         MyBase.OnLostFocus(e)
  501.     End Sub
  502.  
  503.     Protected Overrides Sub OnDrawSubItem(ByVal e As DrawListViewSubItemEventArgs)
  504.  
  505.         Dim drawAsDefault As Boolean = False
  506.         Dim highlightBounds As Rectangle = Nothing
  507.         Dim highlightBrush As SolidBrush = Nothing
  508.  
  509.         'FIRST DETERMINE THE COLOR
  510.         If e.Item.Selected Then
  511.             If MyBase.Focused Then
  512.                 highlightBrush = New SolidBrush(_itemHighlightColor)
  513.             ElseIf HideSelection Then
  514.                 drawAsDefault = True
  515.             Else
  516.                 highlightBrush = New SolidBrush(_itemNotFocusedHighlighColor)
  517.             End If
  518.         Else
  519.             drawAsDefault = True
  520.         End If
  521.  
  522.         If drawAsDefault Then
  523.             e.DrawBackground()
  524.         Else
  525.  
  526.             'NEXT DETERMINE THE BOUNDS IN WHICH TO DRAW THE BACKGROUND
  527.             If FullRowSelect Then
  528.                 highlightBounds = e.Bounds
  529.             Else
  530.                 highlightBounds = e.Item.GetBounds(ItemBoundsPortion.Label)
  531.             End If
  532.  
  533.             'ONLY DRAW HIGHLIGHT IN 1 OF 2 CASES
  534.             'CASE 1 - FULL ROW SELECT (AND DRAWING ANY ITEM)
  535.             'CASE 2 - NOT FULL ROW SELECT (AND DRAWING 1ST ITEM)
  536.             If FullRowSelect Then
  537.                 e.Graphics.FillRectangle(highlightBrush, highlightBounds)
  538.             ElseIf e.ColumnIndex = 0 Then
  539.                 e.Graphics.FillRectangle(highlightBrush, highlightBounds)
  540.             Else
  541.                 e.DrawBackground()
  542.             End If
  543.         End If
  544.  
  545.         Dim bounds As Rectangle = e.Bounds
  546.         bounds.X += 5
  547.         bounds.Width -= 5
  548.  
  549.         Select Case e.Header.TextAlign
  550.  
  551.             Case HorizontalAlignment.Left
  552.  
  553.                 TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font,
  554.                                       bounds, e.SubItem.ForeColor,
  555.                                       TextFormatFlags.Left Or _
  556.                                       TextFormatFlags.EndEllipsis)
  557.  
  558.             Case HorizontalAlignment.Center
  559.  
  560.                 TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font,
  561.                                       bounds, e.SubItem.ForeColor,
  562.                                       TextFormatFlags.GlyphOverhangPadding Or _
  563.                                       TextFormatFlags.HorizontalCenter Or _
  564.                                       TextFormatFlags.EndEllipsis)
  565.  
  566.             Case HorizontalAlignment.Right
  567.                 TextRenderer.DrawText(e.Graphics, e.SubItem.Text, e.SubItem.Font,
  568.                                       bounds, e.SubItem.ForeColor,
  569.                                       TextFormatFlags.Right Or _
  570.                                       TextFormatFlags.EndEllipsis)
  571.  
  572.         End Select
  573.  
  574.         If _gridLines Then
  575.             e.Graphics.DrawRectangle(New Pen(_gridLineColor), e.Bounds)
  576.         End If
  577.  
  578.         If FullRowSelect Then
  579.             e.DrawFocusRectangle(e.Item.GetBounds(ItemBoundsPortion.Entire))
  580.         Else
  581.             e.DrawFocusRectangle(e.Item.GetBounds(ItemBoundsPortion.Label))
  582.         End If
  583.  
  584.         MyBase.OnDrawSubItem(e)
  585.  
  586.     End Sub
  587.  
  588. #End Region
  589.  
  590. #End Region
  591.  
  592. #Region " ProgressBar "
  593.  
  594. #Region " Members "
  595.  
  596. #Region " Variables "
  597.  
  598.     ''' <summary>
  599.     ''' Enable or disable the drawing of the ProgressBar.
  600.     ''' This property should be "True" to use any of the ProgressBar features.
  601.     ''' </summary>
  602.     Private _ProgressBar_Enabled As Boolean = False
  603.  
  604.     ''' <summary>
  605.     ''' Indicates the column index to draw the ProgressBar.
  606.     ''' </summary>
  607.     Private _Progressbar_Column As Integer = Nothing
  608.  
  609.     ''' <summary>
  610.     ''' Indicates the ProgressBar BackColor.
  611.     ''' </summary>
  612.     Private _ProgressBar_BackColor As SolidBrush = New SolidBrush(Color.Red)
  613.  
  614.     ''' <summary>
  615.     ''' The ProgressBar BorderColor.
  616.     ''' </summary>
  617.     Private _ProgressBar_BorderColor As Pen = New Pen(Color.LightGray)
  618.  
  619.     ''' <summary>
  620.     ''' Indicates the first ProgressBar gradient color.
  621.     ''' </summary>
  622.     Private _ProgressBar_FillColor1 As Color = Color.YellowGreen
  623.  
  624.     ''' <summary>
  625.     ''' Indicates the last ProgressBar gradient color.
  626.     ''' </summary>
  627.     Private _ProgressBar_FillColor2 As Color = Color.White
  628.  
  629.     ''' <summary>
  630.     ''' Indicates the ProgressBar Percent value.
  631.     ''' </summary>
  632.     Private _ProgressPercent As Double = 0
  633.  
  634.     ''' <summary>
  635.     ''' Indicates the ProgressBar Percent Value decimal factor.
  636.     ''' </summary>
  637.     Private _ProgressPercent_Decimal As Short = 2
  638.  
  639.     ''' <summary>
  640.     ''' Indicates the ProgressBar Percent text.
  641.     ''' </summary>
  642.     Private _ProgressPercent_Text As String = "%"
  643.  
  644.     ''' <summary>
  645.     ''' Indicates the ProgressBar Percent Text Allignment.
  646.     ''' </summary>
  647.     Private _ProgressPercent_Text_Allignment As StringAlignment = StringAlignment.Center
  648.  
  649.     ''' <summary>
  650.     ''' Indicates the ProgressBar Percent Text Trimming.
  651.     ''' </summary>
  652.     Private _ProgressPercent_Text_Trimming As StringTrimming = StringTrimming.EllipsisCharacter
  653.  
  654.     ''' <summary>
  655.     ''' Indicates the ProgressBar Percent Text StringFormat.
  656.     ''' </summary>
  657.     Private _ProgressPercent_StringFormat As StringFormat = New StringFormat With
  658.             {
  659.                 .Alignment = _ProgressPercent_Text_Allignment,
  660.                 .Trimming = _ProgressPercent_Text_Trimming
  661.             }
  662.  
  663.     ''' <summary>
  664.     ''' Indicates the ProgressBar Percent Text Font.
  665.     ''' </summary>
  666.     Private _ProgressPercent_Font As Font = Me.Font
  667.  
  668.     ''' <summary>
  669.     ''' Indicates the ProgressBar Percent Text ForeColor.
  670.     ''' </summary>
  671.     Private _ProgressPercent_ForeColor As SolidBrush = New SolidBrush(Color.Black)
  672.  
  673. #End Region
  674.  
  675. #Region " Properties "
  676.  
  677.     ''' <summary>
  678.     ''' Enable or disable the drawing of the ProgressBar.
  679.     ''' This property should be "True" to use any of the ProgressBar features.
  680.     ''' </summary>
  681.     Public Property ProgressBar_Enabled As Boolean
  682.         Get
  683.             Return _ProgressBar_Enabled
  684.         End Get
  685.         Set(ByVal value As Boolean)
  686.             If Not _ProgressBar_Enabled = value Then
  687.                 _ProgressBar_Enabled = value
  688.                 Me.Invalidate(False)
  689.             End If
  690.         End Set
  691.     End Property
  692.  
  693.     ''' <summary>
  694.     ''' Indicates the column index to draw the ProgressBar.
  695.     ''' </summary>
  696.     Public Property Progressbar_Column As Integer
  697.         Get
  698.             Return _Progressbar_Column
  699.         End Get
  700.         Set(ByVal value As Integer)
  701.             If Not _Progressbar_Column = value Then
  702.                 _Progressbar_Column = value
  703.                 Me.Invalidate(False)
  704.             End If
  705.         End Set
  706.     End Property
  707.  
  708.     ''' <summary>
  709.     ''' Indicates the ProgressBar BackColor.
  710.     ''' </summary>
  711.     Public Property ProgressBar_BackColor As Color
  712.         Get
  713.             Return _ProgressBar_BackColor.Color
  714.         End Get
  715.         Set(ByVal value As Color)
  716.             If Not _ProgressBar_BackColor.Color = value Then
  717.                 _ProgressBar_BackColor = New SolidBrush(value)
  718.                 Me.Invalidate(False)
  719.             End If
  720.         End Set
  721.     End Property
  722.  
  723.     ''' <summary>
  724.     ''' The ProgressBar BorderColor.
  725.     ''' </summary>
  726.     Public Property ProgressBar_BorderColor As Color
  727.         Get
  728.             Return _ProgressBar_BorderColor.Color
  729.         End Get
  730.         Set(ByVal value As Color)
  731.             If Not _ProgressBar_BorderColor.Color = value Then
  732.                 _ProgressBar_BorderColor = New Pen(value)
  733.                 Me.Invalidate(False)
  734.             End If
  735.         End Set
  736.     End Property
  737.  
  738.     ''' <summary>
  739.     ''' Indicates the first ProgressBar gradient color.
  740.     ''' </summary>
  741.     Public Property ProgressBar_FillColor1 As Color
  742.         Get
  743.             Return _ProgressBar_FillColor1
  744.         End Get
  745.         Set(ByVal value As Color)
  746.             If Not _ProgressBar_FillColor1 = value Then
  747.                 _ProgressBar_FillColor1 = value
  748.                 Me.Invalidate(False)
  749.             End If
  750.         End Set
  751.     End Property
  752.  
  753.     ''' <summary>
  754.     ''' Indicates the last ProgressBar gradient color.
  755.     ''' </summary>
  756.     Public Property ProgressBar_FillColor2 As Color
  757.         Get
  758.             Return _ProgressBar_FillColor2
  759.         End Get
  760.         Set(ByVal value As Color)
  761.             If Not _ProgressBar_FillColor2 = value Then
  762.                 _ProgressBar_FillColor2 = value
  763.                 Me.Invalidate(False)
  764.             End If
  765.         End Set
  766.     End Property
  767.  
  768.     ''' <summary>
  769.     ''' Indicates the current ProgressBar Percent value.
  770.     ''' </summary>
  771.     Public Property ProgressPercent As Double
  772.         Get
  773.             Return _ProgressPercent
  774.         End Get
  775.         Set(ByVal value As Double)
  776.             If Not _ProgressPercent = value Then
  777.                 _ProgressPercent = value
  778.                 Me.Invalidate(False)
  779.             End If
  780.         End Set
  781.     End Property
  782.  
  783.     ''' <summary>
  784.     ''' Indicates the ProgressBar Percent Value decimal factor.
  785.     ''' </summary>
  786.     Public Property ProgressPercent_Decimal As Short
  787.         Get
  788.             Return _ProgressPercent_Decimal
  789.         End Get
  790.         Set(ByVal value As Short)
  791.             If Not _ProgressPercent_Decimal = value Then
  792.                 _ProgressPercent_Decimal = value
  793.                 Me.Invalidate(False)
  794.             End If
  795.         End Set
  796.     End Property
  797.  
  798.     ''' <summary>
  799.     ''' Indicates the ProgressBar Percent text.
  800.     ''' </summary>
  801.     Public Property ProgressPercent_Text As String
  802.         Get
  803.             Return _ProgressPercent_Text
  804.         End Get
  805.         Set(ByVal value As String)
  806.             If Not _ProgressPercent_Text = value Then
  807.                 _ProgressPercent_Text = value
  808.                 Me.Invalidate(False)
  809.             End If
  810.         End Set
  811.     End Property
  812.  
  813.     ''' <summary>
  814.     ''' Indicates the ProgressBar Percent Text Allignment.
  815.     ''' </summary>
  816.     Public Property ProgressPercent_Text_Allignment As StringAlignment
  817.         Get
  818.             Return _ProgressPercent_Text_Allignment
  819.         End Get
  820.         Set(ByVal value As StringAlignment)
  821.             If Not _ProgressPercent_StringFormat.Alignment = value Then
  822.                 _ProgressPercent_StringFormat.Alignment = value
  823.                 _ProgressPercent_Text_Allignment = value
  824.                 Me.Invalidate(False)
  825.             End If
  826.         End Set
  827.     End Property
  828.  
  829.     ''' <summary>
  830.     ''' Indicates the ProgressBar Percent Text Trimming.
  831.     ''' </summary>
  832.     Public Property ProgressPercent_Text_Trimming As StringTrimming
  833.         Get
  834.             Return _ProgressPercent_Text_Trimming
  835.         End Get
  836.         Set(ByVal value As StringTrimming)
  837.             If Not _ProgressPercent_StringFormat.Trimming = value Then
  838.                 _ProgressPercent_StringFormat.Trimming = value
  839.                 _ProgressPercent_Text_Trimming = value
  840.                 Me.Invalidate(False)
  841.             End If
  842.         End Set
  843.     End Property
  844.  
  845.     ''' <summary>
  846.     ''' Indicates the ProgressBar Percent Text Font.
  847.     ''' </summary>
  848.     Public Property ProgressPercent_Font As Font
  849.         Get
  850.             Return _ProgressPercent_Font
  851.         End Get
  852.         Set(ByVal value As Font)
  853.             If Not _ProgressPercent_Font.Equals(value) Then
  854.                 _ProgressPercent_Font = value
  855.                 Me.Invalidate(False)
  856.             End If
  857.         End Set
  858.     End Property
  859.  
  860.     ''' <summary>
  861.     ''' Indicates the ProgressBar Percent Text ForeColor.
  862.     ''' </summary>
  863.     Public Property ProgressPercent_Forecolor As Color
  864.         Get
  865.             Return _ProgressPercent_ForeColor.Color
  866.         End Get
  867.         Set(ByVal value As Color)
  868.             If Not _ProgressPercent_ForeColor.Color = value Then
  869.                 _ProgressPercent_ForeColor = New SolidBrush(value)
  870.                 Me.Invalidate(False)
  871.             End If
  872.         End Set
  873.     End Property
  874.  
  875. #End Region
  876.  
  877. #End Region
  878.  
  879. #Region " Event Handlers "
  880.  
  881.     ' ListView [DrawSubItem]
  882.     Public Sub Me_DrawSubItem(ByVal sender As Object, ByVal e As DrawListViewSubItemEventArgs) _
  883.     Handles Me.DrawSubItem
  884.  
  885.         If Not ProgressBar_Enabled OrElse Progressbar_Column = Nothing Then
  886.             Exit Sub
  887.         End If
  888.  
  889.         ' Item is highlighted.
  890.         'If (e.ItemState And ListViewItemStates.Selected) <> 0 Then
  891.         '    e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
  892.         'End If
  893.  
  894.         ' Draw the progressbar.
  895.         If e.ColumnIndex = Progressbar_Column Then
  896.  
  897.             ' Background color of the progressbar.
  898.             e.Graphics.FillRectangle(_ProgressBar_BackColor, e.Bounds)
  899.  
  900.             ' Gradient to fill the progressbar.
  901.             Dim brGradient As Brush = _
  902.                 New Drawing2D.LinearGradientBrush(New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), _
  903.                                                                  _ProgressBar_FillColor1, _ProgressBar_FillColor2, 270, True)
  904.             ' Draw the actual progressbar.
  905.             e.Graphics.FillRectangle(brGradient, _
  906.                                      e.Bounds.X + 1, e.Bounds.Y + 2, _
  907.                                      CInt(((_ProgressPercent) / 100) * (e.Bounds.Width - 2)), e.Bounds.Height - 3)
  908.  
  909.             ' Draw the percentage number and percent sign.
  910.             e.Graphics.DrawString(_ProgressPercent.ToString("n" & _ProgressPercent_Decimal) & _ProgressPercent_Text, _
  911.                                   _ProgressPercent_Font, _ProgressPercent_ForeColor, _
  912.                                   CSng(e.Bounds.X + (e.Bounds.Width / 2)), e.Bounds.Y + 3, _
  913.                                   _ProgressPercent_StringFormat)
  914.  
  915.             ' Draw a light gray rectangle/border around the progressbar.
  916.             e.Graphics.DrawRectangle(_ProgressBar_BorderColor, _
  917.                                      e.Bounds.X, e.Bounds.Y + 1, _
  918.                                      e.Bounds.Width - 1, e.Bounds.Height - 2)
  919.         Else
  920.  
  921.             ' e.DrawDefault = True
  922.  
  923.         End If
  924.  
  925.     End Sub
  926.  
  927. #End Region
  928.  
  929. #End Region
  930.  
  931. #Region " Undo/Redo Manager "
  932.  
  933. #Region " Members "
  934.  
  935. #Region " Properties "
  936.  
  937.     ''' <summary>
  938.     ''' Enable or disble the Undo/Redo Manager system.
  939.     ''' </summary>
  940.     Public Property UndoRedo_Enabled As Boolean = False
  941.  
  942. #End Region
  943.  
  944. #Region " Delegates "
  945.  
  946.     ''' <summary>
  947.     ''' Delegate to Add an Item for Undo/Redo operations.
  948.     ''' </summary>
  949.     Private Delegate Sub AddDelegate(item As ListViewItem)
  950.  
  951.     ''' <summary>
  952.     ''' Delegate to Remove an Item for Undo/Redo operations.
  953.     ''' </summary>
  954.     Private Delegate Sub RemoveDelegate(item As ListViewItem)
  955.  
  956. #End Region
  957.  
  958. #Region " Other Objects "
  959.  
  960.     ''' <summary>
  961.     ''' Stack to store Undo actions.
  962.     ''' </summary>
  963.     Public Undostack As New Stack(Of ListView_Action)
  964.  
  965.     ''' <summary>
  966.     ''' Stack to store Redo actions.
  967.     ''' </summary>
  968.     Public Redostack As New Stack(Of ListView_Action)
  969.  
  970.     ''' <summary>
  971.     ''' Flag to check if it is doing a Undo operation.
  972.     ''' </summary>
  973.     Private IsDoingUndo As Boolean = False
  974.  
  975.     ''' <summary>
  976.     ''' Flag to check if it is doing a Redo operation.
  977.     ''' </summary>
  978.     Private IsDoingRedo As Boolean = False
  979.  
  980.     ''' <summary>
  981.     ''' The Undo/Redo action.
  982.     ''' </summary>
  983.     Private action As ListView_Action = Nothing
  984.  
  985. #End Region
  986.  
  987. #Region " Enums "
  988.  
  989.     ''' <summary>
  990.     ''' The operation.
  991.     ''' </summary>
  992.     Public Enum Operation As Short
  993.         Undo = 0
  994.         Redo = 1
  995.     End Enum
  996.  
  997.     ''' <summary>
  998.     ''' The kind of method for the Undo/Redo operation.
  999.     ''' </summary>
  1000.     Public Enum Method As Short
  1001.         Add = 0
  1002.         Remove = 1
  1003.     End Enum
  1004.  
  1005. #End Region
  1006.  
  1007. #Region " Types "
  1008.  
  1009.     ''' <summary>
  1010.     ''' Creates a Undo/Redo Action.
  1011.     ''' </summary>
  1012.     Class ListView_Action
  1013.  
  1014.         ''' <summary>
  1015.         ''' Names the Undo/Redo Action.
  1016.         ''' </summary>
  1017.         Property Name As String
  1018.  
  1019.         ''' <summary>
  1020.         ''' Points to a method to excecute.
  1021.         ''' </summary>
  1022.         Property Operation As [Delegate]
  1023.  
  1024.         ''' <summary>
  1025.         ''' Method of the Undo/Redo operation.
  1026.         ''' </summary>
  1027.         Property Method As Method
  1028.  
  1029.         ''' <summary>
  1030.         ''' Data Array for the method to excecute.
  1031.         ''' </summary>
  1032.         Property Data As ListViewItem
  1033.  
  1034.     End Class
  1035.  
  1036. #End Region
  1037.  
  1038. #Region " Events "
  1039.  
  1040.     ''' <summary>
  1041.     '''  Event is raised after an Undo/Redo action is performed.
  1042.     ''' </summary>
  1043.     Public Event UndoRedo_IsPerformed As EventHandler(Of UndoneRedoneEventArgs)
  1044.     Public Class UndoneRedoneEventArgs : Inherits EventArgs
  1045.         Public Property Operation As Operation
  1046.         Public Property Method As Method
  1047.         Public Property Item As ListViewItem
  1048.         Public Property UndoStack As Stack(Of ListView_Action)
  1049.         Public Property RedoStack As Stack(Of ListView_Action)
  1050.     End Class
  1051.  
  1052.     ''' <summary>
  1053.     ''' Event is raised when Undo/Redo Stack size changed.
  1054.     ''' </summary>
  1055.     Public Event UndoRedo_StackSizeChanged As EventHandler(Of StackSizeChangedEventArgs)
  1056.     Public Class StackSizeChangedEventArgs : Inherits EventArgs
  1057.         Public Property UndoStack As Stack(Of ListView_Action)
  1058.         Public Property RedoStack As Stack(Of ListView_Action)
  1059.         Public Property UndoStackIsEmpty As Boolean
  1060.         Public Property RedoStackIsEmpty As Boolean
  1061.     End Class
  1062.  
  1063. #End Region
  1064.  
  1065. #End Region
  1066.  
  1067. #Region " Public Methods "
  1068.  
  1069.     ''' <summary>
  1070.     ''' Undo the last action.
  1071.     ''' </summary>
  1072.     Public Sub Undo()
  1073.  
  1074.         If Me.Undostack.Count = 0 Then Exit Sub ' Nothing to Undo.
  1075.  
  1076.         Me.IsDoingUndo = True
  1077.         Me.action = Me.Undostack.Pop ' Get the Action from the Stack and remove it.
  1078.         Me.action.Operation.DynamicInvoke(Me.action.Data) ' Invoke the undo Action.
  1079.         Me.IsDoingUndo = False
  1080.  
  1081.         Raise_UndoRedo_IsPerformed(Operation.Undo, Me.action.Method, Me.action.Data)
  1082.  
  1083.     End Sub
  1084.  
  1085.     ''' <summary>
  1086.     ''' Redo the last action.
  1087.     ''' </summary>
  1088.     Public Sub Redo()
  1089.  
  1090.         If Me.Redostack.Count = 0 Then Exit Sub ' Nothing to Redo.
  1091.  
  1092.         Me.IsDoingRedo = True
  1093.         Me.action = Me.Redostack.Pop() ' Get the Action from the Stack and remove it.
  1094.         Me.action.Operation.DynamicInvoke(Me.action.Data) ' Invoke the redo Action.
  1095.         Me.IsDoingRedo = False
  1096.  
  1097.         Raise_UndoRedo_IsPerformed(Operation.Redo, Me.action.Method, Me.action.Data)
  1098.  
  1099.     End Sub
  1100.  
  1101. #End Region
  1102.  
  1103. #Region " Private Methods "
  1104.  
  1105.     ''' <summary>
  1106.     ''' Reverses an Undo/Redo action.
  1107.     ''' </summary>
  1108.     Private Function GetReverseAction(ByVal e As UndoneRedoneEventArgs) As ListView_Action
  1109.  
  1110.         Me.action = New ListView_Action
  1111.  
  1112.         Me.action.Name = e.Item.Text
  1113.         Me.action.Data = e.Item
  1114.  
  1115.         Me.action.Operation = If(e.Method = Method.Add, _
  1116.                         New RemoveDelegate(AddressOf Me.RemoveItem), _
  1117.                         New AddDelegate(AddressOf Me.AddItem))
  1118.  
  1119.         Me.action.Method = If(e.Method = Method.Add, _
  1120.                      Method.Remove, _
  1121.                      Method.Add)
  1122.  
  1123.         Return Me.action
  1124.  
  1125.     End Function
  1126.  
  1127.     ''' <summary>
  1128.     ''' Raises the "UndoRedo_IsPerformed" Event.
  1129.     ''' </summary>
  1130.     Private Sub Raise_UndoRedo_IsPerformed(ByVal Operation As Operation, _
  1131.                                            ByVal Method As Method, _
  1132.                                            ByVal Item As ListViewItem)
  1133.  
  1134.         RaiseEvent UndoRedo_IsPerformed(Me, New UndoneRedoneEventArgs _
  1135.                    With {.Item = Item, _
  1136.                          .Method = Method, _
  1137.                          .Operation = Operation, _
  1138.                          .UndoStack = Me.Undostack, _
  1139.                          .RedoStack = Me.Redostack})
  1140.  
  1141.         Raise_UndoRedo_StackSizeChanged()
  1142.  
  1143.     End Sub
  1144.  
  1145.     ''' <summary>
  1146.     ''' Raises the "UndoRedo_StackSizeChanged" Event.
  1147.     ''' </summary>
  1148.     Private Sub Raise_UndoRedo_StackSizeChanged()
  1149.  
  1150.         RaiseEvent UndoRedo_StackSizeChanged(Me, New StackSizeChangedEventArgs _
  1151.                    With {.UndoStack = Me.Undostack, _
  1152.                          .RedoStack = Me.Redostack, _
  1153.                          .UndoStackIsEmpty = Me.Undostack.Count = 0, _
  1154.                          .RedoStackIsEmpty = Me.Redostack.Count = 0})
  1155.  
  1156.     End Sub
  1157.  
  1158. #End Region
  1159.  
  1160. #Region " Event Handlers "
  1161.  
  1162.     ''' <summary>
  1163.     ''' This handles when an Undo or Redo operation is performed.
  1164.     ''' </summary>
  1165.     Private Sub UndoneRedone(ByVal sender As Object, ByVal e As UndoneRedoneEventArgs) _
  1166.     Handles Me.UndoRedo_IsPerformed
  1167.  
  1168.         Select Case e.Operation
  1169.  
  1170.             Case Operation.Undo
  1171.                 ' Create a Redo Action for the undone action.
  1172.                 Me.Redostack.Push(GetReverseAction(e))
  1173.  
  1174.             Case Operation.Redo
  1175.                 ' Create a Undo Action for the redone action.
  1176.                 Me.Undostack.Push(GetReverseAction(e))
  1177.  
  1178.         End Select
  1179.  
  1180.     End Sub
  1181.  
  1182.     ''' <summary>
  1183.     ''' Monitors when an Item is added to create an Undo Operation.
  1184.     ''' </summary>
  1185.     Private Sub OnItemAdded(sender As Object, e As ItemAddedEventArgs) _
  1186.     Handles Me.ItemAdded
  1187.  
  1188.         If Me.UndoRedo_Enabled _
  1189.             AndAlso (Not Me.IsDoingUndo And Not Me.IsDoingRedo) Then
  1190.  
  1191.             Me.Redostack.Clear()
  1192.  
  1193.             ' // Crate an Undo Action
  1194.             Me.action = New ListView_Action
  1195.             Me.action.Name = e.Item.Text
  1196.             Me.action.Operation = New RemoveDelegate(AddressOf Me.RemoveItem)
  1197.             Me.action.Data = e.Item
  1198.             Me.action.Method = Method.Remove
  1199.  
  1200.             Me.Undostack.Push(action)
  1201.  
  1202.             Raise_UndoRedo_StackSizeChanged()
  1203.  
  1204.         End If
  1205.  
  1206.     End Sub
  1207.  
  1208.     ''' <summary>
  1209.     ''' Monitors when an Item is removed to create an Undo Operation.
  1210.     ''' </summary>
  1211.     Private Sub OnItemRemoved(sender As Object, e As ItemRemovedEventArgs) _
  1212.     Handles Me.ItemRemoved
  1213.  
  1214.         If Me.UndoRedo_Enabled _
  1215.             AndAlso (Not Me.IsDoingUndo And Not Me.IsDoingRedo) Then
  1216.  
  1217.             Me.Redostack.Clear()
  1218.  
  1219.             ' // Crate an Undo Action
  1220.             Me.action = New ListView_Action
  1221.             Me.action.Name = e.Item.Text
  1222.             Me.action.Operation = New AddDelegate(AddressOf Me.AddItem)
  1223.             Me.action.Data = e.Item
  1224.             Me.action.Method = Method.Add
  1225.  
  1226.             Me.Undostack.Push(action)
  1227.  
  1228.             Raise_UndoRedo_StackSizeChanged()
  1229.  
  1230.         End If
  1231.  
  1232.     End Sub
  1233.  
  1234. #End Region
  1235.  
  1236. #End Region
  1237.  
  1238. #End Region
  1239.  
  1240. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement