在把listview中的值111拖拽到picturebox的背景上显示出来后,能否继续拖拽222显示到picturebox上,让两个数值都显示在picturebox背景上面,并且还可以继续拖拽改变两个数值的位置,要实现这个功能是要在picturebox上建立一个集合或者数组吗?接收来自listview中的值吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//listView1.AllowItemDrag = true;
this.pictureBox1.AllowDrop = true;
}
//创建一个接收listview的list
private void pictureBox1_Click(object sender, EventArgs e)
{
}
//将item传送走
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
// DoDragDrop(e.Item.ToString(), DragDropEffects.Copy);
ListViewItem item = (ListViewItem)e.Item;
string itemName = item.Text;
listView1.DoDragDrop(itemName, DragDropEffects.Copy);
}
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
//if (listView1.SelectedItems == null)
// return;
//ListViewItem selectedItem = listView1.SelectedItems[0];
//listView1.DoDragDrop(listView1.SelectedItems[0].Text, DragDropEffects.All);
////listView1.DoDragDrop(listView1.SelectedItems, 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)
{
//listView1.AllowItemDrag = true;
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 void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
string itemName = (string)e.Data.GetData(DataFormats.Text);
//把数据显示在picture
//Bitmap bmp;
//if (pictureBox1.Image != null)
//{
// bmp = new Bitmap(pictureBox1.Image);
//}
//else
//{
// bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//}
//Graphics g = Graphics.FromImage(bmp);
// 计算文本的位置
//int yOffset = bmp.GetPixel(0, 0).A == 0 ? 0 : bmp.Height;
////int x0ffset = bmp.GetPixel(0, 0).A == 0 ? 0 : bmp.Width;
//int lineHeight = new Font("Arial", 10).Height;
////int newX = x0ffset + lineHeight;
//int newY = yOffset + lineHeight;
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
List<string> list = new List<string>();
foreach (string itemNames in list)
{
g.DrawString(itemName, new Font("Arial", 10), Brushes.Red, new Point(10, 10));
}
//g.DrawString(itemName, new Font("Arial", 10), Brushes.Red, new Point(10,10));
pictureBox1.Image = bmp;
}
}
}