fanybupt

日出而作,日入而息,凿井而饮,耕田而食,帝力于我何有哉?

导航

函数结束时自动销毁的窗体

Posted on 2012-05-04 16:33  fanybupt  阅读(225)  评论(0)    收藏  举报

unit ProgressFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, jpeg;

type

  IProgress = interface
  ['{7AE77698-5497-44CD-8925-A33077147FD4}']
  end;

  TProgressForm = class(TForm)
    lblmsg: TLabel;
    imgWait: TImage;
  end;

  TMyProgress = class(TInterfacedObject, IProgress)
  private
    FProgressForm: TProgressForm;
    FWindowList: Pointer;
  public
    constructor Create(AMsg: string);
    destructor Destroy; override;
  end;

  function ShowProgress(AMsg: string): IProgress;
 
var
  ProgressForm: TProgressForm;

implementation

{$R *.dfm}

function ShowProgress(AMsg: string): IProgress;
begin
  Result := TMyProgress.Create(AMsg);
end;

var
  arrWnds: array of HWnd;

procedure DisableThreadWindows(Window: HWnd; Data: Longint); stdcall;
begin
  SetLength(arrWnds, Length(arrWnds) + 1);
  arrWnds[Length(arrWnds) -1] := Window;
  EnableWindow(Window, False);
end;

procedure EnableThreadWindows;
var
  I: Integer;
begin
  for I := 0 to Length(arrWnds) - 1 do
    EnableWindow(arrWnds[I], True);
  SetLength(arrWnds, 0);
end;

{ TMyProgress }

constructor TMyProgress.Create(AMsg: string);
begin
  EnumThreadWindows(GetCurrentThreadID, @DisableThreadWindows, 0);
  FProgressForm := TProgressForm.Create(nil);
  FProgressForm.lblmsg.Caption := AMsg;
  FProgressForm.lblmsg.Refresh;
  FProgressForm.imgWait.Refresh;
  FProgressForm.Show;
end;

destructor TMyProgress.Destroy;
begin
  EnableThreadWindows;
  FreeAndNil(FProgressForm);
  EnableTaskWindows(FWindowList);
  inherited;
end;

end.