つまり、ユーザーがマウス ポインターを pictureBox1 クライアント領域の外に移動しているときにのみ、マウスを離すイベントをアクティブにしたいということです。これは、ユーザーがマウスの左ボタンを押した場合にのみ発生しますが、ユーザーがマウスの左ボタンを押していない場合は発生しません。つまり、ユーザーはマウスを自由に動かすことができ、イベントは何もしません。
Form1 の上部に、mouseLeave 型 bool という変数があります。
コンストラクターでは、それを false にしました。
pictureBox1 マウス ダウン イベントで、mouseLeave 変数を true にしました。
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label1.Visible = true;
label4.Visible = true;
// find the index that is closest to the current mouse location
float t = wireObject1.GetIndexByXY(e.X, e.Y, 5);
if (t == -1)
{
button3.Enabled = false;
}
else
{
button3.Enabled = true;
{
selectedIndex = t;
mouseMove = true;
mouseLeave = true;
pictureBox1 マウス移動イベントで、mouseMove が true かどうかを確認してから、ポイントをドラッグして移動します。
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
Point NewPoint = e.Location;
{
wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !
label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
pictureBox1.Refresh();
}
}
else
{
label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
}
}
そのため、ユーザーがマウスの左ボタンをクリックしたままポイントをドラッグすると、ポイントが pictureBox1 クライアント領域内を移動します。
今、pictureBox1 のマウスを離すイベントで、私は次のことを行いました:
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if (mouseLeave == true)
{
mouseMove = false;
}
}
しかし、それは機能しません。ポイントドラッグを追加して移動しますが、このイベントは、マウスの左ボタンをクリックしていないときにのみ、ポイントをドラッグせずにマウスポインターをpictureBox1領域から移動したときにのみアクティブになります。
私が欲しいのは、マウスの左ボタンをクリックしてマウスの移動イベントのように移動したときだけ、このままにするイベントがこの場合に何かを行い、mouseMove を false にすることです。
そのため、ユーザーはポイントを pictureBox1 領域の外にドラッグできません。では、マウスを離すイベントで何をすべきですか?
編集済み**
これは、pictureBox1 領域に新しいポイントを毎回追加するボタン 1 クリックです。そして、ポイントを描画するペイントイベント。
おそらく、これは、pictureBox の境界外にドラッグしたときに停止するが適切な場所にないという問題を解決するのに役立ちます。
private void button1_Click(object sender, EventArgs e)
{
halfX = pictureBox1.ClientRectangle.Width / 2;
halfY = pictureBox1.ClientRectangle.Height / 2;
Random rnd = new Random();
offsetX = rnd.Next(-10, 10);
offsetY = rnd.Next(-10, 10);
wireObject1.addPoint(halfX + offsetX, halfY + offsetY);
if (wireObjectCoordinates1 == null)
wireObjectCoordinates1 = new WireObjectCoordinates() { FrameNumber = currentFrameIndex };
wireObjectCoordinates1.Point_X.Add(halfX + offsetX);
wireObjectCoordinates1.Point_Y.Add(halfY + offsetY);
wireObjectAnimation1._coordinatesList.Add(wireObjectCoordinates1);
pictureBox1.Refresh();
numberOfPoints++;
label5.Text = "{X = " + (halfX + offsetX) + "}" + " " + "{Y = " + (halfY + offsetY) + "}";
label5.Visible = true;
label7.Visible = true;
label16.Text = numberOfPoints.ToString();
label16.Visible = true;
label15.Visible = true;
buttonLockMode = true;
button8.Enabled = true;
button4.Enabled = true;
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Point connectionPointStart;
Point connectionPointEnd;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
SolidBrush brush = new SolidBrush(Color.Red);
Pen p = new Pen(brush);
for (int idx = 0; idx < wireObject1._point_X.Count; ++idx)
{
Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]);
dPoint.X = dPoint.X - 5; // was - 2
dPoint.Y = dPoint.Y - 5; // was - 2
Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
g.FillEllipse(brush, rect);
// g.FillEllipse(brush, rect);
}
for (int i = 0; i < wireObject1._connectionstart.Count; i++)
{
int startIndex = wireObject1._connectionstart[i];
int endIndex = wireObject1._connectionend[i];
connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
p.Width = 4;
g.DrawLine(p, connectionPointStart, connectionPointEnd);
}
}
マウスの動きのifをこれに変更することで解決したと思います:
if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X + 5,NewPoint.Y) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X - 5,NewPoint.Y) ||
!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y + 5)) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y - 5)))
したがって、移動イベントは次のようになります。
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseMove == true)
{
mouseDrag = true;
Point NewPoint = e.Location;
if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X + 5,NewPoint.Y) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X - 5,NewPoint.Y) ||
!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y + 5)) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y - 5)))
{
if (mouseDrag)
{
mouseMove = false;
return;
}
}
{
wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !
label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
pictureBox1.Refresh();
}
}
else
{
label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
}
}