实现效果:
知识运用:
Form类的KeyPreview属性 Location属性
public bool KeyPreview {get; set;} //获取或设置一个值 指示在将按键事件传递到具有焦点的控件前
//窗体是否接受此按键事件
实现代码:
private void Form1_KeyDown(object sender, KeyEventArgs e) { Point point = this.Location; switch (e.KeyData) { case Keys.Left: point.X -= 2; break; case Keys.Right: point.X += 2; break; case Keys.Up: point.Y -= 2; break; case Keys.Down: point.Y += 2; break; case Keys.Escape: this.Close(); break; default: break; } this.Location = point; }