0

我想用鼠标移动一个图片框,所以我到了这里:

    private void pictureB_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(image, recLoc);
    }
    private void pictureB_MouseDown(object sender, MouseEventArgs e)
    {
        WorkAble = true;
        choosingPoint.X = e.X;
        choosingPoint.Y = e.Y; 
        lastPoint.X = e.X;
        lastPoint.Y = e.Y;
    }

    private void pictureB_MouseMove(object sender, MouseEventArgs e)
    {
        if (WorkAble)
        {
            recLoc.X = e.X - choosingPoint.X;// + lastPoint.X;
            recLoc.Y = e.Y - choosingPoint.Y;// + lastPoint.Y;
            pictureB.Refresh();
        }
    }

    private void pictureB_MouseUp(object sender, MouseEventArgs e)
    {
        WorkAble = false;
        lastPoint.X = e.X;
        lastPoint.Y = e.Y;
    }
    // recLoc = pictureBox Location.

好吧,效果很好..但并不完美..我的意思是一旦执行了 KeyUp 事件,然后再次单击图像将返回到图片框的 0、0 点。为了克服这个问题,我添加了 lastPoint 点,并在鼠标移动中添加了它的值。因此,一方面它确实在最后一个放置点绘制图像,但是鼠标将位于图像的 0、0 点上,而不是在我单击的位置 - 在图像上。就像我单击图像的中心一样,鼠标将位于 0、0 点上。

任何建议如何解决它?

4

2 回答 2

1

对“recLoc”进行增量更改以避免丢失原始位置:

private void pictureB_MouseMove(object sender, MouseEventArgs e)
{
    if (WorkAble)
    {
        recLoc.X = recLoc.X + e.X - choosingPoint.X;
        recLoc.Y = recLoc.Y + e.Y - choosingPoint.Y;
        choosingPoint = e.Location;
        pictureB.Invalidate();
    }
}
于 2013-01-25T13:02:54.677 回答
0
private void pictureB_MouseDown(object sender, MouseEventArgs e)
    {
        WorkAble = true;
        lastPoint.X = e.X;
        lastPoint.Y = e.Y;
    }

        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            mouseMove = false;
            lastPointX = e.X;
            lastPointY = e.Y;
        }


private void pictureB_MouseMove(object sender, MouseEventArgs e)
{
    if (WorkAble)
    {
        recLoc.X -= (lastPoint.X - e.X);
        recLoc.Y -= (lastPoint.X - e.X);
        pictureB.Refresh();
        choosingPoint = e.Location;
    }
}
于 2018-01-07T22:32:11.447 回答