【问题标题】:Manipulating drag and drop images in windows forms c#在windows窗体中操作拖放图像c#
【发布时间】:2013-07-05 13:18:29
【问题描述】:

我正在尝试编写一个程序,它允许用户将图像拖放到程序中,然后能够选择图像、移动它、调整它的大小、裁剪它等等。

到目前为止,我已经创建了一个由面板组成的 windows 窗体。用户可以将图片文件拖到面板上,当鼠标落下并在图片框中加载图像时,将在鼠标坐标处创建一个图片框。我可以用这种方式添加几张图片。

现在我想允许用户操作和移动他们放到面板中的图像。

我已尝试寻找解决方案,但似乎找不到我理解的答案。

非常感谢任何帮助..

这是我当前的代码

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



    private void panel1_DragDrop(object sender, DragEventArgs e)
    {
        String[] imagePaths = (String[])e.Data.GetData(DataFormats.FileDrop);
        foreach (string path in imagePaths)
        {
            Point point = panel1.PointToClient(Cursor.Position);

            PictureBox pb = new PictureBox();
            pb.ImageLocation = path;
            pb.Left = point.X;
            pb.Top = point.Y;

            panel1.Controls.Add(pb);

            //g.DrawImage(Image.FromFile(path), point);
        }

    }

【问题讨论】:

    标签: c# visual-studio user-interface picturebox


    【解决方案1】:

    您可以在用户最初单击时获取鼠标位置,然后在 PictureBox 的MouseMove 事件中跟踪鼠标位置。您可以将这些处理程序附加到多个 PictureBox。

    private int xPos;
    private int yPos;
    
    private void pb_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
           xPos = e.X;
           yPos = e.Y;
        }
    }
    
    private void pb_MouseMove(object sender, MouseEventArgs e)
    {
        PictureBox p = sender as PictureBox;
    
        if(p != null)
        {
            if (e.Button == MouseButtons.Left)
            {
                p.Top += (e.Y - yPos);
                p.Left +=  (e.X - xPos);
            }
        }
    }
    

    对于动态图片框,您可以像这样附加处理程序

    PictureBox dpb = new PictureBox();
    dpb.MouseDown += pb_MouseDown;
    dbp.MouseMove += pb_MouseMove;
    //fill the rest of the properties...
    

    【讨论】:

    • 如果在程序运行时正在生成图片框,我如何将这些处理程序附加到图片框?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2012-02-14
    相关资源
    最近更新 更多