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

前端 未结 6 573
感动是毒
感动是毒 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 13:02

    I usually add a form to the project, like this:

    dfm:

    object WaitForm: TWaitForm
      Left = 0
      Top = 0
      AlphaBlend = True
      AlphaBlendValue = 230
      BorderIcons = []
      BorderStyle = bsNone
      Caption = 'Please wait...'
      ClientHeight = 112
      ClientWidth = 226
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Tahoma'
      Font.Style = []
      OldCreateOrder = False
      Position = poMainFormCenter
      OnCloseQuery = FormCloseQuery
      PixelsPerInch = 96
      TextHeight = 13
      object Panel1: TPanel
        Left = 0
        Top = 0
        Width = 226
        Height = 112
        Align = alClient
        BevelInner = bvLowered
        Caption = 'Please wait...'
        Color = clSkyBlue
        ParentBackground = False
        TabOrder = 0
      end
    end
    

    while unit looks like this:

    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ExtCtrls, StdCtrls;
    
    type
      TWaitForm = class(TForm)
        Panel1: TPanel;
        procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
      private
        { Private declarations }
        FCanClose: Boolean;
      public
        { Public declarations }
        class function ShowWaitForm: TWaitForm;
        procedure AllowClose;
      end;
    
    var
      WaitForm: TWaitForm;
    
    implementation
    
    {$R *.dfm}
    
    { TWaitForm }
    
    procedure TWaitForm.AllowClose;
    begin
      FCanClose := True;
    end;
    
    procedure TWaitForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
      CanClose := FCanClose;
    end;
    
    class function TWaitForm.ShowWaitForm: TWaitForm;
    begin
      Result := Self.Create(Application);
      Result.Show;
      Result.Update;
    end;
    
    end.
    

    you call it like this:

    procedure TForm2.Button1Click(Sender: TObject);
    var
      I: Integer;
    begin
      with TWaitForm.ShowWaitForm do
        try
          for I := 1 to 100 do
            Sleep(30);
        finally
          AllowClose;
          Free;
        end;
    end;
    

    just an idea, refinements is up to you.

提交回复
热议问题