将listview上的数据拖拽到picturebox上,并且可以选择鼠标落下的位置为拖拽的位置,我们需要通过计算拖拽到picturebox上的数据大小,来实现放在想要放置的地方,代码该如何实现。 private void listView1_ItemDrag(object sender, ItemDragEventArgs e) { ListViewItem item = (ListViewItem)e.Item; string itemName = item.Text; listView1.DoDragDrop(itemName, DragDropEffects.Copy); }
时间: 2023-05-27 19:02:16 浏览: 134
listbox拖拽功能实现
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
var point = pictureBox1.PointToClient(new Point(e.X, e.Y));
string itemName = (string)e.Data.GetData(DataFormats.Text);
ListViewItem item = listView1.FindItemWithText(itemName);
if (item != null)
{
long fileSize = new FileInfo(itemName).Length;
int width = Math.Min(pictureBox1.Width, Math.Max(10, (int)(fileSize / 1024)));
int height = Math.Min(pictureBox1.Height, Math.Max(10, (int)(fileSize / 1024)));
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawString(itemName, new Font("Arial", 10), Brushes.Black, new RectangleF(0, 0, width, height), new StringFormat { Alignment = StringAlignment.Center });
}
point.X = Math.Min(point.X - width / 2, pictureBox1.Width - width);
point.Y = Math.Min(point.Y - height / 2, pictureBox1.Height - height);
point.X = Math.Max(point.X, 0);
point.Y = Math.Max(point.Y, 0);
pictureBox1.BackgroundImage = bmp;
pictureBox1.Location = point;
}
}
阅读全文