私は矩形を描き、それらをマウスで動かす小さなアプリケーションに取り組んでいます。マウスで移動する矩形のバグ
私は内部にいくつかのバグがあり、それらを見つけることができないいくつかのロジックを持っています。
常には周りの位置(0,0)の上に長方形を置き、第二のクリックやドラッグがうまく動作します最初のクリックとドラッグ。
3回目のクリックで再び(0,0)が四角形になり、4回目のクリックで再びドラッグできます。
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));
}
}
}
私の動いているロジックはそれほどうまくないと思うので、誰かが何らかのアイデアをもたらすことを願っています。
EDIT: 私はちょうどそれが良い作品が、私は一つの問題を持っている
rect.X = e.X;
rect.Y = e.Y;
に
rect.X = e.X - MouseDownLocation.X;
rect.Y = e.Y - MouseDownLocation.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座標であまりにも大きなオフセットを作ります。
Iは、左から右へ、マウスの動きを(のみX座標)た、デバッグは私にこれを示した:= 500
rect.Y = 100
エクス= 848
rect.X
e。このような動きのためにその差は、Xのみ2、及びY 22のためのためであることを意味= 216
MouseDownLocation.X = 850
MouseDownLocation.Y = 238
Y!
編集4:それを解決するために管理!私はマウスボタンを押し続けた場合、それは新しいMouseDownLocationをリフレッシュしません、ので
MouseDownLocation.X = e.X;
MouseDownLocation.Y = e.Y;
:
ちょうど2行のコードを追加する必要があります。あなたが最初のボックスの移動を開始するとき
Rectangleには(特に)Contains(Point)関数があります。また、Cursor.PositionはScreen座標であり、Mousexxxeventsで取得する相対座標ではありません。両方向の変換機能があります:Control.PointToClientとControl.PointToScreen – TaW
ありがとうございます、あなたのコメントに@TaW。それで私はパネルを(0,0)に置いたので、変更する必要はありません – omicito
画面は実際には画面ではなくフォームです!あなたのレイアウトをひねるのではなく、本当にうまくやるほうがいいですか? – TaW