Reputation: 113
What i mean is that i want to activate the mouse leave event only when the user is moving the mouse pointer out of the pictureBox1 client area. This happens only if he presses down the left mouse button but not if the user doesn't press the left mouse button, i.e he can move the mouse around free and the event wont do anything.
I have a variable in the top of Form1 called mouseLeave type bool.
In constructor i made it to be false;
In the pictureBox1 mouse down event i did the mouseLeave variable to be 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;
In the pictureBox1 mouse move event i check if mouseMove is true then move the point drag it around:
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 + "}";
}
}
So when the user click on the mouse left button without leave it and drag the point then the point will move aorund the pictureBox1 client area.
Now in the pictureBox1 mouse leave event i did:
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if (mouseLeave == true)
{
mouseMove = false;
}
}
But it dosent work. I add a point drag it move it around but this event activate do something only when i move the mouse pointer out of the pictureBox1 area without dragging the point only when im not clicking down the left mouse button.
What i want is that only when i click down the mouse left button and move it around like in the mouse move event only then this leave event will do something in this case will make mouseMove to be false.
So the user will not be able to drag the point out of the pictureBox1 area. What should i do in the mouse leave event then ?
EDITED**
This the button1 click where i add each time a new point to the pictureBox1 area. And the paint event where i draw the points.
Maybe this will help to solve the problem where it stop but not in the right places when drag out of bounds of 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);
}
}
I think i solved it by changing the if in the mouse move to this:
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)))
So the move event should look like this:
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 + "}";
}
}
Upvotes: 0
Views: 1766
Reputation: 54532
Instead of using the MouseLeave
event you can check to see if the Mouse Location is in your PictureBox's Client Rectangle in your MouseMove
Event, something like this.
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 + "}";
}
}
}
Upvotes: 0
Reputation: 874
Do something like this:
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);
}
Where "OnMouseMove" is not attached to pictureBox event by default.
Upvotes: 0