delphi - 如何在循环中同时显示图像

标签 delphi

我是Delphi的新手,仍然在学习。

我有一个程序将图像作为输入,并使用循环使图像向下移动到某些像素,在此过程中,我想显示图像。
我尝试使用“ Timer.Interval”方法在间隔之间显示图像,但是我确定我做错了。
有办法解决吗?如果没有,那么我对在进行某些迭代时如何同时显示图像的任何其他建议都持开放态度。

提前致谢。

procedure BlockSpawn(var Image1: TImage; var Timer1: TTimer);
begin
    Timer1.Enabled := True;
    WITH Image1 do begin
         repeat
               Timer1.Interval := 600;
               Top := Top + 66;
               Image1.Show;
         until (Top = (TForm1.Bottom - Height)); {repeat}
    end; {WITH}
end; {begin}


其背后的原因是,我将需要在每次迭代中多次运行此过程,并且需要用户查看同时显示给他们的图像。
就像在俄罗斯方块中一样,新块将如何始终从同一位置向下移动并不断重复直到发生特定事件。

最佳答案

解决此问题的正确方法是使用TTimer.OnTimer事件,例如:

procedure BlockSpawn(Image: TImage; Timer: TTimer);
begin
  Timer.Tag := NativeInt(Image);
  Timer.Interval := 600;
  Timer.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  Image: TImage;
begin
  Image := TImage(Timer1.Tag);
  Image.Top := Image.Top + 66;
  Image.Show;
  if Image.Top >= (Bottom - Image.Height) then
    Timer1.Enabled := False;
end;


但是请注意,这种方法使BlockSpawn()异步运行。如果您确实希望BlockSpawn()同步运行,请摆脱TTimer并改用Sleep()

procedure BlockSpawn(Image: TImage);
begin
  repeat
    Sleep(600);
    Image1.Top := Image1.Top + 66;
    Image1.Show;
    Form1.Update;
  until Image1.Top >= (Form1.Bottom - Image1.Height);
end;


但是,这种方法可能会使您的UI显得呆滞,并且对用户的响应速度较慢。

关于delphi - 如何在循环中同时显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58292509/

相关文章:

delphi - Control + Click功能在Delphi XE中不起作用

delphi - 使用不明确的参数类型重载 Delphi 方法会导致意外执行

delphi - 使用 Fastmm4 什么时候会出现虚拟内存问题?

delphi - 如何在设计时设置应用程序字体?

delphi - OpenGL Delphi中的渲染错误

Delphi 数组 : Variable Myvar might not been initialized

delphi - 强制 TMonthCalendar 显示用户定义的日期格式

windows - 在没有 WNetAddConnection2 的情况下使用 Delphi 映射网络驱动器

multithreading - 如何修复 TSparseArray<T>?

德尔福: Connect to yahoo web messenger with idhttp