我试图使c#事件益智游戏上的作业

本文关键字:益智游戏 作业 事件 | 更新日期: 2023-09-27 18:04:18

我让图片框移动了。但是当我点击下拉的图片框时,它会跳来跳去。有人能帮我吗?

namespace Move_Shapes
{
    public partial class Form1 : Form
    {
        int X = 0;
        int Y = 0;
        int mX = 0;
        int mY = 0;
        bool Move = false;
        public Form1()
        {
            InitializeComponent();
        }
        private void pic_TL_Click(object sender, EventArgs e)
        {
            //X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
            //Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
            //label1.Text = X.ToString();
            //label2.Text = Y.ToString();
            //X = MousePosition.X - this.Location.X - 8;
            //Y = MousePosition.Y - this.Location.Y - 30;
            //label3.Text = X.ToString();
            //label4.Text = Y.ToString();
        }
        private void pic_TL_MouseDown(object sender, MouseEventArgs e)
        {
            X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
            Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
            label1.Text = X.ToString();
            label2.Text = Y.ToString();
            Move = true;
        }
        private void pic_TL_MouseMove(object sender, MouseEventArgs e)
        {
            mX = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture
            mY = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y;
            if (Move)
            {
                pic_TL.Location = new Point(mX - X, mY - Y);
            }
        }
        private void pic_TL_MouseUp(object sender, MouseEventArgs e)
        {
            Move = false;
        }
    }
}

我试图使c#事件益智游戏上的作业

在MoseMove中,您可以将位置设置为当前鼠标位置与初始鼠标位置的差值。

pic_TL.Location = new Point(mX - X, mY - Y);

如果鼠标移动超过一个像素,图片将移动到左上角