je peut bouger Panel avec Les touches fléchées
mais quand je mets d'autres composants sur la Form, le code sa ne marche pas !
Et Je ne comprends pas pourquoi Edit1 sera marqué !!!
Le code sa marche bien si il n y a que Panel1 sur la Form1 !
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
RadioGroup1: TRadioGroup;
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormKeyPress(Sender: TObject; var Key: Char);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key = 39) then
Panel1.Left:= Panel1.Left+1;
if (Key = 37) then
Panel1.Left:= Panel1.Left-1;
if (Key = 38) then
Panel1.Top:= Panel1.Top-1;
if (Key = 40) then
Panel1.Top:= Panel1.Top+1;
end;
end.
procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Interval := 100; // 100ms
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if getAsyncKeyState(VK_LEFT) < 0 then
begin
if panel1.Left > 0 then
Panel1.Left := Panel1.Left-1;
end
else
if getAsyncKeyState(VK_RIGHT) < 0 then
begin
if (panel1.Left+panel1.Width) < Form1.Width then
Panel1.Left := Panel1.Left+1;
end;
if getAsyncKeyState(VK_UP) < 0 then
begin
if panel1.Top > 0 then
Panel1.Top := Panel1.Top-1;
end
else
if getAsyncKeyState(VK_DOWN) < 0 then
begin
if (panel1.Top+panel1.Height) < Form1.Height then
Panel1.Top := Panel1.Top+1;
end;
end;
Il suffit d'utiliser la propriété refresh de ton TPanel.
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
With Panel1 Do
Begin
Case Key Of
39 : Left:=Left+1;
37 : Left:=Left-1;
38 : Top:=Top-1;
40 : Top:=Top+1;
End;
Refresh;
End;
end;
Salut,
utilise la méthode de francky23012301 FormKeyDown + la méthode de Mauricio mettre Form1.KeyPreview à True;
N'utilise pas FormKeyPress !
pour info dans le OnKeyPress se serait pour 39
if Key = Chr(vk_right) then
ou vk_left
Code pas testé
@+ yanb
le code sa marche si je fait un clique en TEdit
mais si je fait un clique sur panel sa ne marche pas !!!
c est a dire si je clique d'abord sur TEdit, sa marche !
procedure TForm3.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_LEFT then
begin
Panel1.Left := Panel1.Left - 1;
Key := 0;
end;
end;