【问题标题】:C# Drag and Drop from one Picture box into AnotherC# 从一个图片框拖放到另一个图片框
【发布时间】:2013-04-06 22:44:46
【问题描述】:

我正在使用 C# 在 Visual Studio 2012 中工作,我需要将一个图片框拖动到另一个图片框,基本上将目标图片框图像替换为拖动的图片框图像。

我该怎么做?

请具体说明,并尽量以最简单和最好的方式进行解释。 我对编程非常陌生,有点绝望,所以请耐心等待。

【问题讨论】:

标签: c# drag-and-drop picturebox


【解决方案1】:

拖放在 PictureBox 控件上是隐藏的。不知道为什么,它工作得很好。这里可能的指导是,对于用户来说,您可以将图像放在控件上并不明显。您必须对此做一些事情,至少将 BackColor 属性设置为非默认值,以便用户可以看到它。

无论如何,您需要在第一个图片框上实现 MouseDown 事件,以便您可以单击它并开始拖动:

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
        var img = pictureBox1.Image;
        if (img == null) return;
        if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move) {
            pictureBox1.Image = null;
        }
    }

我假设您想移动图像,如果需要复制,则进行调整。然后你必须在第二个图片框上实现 DragEnter 和 DragDrop 事件。由于属性是隐藏的,您应该在表单的构造函数中设置它们。像这样:

    public Form1() {
        InitializeComponent();
        pictureBox1.MouseDown += pictureBox1_MouseDown;
        pictureBox2.AllowDrop = true;
        pictureBox2.DragEnter += pictureBox2_DragEnter;
        pictureBox2.DragDrop += pictureBox2_DragDrop;
    }

    void pictureBox2_DragEnter(object sender, DragEventArgs e) {
        if (e.Data.GetDataPresent(DataFormats.Bitmap))
            e.Effect = DragDropEffects.Move;
    }

    void pictureBox2_DragDrop(object sender, DragEventArgs e) {
        var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
        pictureBox2.Image = bmp;
    }

这确实允许您将图像从另一个应用程序拖到框中。让我们称之为功能。如果您想禁止这样做,请使用 bool 标志。

【讨论】:

  • 如果(img == null)返回;这一行的回报是做什么的?对不起,这实际上是我第一次编程
  • 当用户点击没有图片的图片框时,它会阻止代码崩溃。
【解决方案2】:

汉斯的回答让我找到了正确的解决方案。该答案的问题在于将DoDragDrop 放入MouseDown 将阻止MouseClick 事件触发。

这是我的解决方案:

private void PictureBox_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        var pb = (PictureBox)sender;
        if (pb.BackgroundImage != null)
        {
            pb.DoDragDrop(pb, DragDropEffects.Move);
        }
    }
}

private void PictureBox_DragEnter (object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

private void PictureBox_DragDrop (object sender, DragEventArgs e)
{
    var target = (PictureBox)sender;
    if (e.Data.GetDataPresent(typeof(PictureBox)))
    {
        var source = (PictureBox)e.Data.GetData(typeof(PictureBox));
        if (source != target)
        {
            // You can swap the images out, replace the target image, etc.
            SwapImages(source, target);
        }
    }
}

我的GitHub 上的完整工作示例。

【讨论】:

    【解决方案3】:

    您可以使用鼠标进入和离开事件轻松完成此操作。例如,您有两个图片框pictureBox1 和pictureBox2。你想把图片从图片框1拖到图片框2上,像这样。

    private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
    {
        if (a == 1)
        {
            pictureBox1.Image = pictureBox2.Image;
            a = 0;
        }
    }
    
    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        a = 1;
    }
    

    其中“a”只是一个锁或钥匙,用于检查鼠标是否已进入我们要放置此图像的控件。希望它有所帮助,对我有用。

    【讨论】:

      【解决方案4】:
      1. 您不能在 PictureBox 上设置 AllowDrop...为整个表单设置它。

      代码片段

      Form1.AllowDrop = true;

      1. 使用 Form DragEnter、DragDrop 事件,即使您将其放在图片框上,它们也会起作用。

      private void Form1_DragEnter(object sender, DragEventArgs e)

      {

      e.Effect = DragDropEffects.Move;
      

      }

      private void Form1_DragDrop(object sender, DragEventArgs e)

      {

      int x = this.PointToClient(new Point(e.X, e.Y)).X;
      
      int y = this.PointToClient(new Point(e.X, e.Y)).Y;
      
      if(x >= pictureBox1.Location.X && x <= pictureBox1.Location.X + pictureBox1.Width && y >= pictureBox1.Location.Y && y <= pictureBox1.Location.Y + pictureBox1.Height)
      
      {
      
          string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
      
          pictureBox1.Image = Image.FromFile(files[0]);
      
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-12
        • 2013-11-05
        • 2022-01-02
        • 1970-01-01
        • 2023-03-20
        • 1970-01-01
        • 2013-11-06
        相关资源
        最近更新 更多