1

由于我对 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;
4

2 回答 2

1

根据文档 TCollectFileProgressEvent 的声明如下:

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

让我们把它分成3部分:

1) TCollectFileProgressEvent

2)过程(常量目录:字符串;变量skipScan:布尔值)

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;
于 2017-12-05T10:20:58.343 回答
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.
于 2017-12-05T10:19:55.610 回答