For a programming exercise in Delphi I want to scroll an image across the screen 1 pixel at a time and repaint it each time I move it, so it looks like it is actually moving. At the moment Delphi just draws it at the start and end, so it isn't smooth. I tried Repaint and also Refresh, but neither worked. I also tried making it visible and invisible to see if that would work, but no luck.
image1.Visible := true;
for i := 0 to 50 do
begin
image1.Visible := false;
image1.Left := image1.Left + 1;
image1.Repaint(); // this doesn't work...
for j :开发者_运维技巧= 0 to 1000000 do
begin
k := i + j; // do nothing code
end;
image1.Visible := true;
end;
Does anyone know what I'm missing? Thanks.
A much better way to do this is to use a timer. In Delphi that means an object of class TTimer. Each time the timer fires you just increment the Left property. You probably want a counter that disables the timer once it has fired as many times as you desire.
procedure TForm1.FormClick(Sender: TObject);
var
i: integer;
begin
for i := 0 to 50 do
begin
image1.Left := image1.Left + 1;
Application.ProcessMessages;
sleep(100);
end;
end;
You could also do Image1.Update
instead of Application.ProcessMessages
but then the application would stop responding to Windows messages; hence, it would "freeze". Anyhow, this is a very ugly way to do an animation (very ugly!), but if it is just an exercise, then ...
精彩评论