停止拖动图片框c#

Stop dragging picturebox c #

我有一个图片框,左右移动,但是当它移动到左边缘或右边缘时,图片框出现在表格的中间,如下图

这样

我想要的是当picturebox的棋盘在棋盘的相同位置时向左或向右移动形成图像不能移动更多,只是到另一个lado.Conforme下图

遵循脚本:

    private void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button==MouseButtons.Left)
        {
            x = e.X;
            //y = e.Y;
        }
    }

    private void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Left)
        {             
            imagemPictureBox.Left += (e.X - x);

            //imagemPictureBox.Top += (e.Y - y);                
        }
    }

尝试这样的事情:

  // the point in the PB where we grab it:
  Point mDown;

  void imagemPictureBox_MouseDown(object sender, MouseEventArgs e)
  {
      // grab the pb
      mDown = e.Location;
  }

  void imagemPictureBox_MouseMove(object sender, MouseEventArgs e)
  {
    // new position:
    int x = imagemPictureBox.Left + e.X - mDown.X;
    int y = imagemPictureBox.Top  + e.Y - mDown.Y;

    // limit to form size:
    x = Math.Min(Math.Max(x, 0), this.ClientSize.Width -  imagemPictureBox.Width);
    y = Math.Min(Math.Max(y, 0), this.ClientSize.Height - imagemPictureBox.Height);

    // move the pb:
    imagemPictureBox.Location = new Point(x, y);
  }

  void imagemPictureBox_MouseUp(object sender, MouseEventArgs e)
  {
      //release it (optional)
      mDown = Point.Empty;
  }