(转)在 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
微软BI技术交流群:316744959
武汉NET技术群:961108969
NET技术群:21386099
本人具有丰富的系统开发经验,承接系统开发,小程序,NET系统开发,BI开发,有需求联系微信手机:15010195887