private void listView1_ItemDrag(object sender, ItemDragEventArgs e) { ListViewItem item = (ListViewItem)e.Item; string itemName = item.Text; listView1.DoDragDrop(itemName, DragDropEffects.Copy); }将listview中的值比如说111,拖拽到picturebox中显示出来后,继续拖拽listview的值222,使其可以选择放置的位置,即鼠标按下时在已经放置的数据111的前面,222就显示在111的前面,如果鼠标按下时在111的后面,222就显示在111的后面,代码该如何实现,是计算拖过去图片的大小来实现移动的吗
时间: 2023-05-14 17:07:28 浏览: 122
可以通过计算拖拽过去的图片的位置来实现移动。具体代码如下:
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
string itemName = (string)e.Data.GetData(typeof(string));
pictureBox1.Image = Image.FromFile(itemName);
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox1.Image != null)
{
int index = listView1.Items.IndexOfKey(pictureBox1.ImageLocation);
if (index >= 0)
{
if (e.Button == MouseButtons.Left)
{
listView1.DoDragDrop(listView1.Items[index], DragDropEffects.Move);
}
}
}
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
Point point = listView1.PointToClient(new Point(e.X, e.Y));
ListViewItem targetItem = listView1.GetItemAt(point.X, point.Y);
int targetIndex = targetItem == null ? listView1.Items.Count : targetItem.Index;
ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
int draggedIndex = draggedItem.Index;
if (targetIndex > draggedIndex) targetIndex--;
listView1.Items.RemoveAt(draggedIndex);
listView1.Items.Insert(targetIndex, draggedItem);
}
阅读全文