1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Winapi.ShellAPI;
type
TDropFilesEvent = procedure(Sender: TObject; const Files: TArray<string>) of object;
TListBox = class(Vcl.stdCtrls.TListBox)
private
FOnDropFiles: TDropFilesEvent;
procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
public
procedure CreateWnd; override;
property OnDropFiles: TDropFilesEvent read FOnDropFiles write FOnDropFiles;
end;
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Déclarations privées }
procedure OnDropFiles(Sender: TObject; const Files: TArray<string>);
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TListBox }
procedure TListBox.CreateWnd;
begin
inherited;
DragAcceptFiles(Handle, true);
end;
procedure TListBox.WMDropFiles(var Msg: TWMDropFiles);
var
Count: Integer;
Files: TArray<string>;
Index: Integer;
Len: Integer;
begin
Count := DragQueryFile(Msg.Drop, Cardinal(-1), nil, 0);
SetLength(Files, Count);
for Index := 0 to Count - 1 do
begin
Len := DragQueryFile(Msg.Drop, Index, nil, 0);
SetLength(Files[Index], Len);
DragQueryFile(Msg.Drop, Index, PChar(Files[Index]), Len + 1);
end;
DragFinish(Msg.Drop);
if (Count > 0) and Assigned(FOnDropFiles) then
FOnDropFiles(Self, Files);
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.OnDropFiles := OnDropFiles;
end;
procedure TForm1.OnDropFiles(Sender: TObject; const Files: TArray<string>);
begin
for var I := 0 to Length(Files) - 1 do
ShowMessage(Files[I]);
end;
end. |
Partager