如何实现将listview中的值比如111,拖拽到picturebox上时,可以选择放置 的位置,即鼠标按下的位置。如果picturebox上已经有了被拖拽过去的数据比如222,实现将111可以放在222的前面或者后面,限制条件是一行最多3个数据,只采纳完整代码答案时间截止今天。
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
ListViewItem item = (ListViewItem)e.Item;
string itemName = item.Text;
listView1.DoDragDrop(itemName, DragDropEffects.Copy);
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
e.Effect = DragDropEffects.Copy;
} else
{ e.Effect = DragDropEffects.None; }
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.AllowDrop = true;
}
//判断是不是可以接收的类型
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effect = DragDropEffects.Copy;
} else
{ e.Effect = DragDropEffects.None; }
}
private List<string> list = new List<string>();
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
string itemName = (string)e.Data.GetData(DataFormats.Text);
list.Add(itemName);
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
for (int i = 0; i < list.Count; i++)
{
itemName = list[i];
int row = i / 3;
int col = i % 3;
g.DrawString(itemName, new Font("Arial", 15), Brushes.Red, new Point(10 + col * 50, 10 + row * 30));
//Graphics p = pictureBox1.CreateGraphics();
//Font font = new Font("Arila", 15);
}
pictureBox1.Image = bmp;
Point mousePosition = pictureBox1.PointToClient(new Point(e.X, e.Y));
}