我的意思是我只想在用户将鼠标指针移出pictureBox1客户区时才激活鼠标离开事件。只有当他按下鼠标左键时才会发生这种情况,但如果用户没有按下鼠标左键则不行,即他可以自由移动鼠标,事件不会做任何事情。
我在Form1的顶部有一个名为mouseLeave type bool的变量。
在构造函数中,我认为它是假的;
在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变为假。
因此用户将无法将该点拖出pictureBox1区域。我应该怎么做鼠标离开事件呢?
EDITED **
这次按钮1单击我每次向pictureBox1区域添加新点时添加的位置。 我绘制点的油漆事件。
当拖出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)))
所以move事件应该是这样的:
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 + "}";
}
}
答案 0 :(得分:0)
做这样的事情:
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.MouseMove -= OnMouseMove;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
pictureBox1.MouseMove += OnMouseMove;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
Debug.WriteLine(e.Location);
}
默认情况下,“OnMouseMove”未附加到pictureBox事件。
答案 1 :(得分:0)
您可以检查MouseLeave
事件中的鼠标位置是否位于PictureBox的客户端矩形中,而不是使用MouseMove
事件。
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
{
if (mouseMove == true)
{
Point NewPoint = e.Location;
if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint))
{
if (mouseLeave)
{
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 + "}";
}
}
}