停止拖动图片框c#

时间:2015-02-02 10:39:48

标签: c# image winforms picturebox

我有一个右边和左边的图片框和移动,但当它移动到图片框的左边或右边时出现在窗体的中间,如下图所示

这样 enter image description here

我想要的是当向左或向右移动时,当图片板的板位于相同的位置时,图像不能再移动,只能移动到另一个lado.Conforme下面的图像

enter image description here

按照脚本:

    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);                
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

  // 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;
  }