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