C# winforms中的拖放

C# winforms中的拖放,c#,drag-and-drop,C#,Drag And Drop,我需要将6幅图片(位于PictureBox中)拖动到6个回答面板中,一次一幅,如果将正确的图片拖动到正确的位置,则会在名为“score”的类中为分数添加1。下面有代码显示其中一个PCiTunes正在工作,但是表单滞后了很多,如果将图像拖到面板上方的任何位置,它将捕捉到位。有没有办法阻止这一切 namespace DragAndDropQuiz { 公共部分类表单2:表单 { 公共表格2 { 初始化组件 } private void pcBxLiverpool_MouseEnt

我需要将6幅图片(位于PictureBox中)拖动到6个回答面板中,一次一幅,如果将正确的图片拖动到正确的位置,则会在名为“score”的类中为分数添加1。下面有代码显示其中一个PCiTunes正在工作,但是表单滞后了很多,如果将图像拖到面板上方的任何位置,它将捕捉到位。有没有办法阻止这一切

namespace DragAndDropQuiz
{ 公共部分类表单2:表单 { 公共表格2 { 初始化组件

    }

    private void pcBxLiverpool_MouseEnter(object sender, EventArgs e)
    {
        this.Cursor = Cursors.Hand;
    }

    private void pcBxLiverpool_MouseLeave(object sender, EventArgs e)
    {
        this.Cursor = Cursors.Default;
    }

    private int xPos;
    private int yPos;

    private void pcBxGeneral_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            xPos = e.X;
            yPos = e.Y;
        }
    }

    private void pcBxGeneral_MouseMove(object sender, MouseEventArgs e)
    {
        if (sender != null && sender.GetType() == typeof(PictureBox))
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                ((PictureBox)sender).Top += (e.Y - yPos);
                ((PictureBox)sender).Left += (e.X - xPos);
                this.Refresh();
            }
        }
    }

    private void pcBxGeneral_MouseUp(object sender, MouseEventArgs e)
    {
        if (sender != null && sender.GetType() == typeof(PictureBox))
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                PictureBox answer = (PictureBox)sender;

                if (answer.Location.X < pnlAnswer.Location.X)
                {
                    if (answer.Location.X + answer.Width > pnlAnswer.Location.X)
                    {
                        if ((answer.Location.X + answer.Width) < pnlAnswer.Location.X + pnlAnswer.Width)
                        {
                            answer.Location = pnlAnswer.Location;
                        }
                    }
                }
                else if (answer.Location.X > pnlAnswer.Location.X)
                {
                    if (answer.Location.X < (pnlAnswer.Location.X + pnlAnswer.Width))
                    {
                        answer.Location = pnlAnswer.Location;
                    }
                }
            }
        }
    }

    private void Form2_Paint(object sender, PaintEventArgs e)
    {

    }


    private void groupBox2_Paint(object sender, PaintEventArgs e)
    {
       Graphics g = e.Graphics;
       g.DrawRectangle(new Pen(Color.Red), pnlAnswer.Location.X, pnlAnswer.Location.Y, pnlAnswer.Width, pnlAnswer.Height);
    }





}

}

您好,欢迎来到SO。您应该制作一个完整的可复制示例,以增加您获得有价值答案的机会。groupBox2是什么?为什么每次重新绘制时都会在答案面板的顶部绘制一个矩形?您不能只留下答案面板吗?为什么连Y位置都不考虑,它是否无关?具体的代价是什么CTE行为?groupBox2正是所有项目的所在。绘制矩形纯粹是为了设计目的,我无法更改面板上的边框。我希望能够将图片框中的图像拖动到s面板上,并将其捕捉到位。这将是6幅图像的6倍。您正在重新发明Microsoft alr提供的拖放代码已经为你编码了,看到了吗