unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, shellapi,
ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
//file://设置需要处理文件WM_DROPFILES拖放消息
DragAcceptFiles(ListView1.Handle, TRUE);
//file://设置AppMessage过程来捕获所有消息
Application.OnMessage := AppMessage;
end;
procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
var
nFiles, I: Integer;
Filename: string;
ListItem: TListItem;
begin
//
// 注意!所有消息都将通过这里!
// 不要在此过程中编写过多的或者需要长时间操作的代码,否则将影响程序的性能
//
// 判断是否是发送到ListView1的WM_DROPFILES消息
if (Msg.message = WM_DROPFILES) and (msg.hwnd = ListView1.Handle) then
begin
// 取dropped files的数量
nFiles := DragQueryFile (Msg.wParam, $FFFFFFFF, nil, 0);
// 循环取每个拖下文件的全文件名
try
for I := 0 to nFiles - 1 do
begin
// 为文件名分配缓冲 allocate memory
SetLength (Filename, 80);
// 取文件名 read the file name
DragQueryFile (Msg.wParam, I, PChar (Filename), 80);
Filename := PChar (Filename);
//file://将全文件名分解程文件名和路径
ListItem := ListView1.Items.Add;
ListItem.Caption := ExtractFileName(FileName);
ListItem.SubItems.Add(ExtractFilePath(FileName));
end;
finally
//file://结束这次拖放操作
DragFinish (Msg.wParam);
end;
///file://标识已处理了这条消息
Handled := True;
end;
end;
end.