在 ListViewItem 上拖动进行框选

public partial class Form1 : Form { private bool IsMouseDown = false; Rectangle MouseRect = Rectangle.Empty; public Form1() { InitializeComponent(); } private void listView1_MouseDown(object sender, MouseEventArgs e) { IsMouseDown = true; DrawStart(e.Location); } private void listView1_MouseMove(object sender, MouseEventArgs e) { if (IsMouseDown) { ResizeToRectangle(e.Location); ListViewItem item = listView1.HitTest(e.X, e.Y).Item; if (item != null) { item.Selected = true; } foreach (ListViewItem listViewItem in listView1.SelectedItems) { if ((listViewItem.Bounds.Top > MouseRect.Top && listViewItem.Bounds.Bottom > MouseRect.Top) || (listViewItem.Bounds.Bottom < MouseRect.Bottom && listViewItem.Bounds.Top < MouseRect.Bottom)) { listViewItem.Selected = false; } } } } private void listView1_MouseUp(object sender, MouseEventArgs e) { this.Capture = false; Cursor.Clip = Rectangle.Empty; IsMouseDown = false; DrawRectangle(); MouseRect = Rectangle.Empty; } private void ResizeToRectangle(Point p) { DrawRectangle(); MouseRect.Width = p.X - MouseRect.Left; MouseRect.Height = p.Y - MouseRect.Top; DrawRectangle(); } private void DrawRectangle() { Rectangle rect = listView1.RectangleToScreen(MouseRect); ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed); } private void DrawStart(Point StartPoint) { this.Capture = true; Cursor.Clip = listView1.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height)); MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0); } }

转自: http://social.msdn.microsoft.com/Forums/silverlight/zh-CN/5685ef2c-7cfa-4f03-822c-4992bf6bce1c/-listviewitem-?forum=visualcshartzhchs

你可能感兴趣的:(ListView)