夏456 2023-03-18 08:57 采纳率: 85.7%
浏览 68
已结题

继续拖拽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;



        }




    }

}

  • 写回答

3条回答 默认 最新

  • 文盲老顾 WEB应用领新星创作者 2023-03-18 09:18
    关注

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

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

            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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月26日
  • 已采纳回答 3月18日
  • 修改了问题 3月18日
  • 创建了问题 3月18日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来