What's the easiest way to write a please wait screen with Delphi?

前端 未结 6 474
感动是毒
感动是毒 2020-12-11 12:03

I just want a quick and dirty non-modal, non-closable screen that pops up and goes away to make 2 seconds seem more like... 1 second. Using 3-5 lines of code.

6条回答
  •  有刺的猬
    2020-12-11 12:59

    If your application is doing work and not processing any messages during this brief period, you can just do

    procedure TForm3.Button1Click(Sender: TObject);
    begin
      Form4.Show;
      try
        Sleep(2000);
      finally
        Form4.Hide;
      end;
    end;
    

    where Form4 is the "please wait" form (which is fsStayOnTop), and Sleep(2000) symbolizes the work done.

    Now, the best way to do things is in the background (maybe in a separate thread), or at least you should ProcessMessages once in a while in slow process. If you do the latter, the equivalent of Sleep(2000) will still not return until the process is complete, but you need to write

    procedure TForm4.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
      CanClose := false;
    end;
    

    in the "please wait" dialog so it cannot be closed (not even with Alt+F4).

    If you are using threads or something else more sophisticated, I think that I'll need more details in order to provide an appropriate answer.

提交回复
热议问题