简体   繁体   English

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

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

I have a Delphi project that consists of two forms namely MainForm and DialogForm .我有一个 Delphi 项目,它由两种形式组成,即MainFormDialogForm When I click on Button1 , the DialogForm should appear and stay on top until a process complete (the process takes a few seconds to complete).当我单击Button1 时DialogForm应该出现并保持在顶部,直到一个过程完成(该过程需要几秒钟才能完成)。

The DialogForm includes a Timage component. DialogForm包括一个Timage组件。 When I click on the Button1 to show the DialogForm , the Gif image appears but without animation.当我单击Button1以显示DialogForm 时,Gif 图像出现但没有动画。 This happens only when the process starts (without the process the animation works).这仅在进程开始时发生(没有动画工作的进程)。 What is the reason for this and how to keep the animation until closing the DialogForm ?这是什么原因以及如何在关闭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;

As said by many in this thread, because the processing is done in the main thread, the UI is not updated during this process.正如这个线程中的许多人所说,因为处理是在主线程中完成的,因此在此过程中不会更新UI。

To make sure the UI is updated while the process is running, let a separate thread do the processing:为了确保在进程运行时更新 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;

Of course this only a basic example of how to use an (anonymous) thread to do some processing in the background.当然,这只是如何使用(匿名)线程在后台进行一些处理的基本示例。 Please note you need to handle Exceptions inside the thread (try/except).请注意,您需要在线程内处理异常(尝试/除外)。

A small tip regarding the TGifImage loading: you can just call Picture.LoadfromFile to load the gif as long as you include Vcl.Imaging.GIFImg in the uses clause.关于 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