Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' March 10, 2020
- ' --------------
- Imports System.Runtime.InteropServices
- Imports System.Windows.Forms
- Imports System.Drawing
- Public Class DragSelectListView
- Inherits ListView
- Private startPoint As Point
- Sub New()
- DoubleBuffered = True
- End Sub
- Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
- MyBase.OnMouseDown(e)
- If e.Button = MouseButtons.Left AndAlso
- Items.Count > 1 Then
- Dim vsp = GetScrollPos(Handle, Orientation.Vertical)
- Dim yoffset = Font.Height * vsp
- startPoint = New Point(e.X, e.Y + yoffset)
- End If
- End Sub
- Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
- MyBase.OnMouseMove(e)
- If e.Button = MouseButtons.Left AndAlso Items.Count > 1 Then
- Dim vsp = GetScrollPos(Handle, Orientation.Vertical)
- Dim fh = Font.Height
- Dim yoffset = fh * vsp
- Dim selRect As New Rectangle(Math.Min(startPoint.X, e.Location.X),
- Math.Min(startPoint.Y - yoffset, e.Location.Y),
- Math.Abs(e.Location.X - startPoint.X),
- Math.Abs(e.Location.Y - startPoint.Y + yoffset))
- Dim cr = ClientRectangle
- 'Toggle selection...
- For Each item In Items.Cast(Of ListViewItem).
- Where(Function(x) x.Bounds.IntersectsWith(cr))
- item.Selected = selRect.IntersectsWith(item.Bounds)
- Next
- 'Scroll if needed...
- Dim p = PointToClient(Cursor.Position)
- Dim lvi = GetItemAt(p.X, p.Y)
- If lvi Is Nothing Then Return
- If lvi.Index > 0 AndAlso (p.Y - lvi.Bounds.Height * 1.5) <= fh Then
- Items(lvi.Index - 1).EnsureVisible()
- ElseIf lvi.Index < Items.Count - 1 AndAlso (p.Y + lvi.Bounds.Height * 1.5) > (Height - fh) Then
- Items(lvi.Index + 1).EnsureVisible()
- End If
- End If
- End Sub
- <DllImport("user32.dll", CharSet:=CharSet.Auto)>
- Private Shared Function GetScrollPos(hWnd As IntPtr,
- nBar As Orientation) As Integer
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement