【问题标题】:Drag and drop picturebox disappears拖放图片框消失
【发布时间】:2013-10-25 07:22:33
【问题描述】:

我的代码有问题。我正在尝试在我的表单上拖放图片,但是当我移动选定的图片框时,当我离开组框时它会丢失。它就这样消失了。

public partial class Form1 : Form
{
    int x_offset = 0; // any better to do this without having a global variable?
    int y_offset = 0;

    PictureBox dpb = new PictureBox();
    public Form1()
    {
        InitializeComponent();

        this.WindowState = FormWindowState.Maximized;
        this.AllowDrop = true;
        this.pictureBox1.MouseDown += pictureBox1_MouseDown;
        this.pictureBox2.MouseDown += pictureBox2_MouseDown;
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        PictureBox me = (PictureBox)sender;
        x_offset = e.X;
        y_offset = e.Y;

    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            PictureBox me = (PictureBox)sender;
            me.Left = e.X + me.Left - x_offset;
            me.Top = e.Y + me.Top - y_offset;
        }
    }

【问题讨论】:

    标签: c# winforms drag-and-drop picturebox


    【解决方案1】:

    您的 PictureBox 正在被父级(即 GroupBox)剪裁。您可以修复层次结构(查看->其他窗口->文档大纲)。

    此外,通常最好使用标准拖放功能,如下所述:http://social.msdn.microsoft.com/Forums/en-US/92cad3ba-dd05-4aa9-ad44-411051407d57/drag-and-drop-picturebox-to-picturebox-in-c?forum=csharplanguage。这将处理所有拖放特殊情况。为了更改标准光标,请将 Cursor.Current 设置为光标,由 CreateCursor(myBitmap) 返回。注意:在某些情况下,CreateCursor 可能会失败,因此请确保提供标准游标的回退。

    【讨论】:

    • 欢迎来到 Stack Overflow!最好在您的答案中总结链接背后的内容,因此如果链接过时,它不会丢失。否则,你的第一个答案做得很好。
    【解决方案2】:

    您的PictureBox 有一个GroupBox 作为其Parent,在winforms 和许多其他UI 技术中,您不能在其父控件之外呈现子控件。在使用您的代码之前,您可能需要执行以下操作:

    pictureBox1.Parent = this;//set your Form as Parent of the pictureBox1
    pictureBox1.BringToFront();//Ensure your pictureBox1 is on top.
    

    如果您的要求是将pictureBox1GroupBox 拖放到另一个控件上,使其成为pictureBox1 的新Parent,您可以尝试以下代码:

        Point downPoint;
        //MouseDown event handler for your pictureBox1
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e){
            downPoint = e.Location;
            pictureBox1.Parent = this;
            pictureBox1.BringToFront();
        }
        //MouseMove event handler for your pictureBox1
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e){
            if (e.Button == MouseButtons.Left) {
                pictureBox1.Left += e.X - downPoint.X;
                pictureBox1.Top += e.Y - downPoint.Y;
            }
        }
        //MouseUp event handler for your pictureBox1
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
            Control c = GetChildAtPoint(new Point(pictureBox1.Left - 1, pictureBox1.Top));
            if (c == null) c = this;
            Point newLoc = c.PointToClient(pictureBox1.Parent.PointToScreen(pictureBox1.Location));
            pictureBox1.Parent = c;
            pictureBox1.Location = newLoc;
        }
    

    【讨论】:

    • 非常感谢您的信息,但是我需要将选定的图片框留在其位置,只需拖放图像的副本。所以我的问题是我怎样才能离开原来的图片框,只拿一个我要拖动的副本。亲切的问候:)
    • @ToshkoKosev 你的问题和你的代码显示你想移动你的pictureBox,你甚至没有提到任何与你在评论中所说的内容相关的内容.
    猜你喜欢
    • 2023-03-20
    • 2013-11-05
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多