简体   繁体   English

如果用户处于鼠标按下事件中,如何检查pictureBox1鼠标离开事件?

[英]How can i check on pictureBox1 mouse leave event if the user is in mouse down event?

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. 我的意思是,仅当用户将鼠标指针移出pictureBox1客户区域时,我才想激活鼠标离开事件。 This happens only if he presses down the left mouse button but not if the user doesn't press the left mouse button, ie 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. 我在Form1的顶部有一个名为mouseLeave类型bool的变量。

In constructor i made it to be false; 在构造函数中,我将其设置为假。

In the pictureBox1 mouse down event i did the mouseLeave variable to be true. 在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;

In the pictureBox1 mouse move event i check if mouseMove is true then move the point drag it around: 在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 + "}";
            }
        }

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. 因此,当用户单击鼠标左键而不离开它并拖动该点时,该点将在pictureBox1客户区周围移动。

Now in the pictureBox1 mouse leave event i did: 现在在pictureBox1鼠标离开事件中,我做了:

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. 我添加了一个点拖动它来移动它,但是仅当我将鼠标指针移出pictureBox1区域时才激活该事件,而仅当我不单击鼠标左键时才拖动该点。

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. 我想要的是,只有当我单击鼠标左键并像在mouse move事件中那样移动它时,此离开事件才会起作用,在这种情况下会使mouseMove为false。

So the user will not be able to drag the point out of the pictureBox1 area. 因此,用户将无法将该点拖出pictureBox1区域。 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. 这是我每次将新点添加到pictureBox1区域的button1单击。 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. 也许这将有助于解决问题,当它拖出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来解决此问题:

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 + "}";
            }
        }

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. 默认情况下,其中“ OnMouseMove”未附加到pictureBox事件。

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. 代替使用MouseLeave事件,您可以检查MouseMove事件中的Mouse Location是否位于PictureBox的客户端矩形中。

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 + "}";
        }
    } 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM