private void listView1_ItemDrag(object sender, ItemDragEventArgs e) { ListViewItem item = (ListViewItem)e.Item; string itemName = item.Text; listView1.DoDragDrop(itemName, DragDropEffects.Copy); }如果将listview中的值拖拽到picturebox上并且使用drawstring的方法显示出来后,如何在picturebox上继续拖拽移动?代码该如何实现
时间: 2023-05-20 21:04:51 浏览: 105
您可以在picturebox的MouseMove事件中获取鼠标的位置,然后使用Graphics对象的DrawString方法将文本绘制到picturebox上。在picturebox的MouseDown事件中,您可以使用DoDragDrop方法开始拖拽操作。在picturebox的DragOver事件中,您可以更新拖拽操作的位置。最后,在picturebox的DragDrop事件中,您可以完成拖拽操作。以下是示例代码:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
string itemName = (string)pictureBox1.Tag;
if (!string.IsNullOrEmpty(itemName))
{
Graphics g = pictureBox1.CreateGraphics();
g.DrawString(itemName, Font, Brushes.Black, e.Location);
g.Dispose();
}
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
string itemName = (string)pictureBox1.Tag;
if (!string.IsNullOrEmpty(itemName))
{
pictureBox1.DoDragDrop(itemName, DragDropEffects.Copy);
}
}
}
private void pictureBox1_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
string itemName = (string)e.Data.GetData(typeof(string));
pictureBox1.Tag = itemName;
}
}
阅读全文