支持LargeIcon、SmallIcon、Tile
#region ListView内部值的交换
private void listView6_ItemDrag(object sender, ItemDragEventArgs e)
{
listView6.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void listView6_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
private void listView6_DragLeave(object sender, EventArgs e)
{
listView6.InsertionMark.Index = -1;
}
private void listView6_DragOver(object sender, DragEventArgs e)
{
Point targetPoint = listView6.PointToClient(new Point(e.X, e.Y));
int targetIndex = listView6.InsertionMark.NearestIndex(targetPoint);
if (targetIndex > -1)
{
Rectangle itemBounds = listView6.GetItemRect(targetIndex);
if (targetPoint.X > itemBounds.Left + 10
&& targetPoint.X < itemBounds.Left + itemBounds.Width - 20
&& targetPoint.Y > itemBounds.Top + 10
&& targetPoint.Y < itemBounds.Top + itemBounds.Height - 20)
{
listView6.Items[targetIndex].BackColor = Color.Red;
//设置预期放置位置
listView6.InsertionMark.Index = targetIndex;
}
else
{
foreach (ListViewItem item in listView6.Items)
{
item.BackColor = Color.White;
}
}
}
else
{
foreach (ListViewItem item in listView6.Items)
{
item.BackColor = Color.White;
}
}
}
private void listView6_DragDrop(object sender, DragEventArgs e)
{
int targetIndex = listView6.InsertionMark.Index;
if (targetIndex == -1) { return; }
ListViewItem draggedItme = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
string message = "是否要将“" + draggedItme.Text + "”移至“" + listView6.Items[targetIndex].Text + "”";
if (MessageBox.Show(message, "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
string temp = "";
int imgIndex = 0;
//更换图标
imgIndex = listView6.Items[targetIndex].ImageIndex;
listView6.Items[targetIndex].ImageIndex = draggedItme.ImageIndex;
listView6.SelectedItems[0].ImageIndex = imgIndex;
//更换子项值
for (int i = 1; i < listView6.Items[targetIndex].SubItems.Count; i++)
{
temp = listView6.Items[targetIndex].SubItems[i].Text;
listView6.Items[targetIndex].SubItems[i].Text = draggedItme.SubItems[i].Text;
listView6.SelectedItems[0].SubItems[i].Text = temp;
}
}
//重置背景色
foreach (ListViewItem item in listView6.Items)
{
if (item.Index % 2 == 0) { item.BackColor = System.Drawing.Color.PaleGreen; }
else { item.BackColor = new Color(); }
}
listView6.InsertionMark.Index = -1;
}
#endregion