1

我有 aPictureBox在 a 内TabPage,当然这TabPage是 a 的一部分,TabViewTabView是在 a 内Form。我希望用户能够在标签页中移动这个图片框。为此,我使用图片框的MouseDown,MouseMoveMouseUp事件:

private void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e)
{
    if (!_mapPackageIsMoving)
    {
        _mapPackageIsMoving = true;
    }
} 

private void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e)
{
    if(_mapPackageIsMoving)
    {
        pictureBoxPackageView.Location = MousePosition; //This is not exact at all!
        return;
    }

    //Some other code for some other stuff when picturebox is not moving...
}

private void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e)
{
    if (_mapPackageIsMoving)
    {
        _mapPackageIsMoving = false; //Mouse button is up, end moving!
        return;
    }
}

但我的问题在于MouseMove事件。按下按钮后,只要我移动鼠标,图片框就会跳出标签页的可见区域。

我需要知道如何仅在选项卡页的矩形内处理移动,如果图片框被拖出选项卡视图的可见区域,除非用户将鼠标移到选项卡视图的可见矩形内,否则它不应再移动。

任何帮助/提示将被应用!

4

1 回答 1

3

您需要一个变量来保存 PictureBox 的原始位置:

根据HansPassant 的回答修改:

private Point start = Point.Empty;

void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e) {
  _mapPackageIsMoving = false;
}

void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e) {
  if (_mapPackageIsMoving) {
    pictureBoxPackageView.Location = new Point(
                             pictureBoxPackageView.Left + (e.X - start.X), 
                             pictureBoxPackageView.Top + (e.Y - start.Y));
  }
}

void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e) {
  start = e.Location;
  _mapPackageIsMoving = true;
}
于 2012-08-16T20:02:02.577 回答