这是Windows窗体'private:'部分中的C ++代码。我希望“head”控件(它是一个图片框)沿最后一个按下的向下箭头键的方向移动。因此,我添加了一个计时器(tmrStep)并将其移动到其tick事件中。在btnPause(始终具有焦点)的按键事件中,我根据按下哪个键设置了int方向。当代码运行时,Head控件只是向上移动,无论按键是什么,我知道Key down事件没有被触发,因为我尝试在按键事件中设置一个断点,但是在我按下按键后程序继续。 / p>
System::Void tmrStep_Tick(System::Object^ sender, System::EventArgs^ e)
{
if (direction == 1)
Head->Location = Point(Head->Location.X + 10, Head->Location.Y);
if (direction == 3)
Head->Location = Point(Head->Location.X - 10, Head->Location.Y);
if (direction == 0)
Head->Location = Point(Head->Location.X , Head->Location.Y - 10);
if (direction == 2)
Head->Location = Point(Head->Location.X , Head->Location.Y + 10);
}
System::Void btnPause_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e)
{
if (e->KeyCode == Keys::Down)
direction = 2;
if (e->KeyCode == Keys::Up)
direction = 0;
if (e->KeyCode == Keys::Left)
direction = 3;
if (e->KeyCode == Keys::Right)
direction = 1;
}
以防它是相关的,btnPause是一个按钮控件。