关于C#winform程序中listview控件LargeImageList模式下图标的拖放,本人想做出相似,在Windows桌面上不自动排列时,当用鼠标圈选几个图标后,拖到桌面上新的空白位置后松手,这几个图标也画在这个新位置了。这个需求本人查遍网络也没解决,希望这里的高手帮本人一下。若有满意答案,本人QQ或支付宝100刀红包。另外,在Delphi里有setPosition,可以设置item到一个新位置,貌似C#中没此功能。
解决方案
2
假如只是显示图标,可以在panel里放PictureBox,在panel的mouseDown、mouseUp、mouseMove事件里完成以上需求。
mouseDown时判断鼠标是在PictureBox上还是空白处,前者是拖动,后者是框选
。
。
mouseDown时判断鼠标是在PictureBox上还是空白处,前者是拖动,后者是框选
。
。
3
看错了,还带文字。
可以把PictureBox和Label放在一个panel里为一个元素,通过以上方法操作此类元素
可以把PictureBox和Label放在一个panel里为一个元素,通过以上方法操作此类元素
30
// 这个别忘了
// listView1.AllowDrop = true;
// listView1.AutoArrange = false;
private double getVector(Point pt1, Point pt2) // 获取两点间的距离
{
var x = Math.Pow((pt1.X - pt2.X), 2);
var y = Math.Pow((pt1.Y - pt2.Y), 2);
return Math.Abs(Math.Sqrt(x - y));
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(ListViewItem[])))
e.Effect = DragDropEffects.Move;
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(ListViewItem[])))
{
var items = e.Data.GetData(typeof(ListViewItem[])) as ListViewItem[];
var pos = listView1.PointToClient(new Point(e.X, e.Y));
var offset = new Point(pos.X - startPoint.X, pos.Y - startPoint.Y);
foreach (var item in items)
{
pos = item.Position;
pos.Offset(offset);
item.Position = pos;
}
}
}
Point startPoint = Point.Empty;
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
startPoint = e.Location;
}
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
if (listView1.SelectedItems.Count == 0)
return;
if (e.Button == MouseButtons.Left)
{
var vector = getVector(startPoint, e.Location);
if (vector < 50) return;
var data = listView1.SelectedItems.OfType<ListViewItem>().ToArray();
listView1.DoDragDrop(data, DragDropEffects.Move);
}
}