Advertisement
JQSOFT

DragSelectListView - C#

Mar 9th, 2020
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. // March 10, 2020
  2.  
  3. using System;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. using System.Drawing;
  7. using System.ComponentModel;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace JQ.Windows.Forms
  11. {
  12.     [DesignerCategory("Code")]
  13.     public class DragSelectListView : ListView
  14.     {
  15.         #region Class Members
  16.  
  17.         private Point StartPoint;
  18.  
  19.         #endregion
  20.  
  21.         #region Constructors
  22.  
  23.         public DragSelectListView() : base() => DoubleBuffered = true;
  24.  
  25.         #endregion
  26.  
  27.         #region Base
  28.  
  29.         protected override void OnMouseDown(MouseEventArgs e)
  30.         {
  31.             base.OnMouseDown(e);
  32.  
  33.             if (e.Button == MouseButtons.Left && Items.Count > 1)
  34.             {
  35.                 var vsp = GetScrollPos(Handle, Orientation.Vertical);
  36.                 var yOffset = Font.Height * vsp;
  37.  
  38.                 StartPoint = new Point(e.X, e.Y + yOffset);
  39.             }
  40.         }
  41.  
  42.         protected override void OnMouseMove(MouseEventArgs e)
  43.         {
  44.             base.OnMouseMove(e);
  45.  
  46.             if(e.Button == MouseButtons.Left && Items.Count > 1)
  47.             {
  48.                 var vsp = GetScrollPos(Handle, Orientation.Vertical);
  49.                 var fh = Font.Height;
  50.                 var yoffset = fh * vsp;
  51.  
  52.                 var selRect = new Rectangle(Math.Min(StartPoint.X, e.Location.X),
  53.                                            Math.Min(StartPoint.Y - yoffset, e.Location.Y),
  54.                                            Math.Abs(e.Location.X - StartPoint.X),
  55.                                            Math.Abs(e.Location.Y - StartPoint.Y + yoffset));
  56.  
  57.                 var cr = ClientRectangle;
  58.  
  59.                 //Toggle...
  60.                 foreach (var item in Items.Cast<ListViewItem>()
  61.                     .Where(x => x.Bounds.IntersectsWith(cr)))
  62.                     item.Selected = selRect.IntersectsWith(item.Bounds);
  63.  
  64.                 //Scroll if needed...
  65.                 var p = PointToClient(Cursor.Position);
  66.                 var lvi = GetItemAt(p.X, p.Y);
  67.  
  68.                 if (lvi == null) return;
  69.  
  70.                 if (lvi.Index > 0 && (p.Y - lvi.Bounds.Height) <= fh)
  71.                     Items[lvi.Index - 1].EnsureVisible();
  72.                 else if (lvi.Index < Items.Count - 1 && (p.Y + lvi.Bounds.Height) > (Height - fh))
  73.                     Items[lvi.Index + 1].EnsureVisible();
  74.             }
  75.         }
  76.  
  77.         #endregion
  78.  
  79.         #region win32
  80.  
  81.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  82.         private static extern int GetScrollPos(IntPtr hWnd, Orientation nBar);
  83.  
  84.         #endregion
  85.  
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement