我有一个包含面板的表单。我们的想法是,当鼠标悬停在面板上时,它会显示 OpenHand 光标。然后,您可以拖动面板,使光标变为 Grab 光标。鼠标释放后, Grab 光标应更改回 OpenHand 光标。
不幸的是,我有一个问题,如果我拖动并释放我的面板(鼠标在面板范围内),最终(通常在第二或第三次) Grab 光标不会变回 OpenHand 光标。 即使将鼠标移出面板并重新进入并尝试拖动和释放,情况仍然如此。
这是我的Panel类:
public class MapPanel : Panel
{
private Point MouseDownLocation;
private Cursor OpenHand = new Cursor(Properties.Resources.hand.Handle);
private Cursor Grab = new Cursor(Properties.Resources.punch.Handle);
public MapPanel()
{
MouseDown += new MouseEventHandler(mapPanel_MouseDown);
MouseUp += new MouseEventHandler(mapPanel_MouseUp);
MouseMove += new MouseEventHandler(mapPanel_MouseMove);
}
private void mapPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor = Grab;
MouseDownLocation = e.Location;
}
}
private void mapPanel_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor = OpenHand;
}
}
private void mapPanel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Left = e.X + Left - MouseDownLocation.X;
Top = e.Y + Top - MouseDownLocation.Y;
}
}
}
有趣的是,如果我不包含 MouseMove 处理程序,那么 MouseUp Cursor = OpenHand; 语句就没有问题>方法仍然会被包含 MouseMove 处理程序调用。它只是没有生效。
希望有人可以对此有所了解。非常感谢任何帮助!