既然你说的是 VS2012,C#,但Elipse
我System.Windows.Forms
会冒险猜测你真的做了 File -> New Project -> WPF Application (因为它有一个 Ellipse control (primitive) off the bat)。
由于Elipse
无法获得焦点,您需要决定何时按下按键是有趣的,为此添加一个处理程序到窗口。这是一些示例 XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" KeyDown="Window_KeyDown">
<Grid>
<Ellipse x:Name="elipsy" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="100" Canvas.Left="241" Canvas.Top="119"/>
</Grid>
</Window>
在您的处理程序 ( Window_KeyDown
) 中,您可以执行以下操作:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.W)
{
Canvas.SetTop(elipsy, Canvas.GetTop(elipsy) - 1);
}
else if (e.Key == Key.A)
{
Canvas.SetLeft(elipsy, Canvas.GetLeft(elipsy) - 1);
}
else if (e.Key == Key.S)
{
Canvas.SetTop(elipsy, Canvas.GetTop(elipsy) + 1);
}
else if (e.Key == Key.D)
{
Canvas.SetLeft(elipsy, Canvas.GetLeft(elipsy) + 1);
}
}
这应该让你开始走上正轨。如果您真的希望 WinForms 大喊大叫,我们应该能够在那里找到类似的解决方案。