using System;
using System.Windows.Forms;
class MyButton : Button
{
protected override bool IsInputKey(Keys keyData)
{
return true;
}
protected override void OnKeyDown(KeyEventArgs e)
{
Text = e.KeyCode.ToString(); // <--- 调试用,正式程序中这行可删除。
switch (e.KeyCode)
{
case Keys.W: case Keys.Up : Top -= 5; break;
case Keys.S: case Keys.Down : Top += 5; break;
case Keys.A: case Keys.Left : Left -= 5; break;
case Keys.D: case Keys.Right: Left += 5; break;
}
base.OnKeyDown(e);
}
}
class Form1 : Form
{
Form1()
{
MyButton btn1 = new MyButton();
btn1.Parent = this;
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
s\说个思路,可监听窗口的键盘事件,键盘方向键按下时往某个方向移动多少像素之类的。