我正在一個小應用程序中繪製矩形並用鼠標移動它們。用鼠標移動矩形的C#bug
我有一些邏輯裏面有一些錯誤,我找不到它們。
首先單擊並拖動總是穿繞位置(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;
它工作得更好,但我有一個問題。
當我點擊時,矩形會移動,以便矩形的左上角位於鼠標位置。
我想讓它保持在相同的位置,除非我移動它。
EDIT 2
我改變了部分代碼:
rect.X = rect.X + e.X - MouseDownLocation.X;
rect.Y = rect.Y + e.Y - MouseDownLocation.Y;
但是,現在的矩形移動太快,它會比鼠標要快得多。
這可能是定時器的問題嗎?
EDIT 3
MouseDownLocatioon必須的問題。如果沒有它的代碼有效(矩形在鼠標座標上移動),那意味着有什麼問題。
試圖這樣做:
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
的eX = 848
e。Y = 216
MouseDownLocation.X = 850
MouseDownLocation.Y = 238
這意味着對於這種舉動差異僅是爲了×2,和y的22!
編輯4:管理解決它!
只需要添加兩行代碼:
MouseDownLocation.X = e.X;
MouseDownLocation.Y = e.Y;
因爲,如果我按住鼠標按鈕,它不會刷新新MouseDownLocation。
請注意,矩形有(除其他外)一個很好的包含(點)功能!還要注意Cursor.Position是在屏幕座標中,而不是在Mousexxxevents中獲得的相對值!有兩個方向的轉換功能:Control.PointToClient和Control.PointToScreen – TaW
感謝,@TaW在您的評論。這就是爲什麼我把面板(0,0),所以我不必改變 – omicito
屏幕真的是屏幕,而不是形式!更好的做它真的是正確的,而不是扭曲你的佈局;-) – TaW