继续拖拽222显示到picturebox上,让两个数值都显示在picturebox背景上

img


在把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;



        }




    }

}

怎么又问了一遍?已经说的很清楚了啊,你如果不记录数据,图片本身是不会知道你已经放了多少内容的,你得自己记录

打个比方,厨师只管做饭,具体谁坐哪,各自吃了什么,厨师才不关心,得服务员处理,你加个记录就是干服务员的活,画布是干厨师的活

        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];
                g.DrawString(itemName, new Font("Arial", 10), Brushes.Red, new Point(10, 10 + i * 24));
            }
            //g.DrawString(itemName, new Font("Arial", 10), Brushes.Red, new Point(10,10));
            pictureBox1.Image = bmp;
        }

img

该回答引用GPTᴼᴾᴱᴺᴬᴵ
是的,如果要同时显示多个数值并能拖拽改变它们的位置,可以在pictureBox上建立一个集合或数组来存储每个数值的位置信息,然后在DragDrop事件中将新的数值添加到集合或数组中,并在画布上绘制出所有数值。

以下是示例代码,其中使用一个List来存储数值的位置信息:

public partial class Form1 : Form
{
    private List<Point> pointList = new List<Point>(); // 存储数值位置信息的List

    public Form1()
    {
        InitializeComponent();
        this.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);

        Point point = new Point(e.X, e.Y); // 新数值的位置信息
        pointList.Add(point); // 将位置信息添加到List中

        DrawPoints(); // 重新绘制所有数值

        pictureBox1.Invalidate(); // 刷新pictureBox
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        DrawPoints(); // 绘制数值
    }

    private void DrawPoints()
    {
        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        Graphics g = Graphics.FromImage(bmp);

        // 绘制每个数值
        foreach (Point point in pointList)
        {
            g.DrawString(point.X.ToString() + ", " + point.Y.ToString(), new Font("Arial", 10), Brushes.Red, point);
        }

        pictureBox1.Image = bmp;
    }

}


在这个示例中,我们使用了一个List来存储所有数值的位置信息。在DragDrop事件中,我们将新的数值的位置信息添加到List中,并在pictureBox上重新绘制所有数值。我们还添加了一个Paint事件,在该事件中绘制所有数值。在绘制过程中,我们使用Graphics.DrawString方法在画布上绘制出每个数值。最后,我们在DragDrop事件中刷新pictureBox。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^