Recht simple Vorgehensweise (2 Panels und ein Timer mit kurzem Intervall):
Delphi-Quellcode:
type
TDirection = (drUp, drDown);
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Timer1: TTimer;
procedure Timer1Timer(Sender: TObject);
private
{ Private-Deklarationen }
procedure CheckButtons;
procedure MovePanel(Panel: TPanel; Direction: TDirection);
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm11 }
procedure TForm1.CheckButtons;
begin
if GetActiveWindow = self.Handle then
begin
if GetKeyState(Ord('W')) < 0 then
MovePanel(Panel1, drUp)
else if GetKeyState(Ord('S')) < 0 then
MovePanel(Panel1, drDown);
if GetKeyState(VK_UP) < 0 then
MovePanel(Panel2, drUp)
else if GetKeyState(VK_DOWN) < 0 then
MovePanel(Panel2, drDown);
end;
end;
procedure TForm1.MovePanel(Panel: TPanel; Direction: TDirection);
const
PixelsPerMove = 5;
begin
case Direction of
drDown:
if (Panel.Top + Panel.Height) < (ClientHeight - PixelsPerMove) then
Panel.Top := Panel.Top + PixelsPerMove
else
Panel.Top := ClientHeight - Panel.Height;
drUp:
if Panel.Top > PixelsPerMove then
Panel.Top := Panel.Top - PixelsPerMove
else
Panel.Top := 0;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
CheckButtons;
end;
Das geht bestimmt auch eleganter, aber so sollte es einfacher zu verstehen sein.