【问题标题】:C# bug in moving rectangle with mouse用鼠标移动矩形的 C# 错误
【发布时间】:2016-08-15 16:35:52
【问题描述】:

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

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

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

rect.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。

【问题讨论】:

  • 请注意 Rectangle (除其他外)有一个不错的 Contains(Point) 函数!另请注意,Cursor.Position 位于屏幕坐标中,而不是您在 Mousexxxevents 中获得的相对坐标!两个方向都有转换函数:Control.PointToClient 和 Control.PointToScreen
  • 谢谢,@TaW 对您的评论。这就是为什么我把面板放在(0,0)上,所以我不必改变
  • 屏幕真的是屏幕,不是表格!最好把它做对,而不是扭曲你的布局;-)
  • 是的,你可能是对的 :) 我会这样做,谢谢 :)

标签: c# winforms rectangles


【解决方案1】:
rect.X = e.X - MouseDownLocation.X; 
rect.Y = e.Y - MouseDownLocation.Y;

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

从技术上讲,您的第 2 次和第 4 次点击也会这样做,但由于您已经在角落里,所以并不明显。

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

【讨论】:

  • 很抱歉,我好像没有复制/粘贴部分代码。 MouseDownLocation 在按下时给出光标的位置
  • @omicito 我认为这就是这种效果。我也对您对答案的编辑添加了一些想法。
  • 是的,我做了类似的事情,但现在我遇到了一些新问题。它在我的新编辑中。并感谢@Tyler
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多