如何阻止用户将点移动到pictureBox1边框之外?

时间:2012-03-29 16:58:24

标签: c#

这是我Form1中的所有事件和功能,我该怎么办? 我需要检测pictureBox1控件的边框/边界,并在触摸边框时停止鼠标移动。

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                label1.Text = e.X.ToString();
                label2.Text = e.Y.ToString();
                label1.Visible = true;
                label2.Visible = true;
                label3.Visible = true;
                label4.Visible = true;
                // find the index that is closest to the current mouse location
                MinDist = float.MaxValue;

                for (idx = 0; idx < Point_X.Count; ++idx)
                {
                    float dx = Point_X[idx] - e.X;
                    float dy = Point_Y[idx] - e.Y;
                    float dist = (float)Math.Sqrt(dx * dx + dy * dy);

                    if (dist < MinDist)
                    {
                        MinDist = dist;
                        selectedIndex = idx;
                    }
                }



                if (MinDist < 5)
                {
                    mouseMove = true;
                    OriginalX = Point_X[(int)selectedIndex];
                    OriginalY = Point_Y[(int)selectedIndex];




                    if (cyclicSelectedIndex.Count() == 2)
                    {
                        cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
                        currentCyclicIndex++;
                        if (currentCyclicIndex > 1)
                        {
                            currentCyclicIndex = 0;
                        }
                        if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) || (cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))
                        {
                            button2.Enabled = false;
                        }
                        else
                        {
                            button2.Enabled = true;
                        }


                        label13.Text = selectedIndex.ToString();
                        label13.Visible = true;
                        label14.Visible = true;

                        listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
                    }

                }







            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseMove == true)
            {
                Point NewPoint = e.Location;
                Point_X[(int)selectedIndex] = NewPoint.X;
                Point_Y[(int)selectedIndex] = NewPoint.Y;
                label1.Text = NewPoint.X.ToString();
                label2.Text = NewPoint.Y.ToString();
                pictureBox1.Refresh();

                Point_X[(int)selectedIndex] = Math.Max(pictureBox1.Left, Math.Min(pictureBox1.Right, NewPoint.X));

            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mouseMove == true)
            {
                Point NewPoint = e.Location;
                Point_X[(int)selectedIndex] = NewPoint.X;
                Point_Y[(int)selectedIndex] = NewPoint.Y;
                mouseMove = false;
            }

        }      

        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);
            addPoint(halfX + offsetX, halfY + offsetY);
            pictureBox1.Refresh();
            numberOfPoints++;
            label16.Text = numberOfPoints.ToString();
            label16.Visible = true;
            label15.Visible = true;


        }

        private void addPoint(float x , float y)
        {
            Point_X.Add(x);
            Point_Y.Add(y);
            label5.Text = x.ToString();
            label6.Text = y.ToString();
            label5.Visible = true;
            label6.Visible = true;
            label7.Visible = true;
            label8.Visible = 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 < Point_X.Count; ++idx)
               {
                   Point dPoint = new Point((int)Point_X[idx], (int)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);
               }

               for (int i = 0; i < connectionStart.Count; i++)
               {

                   int startIndex = connectionStart[i];
                   int endIndex = connectionEnd[i];



                   connectionPointStart = new Point((int)Point_X[startIndex], (int)Point_Y[startIndex]);
                   connectionPointEnd = new Point((int)Point_X[endIndex], (int)Point_Y[endIndex]);
                   p.Width = 4;
                   g.DrawLine(p, connectionPointStart, connectionPointEnd);

               }


        }

private void button2_Click(object sender, EventArgs e)
        {
            addConnection(cyclicSelectedIndex[0], cyclicSelectedIndex[1]);
            pictureBox1.Invalidate();
        }

        private void addConnection(int i, int j)
        {

            if (cyclicSelectedIndex[0] == -1)
            {
                return;
            }
            if (cyclicSelectedIndex[0] == cyclicSelectedIndex[1])
            {
                return;
            }

            if (connectionStart.Count() == 0)
            {
                connectionStart.Add(i);
                connectionEnd.Add(j);
                //label12.Text = i.ToString();
                //label11.Text = j.ToString();
                label12.Text = connectionStart[0].ToString();
                label11.Text = connectionEnd[0].ToString();
                label9.Visible = true;
                label10.Visible = true;
                label11.Visible = true;
                label12.Visible = true;
                return;
            }

            for (int f = 0; f < connectionStart.Count(); f++)
            {
                if ((connectionStart[f] == i && connectionEnd[f] == j) || (connectionStart[f] == j && connectionEnd[f] == i)) // this checking dosent work good !
                {
                    button2.Enabled = false;
                    return;
                }
                else
                {
                    label12.Text = connectionStart[f].ToString();
                    label11.Text = connectionEnd[f].ToString();
                    label9.Visible = true;
                    label10.Visible = true;
                    label11.Visible = true;
                    label12.Visible = true;
                }
            }

            connectionStart.Add(i);
            connectionEnd.Add(j);

        }

我不确定如何解释它。但我怎么能这样做呢?

感谢。

1 个答案:

答案 0 :(得分:0)

计算X值时,使用Math.Max和图像的左边界,以及Min在图像的右边界。对顶部和底部的Y值执行相同的操作。

Point_X[(int)selectedIndex] = Math.Max(imageLeft, Math.Min(imageRight, NewPoint.X));

我假设您可以确定图像的左/右/上/下?