C# 用鼠标移动矩形的错误
C# bug in moving rectangle with mouse
我正在开发一个小应用程序,我在其中绘制矩形并用鼠标移动它们。
我有一些逻辑,里面有一些错误,但我找不到它们。
第一次点击并拖动始终将矩形放在 (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。
rect.X = e.X - MouseDownLocation.X;
rect.Y = e.Y - MouseDownLocation.Y;
我不确定MouseDownLocation
是什么,但是根据名称,当你第一次开始移动盒子时,当前位置(e.X,e.Y)
等于MouseDownLocation
,意思是你的数学计算结果为 (0,0)。这似乎就是你搬到角落的原因。
从技术上讲,您的第 2 次和第 4 次点击也是这样做的,但由于您已经在角落里了,所以并不明显。
编辑关于矩形现在将角移动到光标的编辑:
当您单击鼠标时,您需要计算并存储从矩形位置到鼠标的偏移量,然后在 MouseMove 事件处理程序中使用该偏移量。
我正在开发一个小应用程序,我在其中绘制矩形并用鼠标移动它们。
我有一些逻辑,里面有一些错误,但我找不到它们。
第一次点击并拖动始终将矩形放在 (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。
rect.X = e.X - MouseDownLocation.X;
rect.Y = e.Y - MouseDownLocation.Y;
我不确定MouseDownLocation
是什么,但是根据名称,当你第一次开始移动盒子时,当前位置(e.X,e.Y)
等于MouseDownLocation
,意思是你的数学计算结果为 (0,0)。这似乎就是你搬到角落的原因。
从技术上讲,您的第 2 次和第 4 次点击也是这样做的,但由于您已经在角落里了,所以并不明显。
编辑关于矩形现在将角移动到光标的编辑: 当您单击鼠标时,您需要计算并存储从矩形位置到鼠标的偏移量,然后在 MouseMove 事件处理程序中使用该偏移量。