C# 用鼠标移动矩形的错误

标签 c# winforms rectangles

我正在开发一个小应用程序,我在其中绘制矩形并用鼠标移动它们。

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

第一次点击和拖动总是将矩形放在 (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

MouseDownLocation 一定是问题所在。如果没有它的代码可以工作(矩形在鼠标坐标上移动),那意味着它有问题。

尝试过:

 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

MouseDownLocation.X = 850

MouseDownLocation.Y = 238

这意味着这种移动的区别是 x 只有 2,而 y 有 22!

编辑 4:设法解决它!

只需添加两行代码:

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

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

最佳答案

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

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

从技术上讲,您的第 2 次和第 4 次点击也会执行此操作,但由于您已经在角落里,所以它并不明显。

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

关于C# 用鼠标移动矩形的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38959148/

相关文章:

c# - 当源为空时如何防止 AutoMapper 初始化目标 JObject 成员

C# 类不公开

c# - 未将对象引用设置为 ComboBox 中对象的实例

c# - 将按钮添加到 DataGridView

java - Slick 变得非常慢但只能绘制矩形

c# - 从 c# 在 odoo PostgreSQL 数据库中插入记录并在 Odoo 中查看记录

c# - 如何使用 Action 过滤器在 asp.net mvc 中集中模型状态验证?

c# 表单最小化/最大化按钮不见了?

opencv - 无法从网络摄像头检测到形状

algorithm - 需要帮助合并一些矩形