我无法解决问题所以在调试后我最终决定写下面的内容。
为什么地点跳来跳去?! 147 86到294 212然后回到每个回调?
pic.MouseMove += my_MouseMove;
my_MouseMove(object sender, MouseEventArgs e)
Console.WriteLine("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y);
147 86 147 86
294 212 294 212
147 86 147 86
294 212 294 212
147 86 147 86
294 212 294 212
//This code expects e.X,Y to return the X,Y Relative to the parent control.
//It seems to be relative to mousedown which breaks this.
Point heroClick = new Point();
private void hero_MouseDown(object sender, MouseEventArgs e)
{
heroClick.X = e.X;
heroClick.Y = e.Y;
}
private void hero_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Left)
return;
var xDif = e.X - heroClick.X;
var yDif = e.Y - heroClick.Y;
heroClick.X += xDif;
heroClick.Y += yDif;
lvl.playerX += xDif;
lvl.playerY += yDif;
PictureBox pic = ((PictureBox)sender);
pic.Left += xDif;
pic.Top+= yDif;
//Console.WriteLine("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y, sender);
textBox2.Text = string.Format("{0} {1} {2} {3}", e.X, e.Y, e.Location.X, e.Location.Y, sender);
textBox2.Update();
}
答案 0 :(得分:2)
发布的代码不足以猜测可能会发生什么。我将从线程主题“移动picBox”中提示。如果使用MouseMove事件移动控件,则更改控件相对于鼠标的位置。即使您没有移动鼠标,下一个报告的位置也会有所不同。由于鼠标位置是相对于控件客户区左上角的报告。
效果通常非常有趣,控制器可以快速前后跳跃。你需要补偿这一点,通常是通过而不是更新存储的前一个鼠标位置。在没有看到任何相关代码的情况下,我无法给出更好的建议。