How to make the
Image1 object move through the form to the right by pressing the D button. Also to keep this button the object moved, releasing stopped. Please give the answer normal, with the code. I am a beginner and do not understand something like "use that property."
How to make the
- Make sure that the form properties are displayed in the Object Inspector.
- In the Property inspector, click the Events tab and find the
- Double click on the text field in this line. Delphi will generate an event handler code.
Paste this fragment as a handler:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = Ord('D') then Shape1.Left := Shape1.Left + 1; end;
My version is the following: A timer was thrown on the form and image (the picture of which you need to manage), double-click on the timer to open the code to write the event.
we insert into it
procedure TForm1.Timer1Timer(Sender: TObject); begin if (GetKeyState (ord('D')) <0) or (GetKeyState (ord('d')) <0) then Image1.left:=Image1.left+1; if (GetKeyState (ord('S')) <0) or (GetKeyState (ord('s')) <0) then Image1.left:=Image1.left-1; if (GetKeyState (ord('E')) <0) or (GetKeyState (ord('e')) <0) then Image1.Top:=Image1.Top-1; if (GetKeyState (ord('X')) <0) or (GetKeyState (ord('x')) <0) then Image1.Top:=Image1.Top+1; image1.Repaint; end;
We get control of the picture with the SDEX keys. Moreover, you can clamp 2 keys at the same time and we will move diagonally.
We throw the ApplicationEvents component on the form (the Additional tab), in the OnMessage event handler we write:
if Msg.message = WM_KEYDOWN then case Msg.wParam of 65: Image1.Left:=Image1.Left -1; 68: Image1.Left:=Image1.Left +1; 87: Image1.top:=Image1.top -1; 83: Image1.top:=Image1.top +1; end;
From myself I will add only that in the very same events you need to find
Form.Create , and inside to write
Form1.DoubleBuffered := True;
Then the picture also will not flash at offset.