用键移动物体 - 对角线

时间:2013-09-01 17:00:08

标签: wpf keyboard wpf-controls key move

我需要移动一个物体,我可以将它向上和向下移动,但是在对角线上失败,这是我移动的方式。

if (e.Key == Key.Up && Canvas.GetTop(Good) > 31)
            {
                double top = Canvas.GetTop(Good);
                Canvas.SetTop(Good, top - 7);
            }
            if (e.Key == Key.Down && Canvas.GetTop(Good) < CanvasA.ActualHeight - 7 - Good.Height)
            {
                double down = Canvas.GetTop(Good);
                Canvas.SetTop(Good, down + 7);

            }
            if (e.Key == Key.Left && Canvas.GetLeft(Good) > -2)
            {
                double left = Canvas.GetLeft(Good);
                Canvas.SetLeft(Good, left - 7);

            }
            if (e.Key == Key.Right && Canvas.GetLeft(Good) < CanvasA.ActualWidth - Good.Width)
            {
                double right = Canvas.GetLeft(Good);
                Canvas.SetLeft(Good, right + 7);
            }
        }

    }

一切正常但无法点击两个按钮来移动对角线中的角色, 小于31的原因是因为我在WPF中做了菜单。

0 个答案:

没有答案