夏456 2023-03-17 09:39 采纳率: 85.7%
浏览 35
已结题

如何将listview中的项拖拽显示到picturebox的背景中

img


想要将listview中的内容比如说111来拖拽到picturebox的背景上并且在该背景上显示111该如何实现?

  • 写回答

4条回答 默认 最新

  • 凉城┓.〆 2023-03-17 09:57
    关注

    参GPT:
    可以通过实现 ListView 控件的 ItemDrag 事件和 PictureBox 控件的 DragEnter 和 DragDrop 事件来实现将 ListView 中的项拖拽显示到 PictureBox 的背景中。
    具体的实现步骤如下:
    1、在 ListView 控件中添加需要拖拽的项,并开启 ListView 的 AllowItemDrag 属性。

    listView1.AllowDrop = true;
    listView1.AllowItemDrag = true;
    listView1.Items.Add("Item 1");
    listView1.Items.Add("Item 2");
    listView1.Items.Add("Item 3");
    

    2、在 ListView 控件的 ItemDrag 事件中设置被拖拽的项的数据格式、数据以及鼠标指针效果。

    private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
    {
        ListViewItem item = (ListViewItem)e.Item;
        string itemName = item.Text;
        listView1.DoDragDrop(itemName, DragDropEffects.Copy);
    }
    

    3、在 PictureBox 控件中设置允许拖拽操作,并实现 DragEnter 和 DragDrop 事件。

    pictureBox1.AllowDrop = true;
    pictureBox1.DragEnter += new DragEventHandler(pictureBox1_DragEnter);
    pictureBox1.DragDrop += new DragEventHandler(pictureBox1_DragDrop);
    

    4、在 PictureBox 控件的 DragEnter 事件中验证被拖拽的数据是否为文本格式。

    private void pictureBox1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            e.Effect = DragDropEffects.Copy;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    

    5、在 PictureBox 控件的 DragDrop 事件中获取被拖拽的文本数据,并将其设置为 PictureBox 的背景图片。

    private void pictureBox1_DragDrop(object sender, DragEventArgs e)
    {
        string itemName = (string)e.Data.GetData(DataFormats.Text);
        Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        Graphics g = Graphics.FromImage(bmp);
        g.DrawString(itemName, new Font("Arial", 16), Brushes.Blue, new Point(10, 10));
        pictureBox1.Image = bmp;
    }
    

    最终的效果是,在 ListView 控件中拖拽一个项并将其放置到 PictureBox 控件上时,PictureBox 的背景图片会显示为被拖拽的项的文本。需要注意的是,在 PictureBox 控件的 DragEnter 事件中要验证被拖拽的数据格式是否为文本格式,以保证只有文本数据才能被拖拽。同时,在 DragDrop 事件中需要将文本数据转换为图片,并将其设置为 PictureBox 的背景图片。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月17日
  • 创建了问题 3月17日

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题