我试图通过改变图片框的填充来用鼠标在图片框内移动图像。我能够移动图像,但它moves too much。这是我到目前为止所得到的:
private bool mouseDown;
private Point lastLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDown = true;
lastLocation = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown == true)
{
int dx = e.X - lastLocation.X;
int dy = e.Y - lastLocation.Y;
pictureBox1.Padding = new Padding(pictureBox1.Padding.Left + dx,
pictureBox1.Padding.Top + dy, 0, 0);
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
答案 0 :(得分:1)
在帮助下,我找到了解决方案:
在MouseMove下,我替换了
pictureBox1.Padding = new Padding(pictureBox1.Padding.Left + dx,
pictureBox1.Padding.Top + dy, 0, 0);
与
pictureBox1.Padding = new Padding(Padding.Left + dx, Padding.Top + dy, 0, 0);
答案 1 :(得分:0)
我认为这是因为您在添加更多填充时通过引用当前填充每次添加更多填充:
pictureBox1.Padding = new Padding(pictureBox1.Padding.Left + dx,
pictureBox1.Padding.Top + dy, 0, 0);
尝试改为:
pictureBox1.Padding = new Padding(dx, dy, 0, 0);