用鼠标移动矩形时出现C#错误

本文关键字:错误 鼠标 移动 | 更新日期: 2023-09-27 18:02:24

我正在开发一个小应用程序,在这个应用程序中我可以绘制矩形并用鼠标四处移动。

我有一些逻辑,里面有一些错误,但我找不到它们。

第一次点击并拖动总是将矩形放置在(0,0(附近的位置,第二次点击并拖拽会很好地工作。

第三次点击会再次将矩形放在(0,0(上,第四次点击我可以再次拖动它。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Pr
{
    public partial class Form1 : Form
    {
        int save=0;
        Timer timer1 = new Timer();
        private Point MouseDownLocation;
        private List<Rectangle> rectangles; 
        public Form1()
        {
            InitializeComponent();
            rectangles = new List<Rectangle>();
            panel1.Paint += panel1_Paint;
            this.DoubleBuffered = true;
            timer1.Interval = 10;
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            Refresh();
        }
        public void PopulateTable(int x, int y)
        {
            rectangles.Add(new Rectangle (500, 10, x, y));
            panel1.Invalidate();
        }
        void panel1_Paint(object sender, PaintEventArgs e)
        {
            foreach( var rectangle in rectangles)
            {
                using (var b=new SolidBrush(Color.HotPink))
                {
                    e.Graphics.FillRectangle(b, rectangle);
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                for (var i = 0; i < rectangles.Count; i++)
                {
                    if (Cursor.Position.X >= rectangles[i].X  &&
                        Cursor.Position.X <= rectangles[i].X + rectangles[i].Width &&
                        Cursor.Position.Y >= rectangles[i].Y  &&
                        Cursor.Position.Y <= rectangles[i].Y + rectangles[i].Height)
                    {
                        save = i;
                        Rectangle rect = rectangles[save];
                        rect.X = e.X - MouseDownLocation.X; 
                        rect.Y = e.Y - MouseDownLocation.Y;
                        rectangles[save] = rect;
                        panel1.Invalidate();
                        break;
                    }
                }
            }
        }
        protected void panel1_OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                MouseDownLocation = e.Location;
        }
        private void panel2_Paint(object sender, PaintEventArgs e)
        {
        }
        private void label1_Click(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            PopulateTable(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
        }
    }
}

我打赌我的移动逻辑不太好,所以我希望有人能带来一些想法。

编辑:我刚换了

rect.X = e.X - MouseDownLocation.X; 
rect.Y = e.Y - MouseDownLocation.Y;

rect.X = e.X; 
rect.Y = e.Y;

它效果更好,但我有一个问题。

单击时,矩形移动,使矩形的左上角位于鼠标位置。

我想要它使矩形保持在相同的位置,除非我移动它。

编辑2

我将部分代码更改为:

rect.X = rect.X + e.X - MouseDownLocation.X;
rect.Y = rect.Y + e.Y - MouseDownLocation.Y;

但是,现在矩形移动得太快了,它比鼠标移动得快得多。

这可能是定时器的问题吗?

编辑3

MouseDownLocationon一定是问题所在。如果没有它的代码有效(矩形在鼠标坐标上移动(,那就意味着它有问题。

尝试做:

 protected void panel1_OnMouseDown(object sender, MouseEventArgs e)
       {
           if (e.Button == MouseButtons.Left)
               MouseDownLocation = panel1.PointToScreen(e.Location);
       }

没有帮助。

试着调试,看起来Y坐标有问题。它只是在Y坐标系中偏移过大。

我做了一个鼠标从左到右的移动(只有X坐标(,调试显示:

rect.X=500

矩形Y=100

e.X=848

e.Y=216

鼠标向下定位.X=850

鼠标向下定位。Y=238

这意味着这种移动的差异对于x只有2,对于y是22!

第4版:设法解决了它

只需添加两行代码:

MouseDownLocation.X = e.X;
MouseDownLocation.Y = e.Y;

因为,如果我按住鼠标按钮,它就不会刷新新的MouseDownLocation。

用鼠标移动矩形时出现C#错误

rect.X = e.X - MouseDownLocation.X; 
rect.Y = e.Y - MouseDownLocation.Y;

我不确定MouseDownLocation是什么,但根据名称,当你第一次开始移动盒子时,当前位置(e.X,e.Y)等于MouseDownLocation,这意味着你的数学运算为(0,0(。这似乎就是你移动到角落的原因。

从技术上讲,你是第二次和第四次点击也在这样做,但由于你已经在角落里了,这并不明显。

编辑关于矩形现在将角移动到光标的编辑:单击鼠标时,需要计算并存储从矩形位置到鼠标的偏移量,然后在MouseMove事件处理程序中使用该偏移量。