如何使用dwsXPlatform.CollectFiles TCollectFileProgressEvent

时间:2017-12-05 09:07:51

标签: delphi dwscript

由于我是delphi中的新手,我正在努力研究如何将dwsXPlatform.TCollectFileProgressEvent与dwsXPlatform.CollectFiles结合使用。

在DWScript存储库中,没有样本甚至是测试代码。

type
        TForm1 = class(TForm)
                btn1: TButton;
                mmoDirList: TMemo;
                mmoOnCollectFiles: TMemo;
                procedure btn1Click(Sender: TObject);
        private
                OnCollectFileProgressEvent: TCollectFileProgressEvent;
        end;
{...}
procedure TForm1.btn1Click(Sender: TObject);
begin
        mmoDirList.Clear;
        CollectFiles('c:\MyDelphiFiles', '*.pas', mmoDirList.Lines, True, OnCollectFileProgressEvent);
end;

2 个答案:

答案 0 :(得分:1)

根据文档,TCollectFileProgressEvent如下:

TCollectFileProgressEvent = procedure (const directory : String; var skipScan : Boolean) of object;

让我们把它分成三部分:

1)TCollectFileProgressEvent

2)过程(const目录:String; var skipScan:Boolean)

3)对象

第一部分TCollectFileProgressEvent是事件类型的名称。在你的例子中,你不需要那些东西。

第二部分程序....是一个如何宣布事件的方法

对象"的第三部分"意味着您的程序需要放在课堂上。

让我给你看一些代码:

  TForm1 = class(TForm)
    btn1: TButton;
    mmoDirList: TMemo;
    mmoOnCollectFiles: TMemo;
    procedure btn1Click(Sender: TObject);
  private
    procedure CollectFileProgress(const directory : String; var skipScan : Boolean);
  end;


{ TForm1 }

procedure TForm1.btn1Click(Sender: TObject);
begin
  mmoDirList.Clear;
  CollectFiles('c:\MyDelphiFiles', '*.pas', mmoDirList.Lines, True, CollectFileProgress);
end;

procedure TForm1.CollectFileProgress(const directory: String; var skipScan: Boolean);
begin
  mmoDirList.Lines.Add(directory);
end;

答案 1 :(得分:0)

[解决]

unit MainFormU;

interface

uses
        Winapi.Windows,
        Winapi.Messages,
        System.SysUtils,
        System.Variants,
        System.Classes,
        Vcl.Graphics,
        Vcl.Controls,
        Vcl.Forms,
        Vcl.Dialogs,
        Vcl.StdCtrls,
        dwsXPlatform;

type
        TForm1 = class(TForm)
                btn1: TButton;
                mmoDirList: TMemo;
                mmoOnCollectFiles: TMemo;
    chkEnableOnCollectEvent: TCheckBox;
                procedure btn1Click(Sender: TObject);
                procedure OnCollectFileProgressEvent(const aDirectory: string; var aSkipScan: Boolean);
        private
                FOnCollectFiles: TCollectFileProgressEvent;
        end;

var
        Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
        mmoDirList.Clear;
        mmoOnCollectFiles.Clear;

        if chkEnableOnCollectEvent.Checked then
                FOnCollectFiles := OnCollectFileProgressEvent
        else
                FOnCollectFiles := nil;

        // procedure CollectFiles(const directory: UnicodeString;
        //                              fileMask: UnicodeString;
        //                              list: TStrings;
        //                              recurseSubdirectories: Boolean = False;
        //                              onProgress: TCollectFileProgressEvent = nil);

        CollectFiles('c:\MyFolder\', '*.pas', mmoDirList.Lines, True, FOnCollectFiles);
end;

procedure TForm1.OnCollectFileProgressEvent(const aDirectory: string; var aSkipScan: Boolean);
begin
        if aDirectory = 'c:\MyFolder\SkipThisFolder\' then begin
                ShowMessage('Folder ' + aDirectory + ' was skipped!');
                aSkipScan := True;
        end;

        mmoOnCollectFiles.Lines.Add(aDirectory);
end;

end.