繁体   English   中英

执行过程时 Gif 图像的动画不起作用

[英]Animation of a Gif image is not working when execute a process

我有一个 Delphi 项目,它由两种形式组成,即MainFormDialogForm 当我单击Button1 时DialogForm应该出现并保持在顶部,直到一个过程完成(该过程需要几秒钟才能完成)。

DialogForm包括一个Timage组件。 当我单击Button1以显示DialogForm 时,Gif 图像出现但没有动画。 这仅在进程开始时发生(没有动画工作的进程)。 这是什么原因以及如何在关闭DialogForm之前保持动画?

procedure TMainForm.Button1Click(Sender: TObject);
var
  gif: TGIFImage;
begin
  Enabled:=false;
  try
        DialogForm.Show;
        DialogForm.Refresh;

        // The process is:
         ...
        ipcAES1.Encrypt;//where ipcAES is part of the IPWorks Encrypt library
        RichEdit1.Text:=ipcAES1.OutputMessage;
    finally
        Enabled:= true;
        DialogForm.Close;
    end;

end;
//--------------------------------------- 
procedure TDialogForm.FormShow(Sender: TObject);
var
  gif: TGIFImage;
begin
  gif := TGIFImage.Create;
  gif.LoadFromFile('D:\preview.gif');
  gif.Animate := True;
    image1.Parent := Self;
    image1.Left := 0;
    image1.Top := 0;
    image1.width := 800;
    image1.height := 800;
    image1.Picture.Assign(gif);
    gif.Animate := True;
    gif.Free;  
end;

正如这个线程中的许多人所说,因为处理是在主线程中完成的,因此在此过程中不会更新UI。

为了确保在进程运行时更新 UI,让一个单独的线程进行处理:

procedure TForm1.Button1Click(Sender: TObject);
var
  aProcessingThread: TThread;
begin
  // First read all data needed by the process from UI controls (or other non-threadsafe parts)
  <data> := ...;

  // Then create a new (anonymous) thread with the code you need to run your process
  aProcessingThread := TThread.CreateAnonymousThread(
    procedure
    begin
      // create the objects you need to do the processing
      ipcAES1 := Txxx.Create;
      try
        // Set the data
        ipcAES1.<data> := <data>;

        // Execute the proces:
        // ...
        ipcAES1.Encrypt;

      finally
        // When the process is done, use 'Synchronize' to interact with the UI
        // again, so you can add the processed data to the RichtEdit and so on...
        TThread.Synchronize(nil,
          procedure
          begin
            // Now you can interact again with the UI
            RichEdit1.Text := ipcAES1.OutputMessage;
            Enabled:= true;
            DialogForm.Close;
          end);
        ipcAES1.Free;
      end;
    end);

  // The thread is now created, but not started/running, so you can now show
  // the dialog and then start the thread, at which point the ButtonClick event
  // exists, but the progress dialog is shown and the thread is running.
  Enabled := False;
  DialogForm.Show;
  aProcessingThread.Start;
end;

当然,这只是如何使用(匿名)线程在后台进行一些处理的基本示例。 请注意,您需要在线程内处理异常(尝试/除外)。

关于 TGifImage 加载的一个小技巧:只要在 uses 子句中包含Vcl.Imaging.GIFImg ,您就可以调用Picture.LoadfromFile来加载 gif。

procedure TForm1.FormShow(Sender: TObject);
begin
  image1.Picture.LoadFromFile('D:\preview.gif');

  image1.Parent := Self;
  image1.Left := 0;
  image1.Top := 0;
  image1.width := Image1.Picture.Width;
  image1.height := Image1.Picture.Height;

  (image1.Picture.Graphic as TGIFImage).Animate := True;
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM