Advertisement
Guest User

ControlDragger.vb

a guest
Mar 29th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 29.87 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 21-November-2015
  4. ' ***********************************************************************
  5.  
  6. #Region " Public Members Summary "
  7.  
  8. #Region " Constructors "
  9.  
  10. ' New(Control, Opt: Boolean, Opt: Cursor)
  11. ' New(IEnumerable(Of Control))
  12.  
  13. #End Region
  14.  
  15. #Region " Properties "
  16.  
  17. ' Controls As IEnumerable(Of ControlDragInfo)
  18. ' Empty As ControlDragger
  19.  
  20. #End Region
  21.  
  22. #Region " Functions "
  23.  
  24. ' FindControlDragInfo(Control) As ControlDragInfo
  25. ' FindControlDragInfo(String, Opt: StringComparison) As ControlDragInfo
  26.  
  27. #End Region
  28.  
  29. #Region " Methods "
  30.  
  31. ' AddControl(Control, Opt: Boolean, Opt: Cursor)
  32. ' AddControls(Control())
  33. ' RemoveControl(Control)
  34. ' RemoveControls(Control())
  35. ' Dispose()
  36.  
  37. #End Region
  38.  
  39. #End Region
  40.  
  41. #Region " Option Statements "
  42.  
  43. Option Explicit On
  44. Option Strict On
  45. Option Infer Off
  46.  
  47. #End Region
  48.  
  49. #Region " Usage Examples "
  50.  
  51. 'Public Class Form1 : Inherits Form
  52. '
  53. '    Dim ctrlDragger As New ControlDragger
  54. '
  55. '    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  56. '
  57. '        ctrlDragger.AddControl(Me.PictureBox1)
  58. '        Dim ctrlInfo As ControlDragInfo = ctrlDragger.FindControlDragInfo(Me.PictureBox1)
  59. '
  60. '        ctrlInfo.Enabled = True
  61. '        ctrlInfo.Cursor = Cursors.SizeAll
  62.  
  63. '
  64. '    End Sub
  65. '
  66. 'End Class
  67.  
  68. #End Region
  69.  
  70. #Region " Imports "
  71.  
  72. Imports System.Collections.ObjectModel
  73. Imports System.ComponentModel
  74. Imports System.Drawing
  75. Imports System.Windows.Forms
  76. Imports Elektro.Core.Types
  77.  
  78. #End Region
  79.  
  80. #Region " Control Dragger "
  81.  
  82. Namespace UI.Types
  83.  
  84.     ''' ----------------------------------------------------------------------------------------------------
  85.     ''' <summary>
  86.     ''' Adds dragging capabilities to a single or multiple <see cref="Control"/> when clicking on they client area.
  87.     ''' </summary>
  88.     ''' ----------------------------------------------------------------------------------------------------
  89.     ''' <example> This is a code example.
  90.     ''' <code>
  91.     ''' Public Class Form1 : Inherits Form
  92.     '''
  93.     '''     Dim ctrlDragger As New ControlDragger
  94.     '''
  95.     '''     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  96.     '''
  97.     '''         ctrlDragger.AddControl(Me.PictureBox1)
  98.     '''         Dim ctrlInfo As ControlDragInfo = ctrlDragger.FindControlDragInfo(Me.PictureBox1)
  99.     '''
  100.     '''         ctrlInfo.Enabled = True
  101.     '''         ctrlInfo.Cursor = Cursors.SizeAll
  102.     '''
  103.     '''
  104.     '''     End Sub
  105.     '''
  106.     ''' End Class
  107.     ''' </code>
  108.     ''' </example>
  109.     ''' ----------------------------------------------------------------------------------------------------
  110.     Public NotInheritable Class ControlDragger : Inherits AestheticObject : Implements IDisposable
  111.  
  112. #Region " Properties "
  113.  
  114.         ''' ----------------------------------------------------------------------------------------------------
  115.         ''' <summary>
  116.         ''' Gets an <see cref="IEnumerable(Of Control)"/> collection that contains the
  117.         ''' owner controls that can perform draggable operations.
  118.         ''' </summary>
  119.         ''' ----------------------------------------------------------------------------------------------------
  120.         ''' <value>
  121.         ''' The <see cref="IEnumerable(Of Control)"/>.
  122.         ''' </value>
  123.         ''' ----------------------------------------------------------------------------------------------------
  124.         <EditorBrowsable(EditorBrowsableState.Always)>
  125.         Public ReadOnly Property Controls As ReadOnlyCollection(Of ControlDragInfo)
  126.             <DebuggerStepThrough>
  127.             Get
  128.                 Return New ReadOnlyCollection(Of ControlDragInfo)(Me.controlsB.ToArray)
  129.             End Get
  130.         End Property
  131.         ''' ----------------------------------------------------------------------------------------------------
  132.         ''' <summary>
  133.         ''' ( Backing field )
  134.         ''' An <see cref="IEnumerable(Of Control)"/> collection that contains the
  135.         ''' owner controls that can perform draggable operations.
  136.         ''' </summary>
  137.         ''' ----------------------------------------------------------------------------------------------------
  138.         Private controlsB As IEnumerable(Of ControlDragInfo)
  139.  
  140.         ''' ----------------------------------------------------------------------------------------------------
  141.         ''' <summary>
  142.         ''' Represents a nul <see cref="ControlDragger"/> instance.
  143.         ''' </summary>
  144.         ''' ----------------------------------------------------------------------------------------------------
  145.         ''' <value>
  146.         ''' <see langword="Nothing"/>
  147.         ''' </value>
  148.         ''' ----------------------------------------------------------------------------------------------------
  149.         <EditorBrowsable(EditorBrowsableState.Always)>
  150.         Public Shared ReadOnly Property Empty As ControlDragger
  151.             <DebuggerStepThrough>
  152.             Get
  153.                 Return Nothing
  154.             End Get
  155.         End Property
  156.  
  157. #End Region
  158.  
  159. #Region " Constructors "
  160.  
  161.         ''' ----------------------------------------------------------------------------------------------------
  162.         ''' <summary>
  163.         ''' Initializes a new instance of the <see cref="ControlDragger"/> class.
  164.         ''' </summary>
  165.         ''' ----------------------------------------------------------------------------------------------------
  166.         <DebuggerStepThrough>
  167.         Public Sub New()
  168.             Me.controlsB = {}
  169.         End Sub
  170.  
  171.         ''' ----------------------------------------------------------------------------------------------------
  172.         ''' <summary>
  173.         ''' Initializes a new instance of the <see cref="ControlDragger"/> class.
  174.         ''' </summary>
  175.         ''' ----------------------------------------------------------------------------------------------------
  176.         ''' <param name="ctrl">
  177.         ''' The owner <see cref="Control"/> used to perform draggable operations.
  178.         ''' </param>
  179.         '''
  180.         ''' <param name="enabled">
  181.         ''' If set to <see langword="True"/>, inmediately enables dragging on the <see cref="Control"/>.
  182.         ''' </param>
  183.         '''
  184.         ''' <param name="cursor">
  185.         ''' The <see cref="Cursor"/> to use when dragging the <see cref="Control"/>.
  186.         ''' </param>
  187.         ''' ----------------------------------------------------------------------------------------------------
  188.         <DebuggerStepThrough>
  189.         Public Sub New(ByVal ctrl As Control,
  190.                        Optional enabled As Boolean = False,
  191.                        Optional cursor As Cursor = Nothing)
  192.  
  193.             Me.controlsB =
  194.                 {
  195.                     New ControlDragInfo(ctrl) With
  196.                         {
  197.                             .Enabled = enabled,
  198.                             .Cursor = cursor
  199.                         }
  200.                 }
  201.  
  202.             Me.AddHandlers(ctrl)
  203.  
  204.         End Sub
  205.  
  206.         ''' ----------------------------------------------------------------------------------------------------
  207.         ''' <summary>
  208.         ''' Initializes a new instance of the <see cref="ControlDragger"/> class.
  209.         ''' </summary>
  210.         ''' ----------------------------------------------------------------------------------------------------
  211.         ''' <param name="ctrls">
  212.         ''' The owner <see cref="Controls"/> used to perform draggable operations.
  213.         ''' </param>
  214.         ''' ----------------------------------------------------------------------------------------------------
  215.         <DebuggerStepThrough>
  216.         Public Sub New(ByVal ctrls As IEnumerable(Of Control))
  217.  
  218.             Me.controlsB = (From ctrl As Control In ctrls Select New ControlDragInfo(ctrl)).ToArray
  219.  
  220.             For Each c As Control In ctrls
  221.                 Me.AddHandlers(c)
  222.             Next c
  223.  
  224.         End Sub
  225.  
  226.         ''' ----------------------------------------------------------------------------------------------------
  227.         ''' <summary>
  228.         ''' Initializes a new instance of the <see cref="ControlDragger"/> class.
  229.         ''' </summary>
  230.         ''' ----------------------------------------------------------------------------------------------------
  231.         ''' <param name="controlInfo">
  232.         ''' The <see cref="FormDragInfo"/> instance that contains the <see cref="Control"/> reference and its draggable info.
  233.         ''' </param>
  234.         '''
  235.         ''' <param name="mouseCoordinates">
  236.         ''' The current mouse coordinates.
  237.         ''' </param>
  238.         '''
  239.         ''' <param name="location">
  240.         ''' The current location.
  241.         ''' </param>
  242.         ''' ----------------------------------------------------------------------------------------------------
  243.         <DebuggerNonUserCode>
  244.         Private Sub New(ByVal controlInfo As ControlDragInfo,
  245.                         ByVal mouseCoordinates As Point,
  246.                         ByVal location As Point)
  247.  
  248.             controlInfo.InitialMouseCoords = mouseCoordinates
  249.             controlInfo.InitialLocation = location
  250.  
  251.         End Sub
  252.  
  253. #End Region
  254.  
  255. #Region " Public Methods "
  256.  
  257.         ''' ----------------------------------------------------------------------------------------------------
  258.         ''' <summary>
  259.         ''' Assigns the specified <see cref="Control"/> as a draggable element.
  260.         ''' </summary>
  261.         ''' ----------------------------------------------------------------------------------------------------
  262.         ''' <param name="ctrl">
  263.         ''' The <see cref="Control"/>.
  264.         ''' </param>
  265.         '''
  266.         ''' <param name="enabled">
  267.         ''' If set to <see langword="True"/>, inmediatelly enables dragging on the <see cref="Control"/>.
  268.         ''' </param>
  269.         '''
  270.         ''' <param name="cursor">
  271.         ''' The <see cref="Cursor"/> to use when dragging the <see cref="Control"/>.
  272.         ''' </param>
  273.         ''' ----------------------------------------------------------------------------------------------------
  274.         ''' <exception cref="Global.System.ArgumentException">
  275.         ''' The specified control is already added.;ctrl
  276.         ''' </exception>
  277.         ''' ----------------------------------------------------------------------------------------------------
  278.         <DebuggerStepThrough>
  279.         Public Sub AddControl(ByVal ctrl As Control,
  280.                               Optional enabled As Boolean = False,
  281.                               Optional cursor As Cursor = Nothing)
  282.  
  283.             For Each controlInfo As ControlDragInfo In Me.controlsB
  284.  
  285.                 If controlInfo.Control.Equals(ctrl) Then
  286.                     Throw New ArgumentException("The specified form is already added.", "form")
  287.                     Exit Sub
  288.                 End If
  289.  
  290.             Next controlInfo
  291.  
  292.             Dim newControlInfo As New ControlDragInfo(ctrl) With {.Enabled = enabled, .Cursor = cursor}
  293.             Me.controlsB = Me.controlsB.Concat({newControlInfo})
  294.             Me.AddHandlers(ctrl)
  295.  
  296.         End Sub
  297.  
  298.         ''' ----------------------------------------------------------------------------------------------------
  299.         ''' <summary>
  300.         ''' Assigns the specified controls as draggable elements of they parents <see cref="Form"/>.
  301.         ''' </summary>
  302.         ''' ----------------------------------------------------------------------------------------------------
  303.         ''' <param name="ctrls">
  304.         ''' An array of <see cref="Control"/>.
  305.         ''' </param>
  306.         ''' ----------------------------------------------------------------------------------------------------
  307.         ''' <exception cref="Global.System.ArgumentException">
  308.         ''' The parent form of the control is not handled.
  309.         ''' </exception>
  310.         ''' ----------------------------------------------------------------------------------------------------
  311.         <DebuggerStepThrough>
  312.         Public Sub AddControls(ByVal ctrls As Control())
  313.  
  314.             For Each ctrl As Control In ctrls
  315.                 Me.AddControl(ctrl)
  316.             Next
  317.  
  318.         End Sub
  319.  
  320.         ''' ----------------------------------------------------------------------------------------------------
  321.         ''' <summary>
  322.         ''' Removes the specified <see cref="Control"/> from the draggable elements.
  323.         ''' </summary>
  324.         ''' ----------------------------------------------------------------------------------------------------
  325.         ''' <param name="ctrl">
  326.         ''' The <see cref="Control"/>.
  327.         ''' </param>
  328.         ''' ----------------------------------------------------------------------------------------------------
  329.         ''' <exception cref="Global.System.ArgumentException">
  330.         ''' The specified control is not found.;ctrl
  331.         ''' </exception>
  332.         ''' ----------------------------------------------------------------------------------------------------
  333.         <DebuggerStepThrough>
  334.         Public Sub RemoveControl(ByVal ctrl As Control)
  335.  
  336.             Dim controlInfoToRemove As ControlDragInfo = Nothing
  337.  
  338.             For Each controlInfo As ControlDragInfo In Me.controlsB
  339.  
  340.                 If controlInfo.Control.Equals(ctrl) Then
  341.                     controlInfoToRemove = controlInfo
  342.                     Exit For
  343.                 End If
  344.  
  345.             Next controlInfo
  346.  
  347.             If (controlInfoToRemove IsNot Nothing) Then
  348.  
  349.                 Me.controlsB = (From controlInfo As ControlDragInfo In Me.controlsB
  350.                                 Where Not controlInfo.Equals(controlInfoToRemove))
  351.  
  352.                 controlInfoToRemove.Enabled = False
  353.                 Me.RemoveHandlers(controlInfoToRemove.Control)
  354.  
  355.             Else
  356.                 Throw New ArgumentException("The specified control is not found.", "ctrl")
  357.  
  358.             End If
  359.  
  360.         End Sub
  361.  
  362.         ''' ----------------------------------------------------------------------------------------------------
  363.         ''' <summary>
  364.         ''' Removes the specified controls from the draggable elements.
  365.         ''' </summary>
  366.         ''' ----------------------------------------------------------------------------------------------------
  367.         ''' <param name="ctrls">
  368.         ''' An array of <see cref="Control"/>.
  369.         ''' </param>
  370.         ''' ----------------------------------------------------------------------------------------------------
  371.         <DebuggerStepThrough>
  372.         Public Sub RemoveControls(ByVal ctrls As Control())
  373.  
  374.             For Each ctrl As Control In ctrls
  375.                 Me.RemoveControl(ctrl)
  376.             Next
  377.  
  378.         End Sub
  379.  
  380.         ''' ----------------------------------------------------------------------------------------------------
  381.         ''' <summary>
  382.         ''' Finds the <see cref="ControlDragInfo"/> instance that is associated with the specified <see cref="Control"/>.
  383.         ''' </summary>
  384.         ''' ----------------------------------------------------------------------------------------------------
  385.         ''' <param name="ctrl">
  386.         ''' The <see cref="Control"/>.
  387.         ''' </param>
  388.         ''' ----------------------------------------------------------------------------------------------------
  389.         ''' <returns>
  390.         ''' The <see cref="ControlDragInfo"/> instance that is associated with the specified <see cref="Control"/>.
  391.         ''' </returns>
  392.         ''' ----------------------------------------------------------------------------------------------------
  393.         <DebuggerStepThrough>
  394.         Public Function FindControlDragInfo(ByVal ctrl As Control) As ControlDragInfo
  395.  
  396.             Return (From controlInfo As ControlDragInfo In Me.controlsB
  397.                     Where controlInfo.Control.Equals(ctrl)
  398.                     ).SingleOrDefault
  399.  
  400.         End Function
  401.  
  402.         ''' ----------------------------------------------------------------------------------------------------
  403.         ''' <summary>
  404.         ''' Finds the <see cref="ControlDragInfo"/> instance that is associated with the specified <see cref="Control"/>.
  405.         ''' </summary>
  406.         ''' ----------------------------------------------------------------------------------------------------
  407.         ''' <param name="controlName">
  408.         ''' The name of the <see cref="Control"/>.
  409.         ''' </param>
  410.         ''' ----------------------------------------------------------------------------------------------------
  411.         ''' <returns>
  412.         ''' The <see cref="FormDragInfo"/> instance that is associated with the specified <see cref="Control"/> .
  413.         ''' </returns>
  414.         ''' ----------------------------------------------------------------------------------------------------
  415.         <DebuggerStepThrough>
  416.         Public Function FindControlDragInfo(ByVal controlName As String,
  417.                                             Optional stringComparison As StringComparison =
  418.                                                      StringComparison.OrdinalIgnoreCase) As ControlDragInfo
  419.  
  420.             Return (From controlInfo As ControlDragInfo In Me.controlsB
  421.                     Where controlInfo.Name.Equals(controlName, stringComparison)
  422.                     ).SingleOrDefault
  423.  
  424.         End Function
  425.  
  426. #End Region
  427.  
  428. #Region " Private Methods "
  429.  
  430.         ''' ----------------------------------------------------------------------------------------------------
  431.         ''' <summary>
  432.         ''' Adds the <see cref="Control"/> handlers to enable draggable operations.
  433.         ''' </summary>
  434.         ''' ----------------------------------------------------------------------------------------------------
  435.         ''' <param name="ctrl">
  436.         ''' The <see cref="Control"/>.
  437.         ''' </param>
  438.         ''' ----------------------------------------------------------------------------------------------------
  439.         <DebuggerStepThrough>
  440.         Private Sub AddHandlers(ByVal ctrl As Control)
  441.  
  442.             AddHandler ctrl.MouseDown, AddressOf Me.Element_MouseDown
  443.             AddHandler ctrl.MouseUp, AddressOf Me.Element_MouseUp
  444.             AddHandler ctrl.MouseMove, AddressOf Me.Element_MouseMove
  445.             ' AddHandler ctrl.MouseEnter, AddressOf Me.Element_MouseEnter
  446.             ' AddHandler ctrl.MouseLeave, AddressOf Me.Element_MouseLeave
  447.  
  448.         End Sub
  449.  
  450.         ''' ----------------------------------------------------------------------------------------------------
  451.         ''' <summary>
  452.         ''' Removes the <see cref="Control"/> handlers to disable draggable operations.
  453.         ''' </summary>
  454.         ''' ----------------------------------------------------------------------------------------------------
  455.         ''' <param name="ctrl">
  456.         ''' The <see cref="Control"/>.
  457.         ''' </param>
  458.         ''' ----------------------------------------------------------------------------------------------------
  459.         <DebuggerStepThrough>
  460.         Private Sub RemoveHandlers(ByVal ctrl As Control)
  461.  
  462.             If Not (ctrl.IsDisposed) AndAlso Not (ctrl.Disposing) Then
  463.  
  464.                 RemoveHandler ctrl.MouseDown, AddressOf Me.Element_MouseDown
  465.                 RemoveHandler ctrl.MouseUp, AddressOf Me.Element_MouseUp
  466.                 RemoveHandler ctrl.MouseMove, AddressOf Me.Element_MouseMove
  467.                 ' RemoveHandler ctrl.MouseEnter, AddressOf Me.Element_MouseEnter
  468.                 ' RemoveHandler ctrl.MouseLeave, AddressOf Me.Element_MouseLeave
  469.  
  470.             End If
  471.  
  472.         End Sub
  473.  
  474.         ''' ----------------------------------------------------------------------------------------------------
  475.         ''' <summary>
  476.         ''' Return the new control location.
  477.         ''' </summary>
  478.         ''' ----------------------------------------------------------------------------------------------------
  479.         ''' <param name="controlInfo">
  480.         ''' The <see cref="ControlDragInfo"/> instance that contains the <see cref="Control"/> instance and its info.
  481.         ''' </param>
  482.         '''
  483.         ''' <param name="currentLocation">
  484.         ''' The current form location.
  485.         ''' </param>
  486.         ''' ----------------------------------------------------------------------------------------------------
  487.         ''' <returns>
  488.         ''' The new control location.
  489.         ''' </returns>
  490.         ''' ----------------------------------------------------------------------------------------------------
  491.         <DebuggerStepThrough>
  492.         Private Function GetNewLocation(ByVal controlInfo As ControlDragInfo,
  493.                                         ByVal currentLocation As Point) As Point
  494.  
  495.             Dim x As Integer = (controlInfo.InitialLocation.X + (currentLocation.X - controlInfo.InitialMouseCoords.X))
  496.             Dim y As Integer = (controlInfo.InitialLocation.Y + (currentLocation.Y - controlInfo.InitialMouseCoords.Y))
  497.             Return New Point(x, y)
  498.  
  499.         End Function
  500.  
  501. #End Region
  502.  
  503. #Region " Event Handlers "
  504.  
  505.         ' ''' ----------------------------------------------------------------------------------------------------
  506.         ' ''' <summary>
  507.         ' ''' Handles the <see cref="Control.Disposed"/> event of the owner controls.
  508.         ' ''' </summary>
  509.         ' ''' ----------------------------------------------------------------------------------------------------
  510.         ' ''' <param name="sender">
  511.         ' ''' The source of the event.
  512.         ' ''' </param>
  513.         ' '''
  514.         ' ''' <param name="e">
  515.         ' ''' The <see cref="MouseEventArgs"/> instance containing the event data.
  516.         ' ''' </param>
  517.         ' ''' ----------------------------------------------------------------------------------------------------
  518.         'Private Sub Element_Disposed(ByVal sender As Object, ByVal e As MouseEventArgs)
  519.         '
  520.         'End Sub
  521.  
  522.         ' ''' ----------------------------------------------------------------------------------------------------
  523.         ' ''' <summary>
  524.         ' ''' Handles the <see cref="Control.MouseEnter"/> event of the owner controls.
  525.         ' ''' </summary>
  526.         ' ''' ----------------------------------------------------------------------------------------------------
  527.         ' ''' <param name="sender">
  528.         ' ''' The source of the event.
  529.         ' ''' </param>
  530.         ' '''
  531.         ' ''' <param name="e">
  532.         ' ''' The <see cref="EventArgs"/> instance containing the event data.
  533.         ' ''' </param>
  534.         ' ''' ----------------------------------------------------------------------------------------------------
  535.         'Private Sub Element_MouseEnter(ByVal sender As Object, ByVal e As EventArgs)
  536.         '
  537.         '    Dim controlInfo As ControlDragInfo = Me.FindFormDragInfo(DirectCast(sender, Control))
  538.         '
  539.         '    controlInfo.CursorNormal = controlInfo.Control.Cursor
  540.         '
  541.         '    If (controlInfo.Enabled) Then
  542.         '        controlInfo.Control.Cursor = controlInfo.Cursor
  543.         '        ' Optionally:
  544.         '        ' controlInfo.Form.BringToFront()
  545.         '    End If
  546.         '
  547.         'End Sub
  548.  
  549.         ' ''' ----------------------------------------------------------------------------------------------------
  550.         ' ''' <summary>
  551.         ' ''' Handles the <see cref="Control.MouseLeave"/> event of the owner controls.
  552.         ' ''' </summary>
  553.         ' ''' ----------------------------------------------------------------------------------------------------
  554.         ' ''' <param name="sender">
  555.         ' ''' The source of the event.
  556.         ' ''' </param>
  557.         ' '''
  558.         ' ''' <param name="e">
  559.         ' ''' The <see cref="EventArgs"/> instance containing the event data.
  560.         ' ''' </param>
  561.         ' ''' ----------------------------------------------------------------------------------------------------
  562.         'Private Sub Element_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
  563.         '
  564.         '    Dim controlInfo As ControlDragInfo = Me.FindFormDragInfo(DirectCast(sender, Control))
  565.         '
  566.         '    controlInfo.Control.Cursor = controlInfo.CursorNormal
  567.         '
  568.         'End Sub
  569.  
  570.         ''' ----------------------------------------------------------------------------------------------------
  571.         ''' <summary>
  572.         ''' Handles the <see cref="Control.MouseDown"/> event of the owner controls.
  573.         ''' </summary>
  574.         ''' ----------------------------------------------------------------------------------------------------
  575.         ''' <param name="sender">
  576.         ''' The source of the event.
  577.         ''' </param>
  578.         '''
  579.         ''' <param name="e">
  580.         ''' The <see cref="MouseEventArgs"/> instance containing the event data.
  581.         ''' </param>
  582.         ''' ----------------------------------------------------------------------------------------------------
  583.         Private Sub Element_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
  584.  
  585.             Dim controlInfo As ControlDragInfo = Me.FindControlDragInfo(DirectCast(sender, Control))
  586.  
  587.             If (controlInfo.Enabled) Then
  588.                 controlInfo.Control.BringToFront()
  589.                 controlInfo.Control.Cursor = controlInfo.Cursor
  590.                 controlInfo.DragInfo = New ControlDragger(controlInfo, Control.MousePosition, controlInfo.Control.Location)
  591.             End If
  592.  
  593.         End Sub
  594.  
  595.         ''' ----------------------------------------------------------------------------------------------------
  596.         ''' <summary>
  597.         ''' Handles the <see cref="Control.MouseUp"/> event of the owner controls.
  598.         ''' </summary>
  599.         ''' ----------------------------------------------------------------------------------------------------
  600.         ''' <param name="sender">
  601.         ''' The source of the event.
  602.         ''' </param>
  603.         '''
  604.         ''' <param name="e">
  605.         ''' The <see cref="MouseEventArgs"/> instance containing the event data.
  606.         ''' </param>
  607.         ''' ----------------------------------------------------------------------------------------------------
  608.         Private Sub Element_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
  609.  
  610.             Dim controlInfo As ControlDragInfo = Me.FindControlDragInfo(DirectCast(sender, Control))
  611.  
  612.             controlInfo.DragInfo = ControlDragger.Empty
  613.             controlInfo.Control.Cursor = controlInfo.CursorNormal
  614.  
  615.         End Sub
  616.  
  617.         ''' ----------------------------------------------------------------------------------------------------
  618.         ''' <summary>
  619.         ''' Handles the <see cref="Control.MouseMove"/> event of the owner controls.
  620.         ''' </summary>
  621.         ''' ----------------------------------------------------------------------------------------------------
  622.         ''' <param name="sender">
  623.         ''' The source of the event.
  624.         ''' </param>
  625.         '''
  626.         ''' <param name="e">
  627.         ''' The <see cref="MouseEventArgs"/> instance containing the event data.
  628.         ''' </param>
  629.         ''' ----------------------------------------------------------------------------------------------------
  630.         Private Sub Element_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
  631.  
  632.             Dim controlInfo As ControlDragInfo = Me.FindControlDragInfo(DirectCast(sender, Control))
  633.  
  634.             If (controlInfo.Enabled) AndAlso (controlInfo.DragInfo IsNot ControlDragger.Empty) Then
  635.                 controlInfo.Control.Location = controlInfo.DragInfo.GetNewLocation(controlInfo, Control.MousePosition)
  636.             End If
  637.  
  638.         End Sub
  639.  
  640. #End Region
  641.  
  642. #Region " IDisposable Implementation "
  643.  
  644.         ''' ----------------------------------------------------------------------------------------------------
  645.         ''' <summary>
  646.         ''' Flag to detect redundant calls when disposing.
  647.         ''' </summary>
  648.         ''' ----------------------------------------------------------------------------------------------------
  649.         Private isDisposed As Boolean = False
  650.  
  651.         ''' ----------------------------------------------------------------------------------------------------
  652.         ''' <summary>
  653.         ''' Releases all the resources used by this instance.
  654.         ''' </summary>
  655.         ''' ----------------------------------------------------------------------------------------------------
  656.         <DebuggerStepThrough>
  657.         Public Sub Dispose() Implements IDisposable.Dispose
  658.             Me.Dispose(isDisposing:=True)
  659.             GC.SuppressFinalize(obj:=Me)
  660.         End Sub
  661.  
  662.         ''' ----------------------------------------------------------------------------------------------------
  663.         ''' <summary>
  664.         ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  665.         ''' Releases unmanaged and, optionally, managed resources.
  666.         ''' </summary>
  667.         ''' ----------------------------------------------------------------------------------------------------
  668.         ''' <param name="isDisposing">
  669.         ''' <see langword="True"/>  to release both managed and unmanaged resources;
  670.         ''' <see langword="False"/> to release only unmanaged resources.
  671.         ''' </param>
  672.         ''' ----------------------------------------------------------------------------------------------------
  673.         <DebuggerStepThrough>
  674.         Protected Sub Dispose(ByVal isDisposing As Boolean)
  675.  
  676.             If (Not Me.isDisposed) AndAlso (isDisposing) Then
  677.  
  678.                 For Each formInfo As ControlDragInfo In Me.controlsB
  679.  
  680.                     With formInfo
  681.  
  682.                         .Enabled = False
  683.                         .CursorNormal = Nothing
  684.                         .DragInfo = ControlDragger.Empty
  685.                         .InitialMouseCoords = Point.Empty
  686.                         .InitialLocation = Point.Empty
  687.  
  688.                         Me.RemoveHandlers(.Control)
  689.  
  690.                     End With ' form
  691.  
  692.                 Next formInfo
  693.  
  694.                 Me.controlsB = Nothing
  695.  
  696.             End If
  697.  
  698.             Me.isDisposed = True
  699.  
  700.         End Sub
  701.  
  702. #End Region
  703.  
  704.     End Class
  705.  
  706. End Namespace
  707.  
  708. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement