1
unit Unit1;
2
3
interface
4
5
uses
6
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7
Dialogs, ComCtrls, StdCtrls;
8
9
type
10
TForm1 = class(TForm)
11
ListView1: TListView;
12
procedure FormCreate(Sender: TObject);
13
private
14
{ Private declarations }
15
procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
16
procedure AppOnMessage(var Msg: TMsg; var Handled: Boolean);
17
public
18
{ Public declarations }
19
end;
20
21
var
22
Form1: TForm1;
23
24
implementation
25
26
uses ShellAPI;
27
28
{$R *.dfm}
29
30
procedure TForm1.AppOnMessage(var Msg: TMsg; var Handled: Boolean);
31
var
32
WMD: TWMDropFiles;
33
begin
34
if Msg.message = WM_DROPFILES then
35
begin
36
WMD.Msg := Msg.message;
37
WMD.Drop := Msg.wParam;
38
WMD.Unused := Msg.lParam;
39
WMD.Result := 0;
40
WMDropFiles(WMD);
41
Handled := TRUE;
42
end;
43
end;
44
45
procedure TForm1.FormCreate(Sender: TObject);
46
begin
47
DragAcceptFiles(listview1.Handle, True);
48
Application.OnMessage := AppOnMessage;
49
end;
50
51
procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
52
var
53
N: Word;
54
buffer: array[0..180] of Char;
55
item: TListItem;
56
begin
57
with Msg do
58
begin
59
for N := 0 to DragQueryFile(Drop, $FFFFFFFF, buffer, 1) - 1 do
60
begin
61
DragQueryFile(Drop, N, Buffer, 80);
62
Item := ListView1.Items.Add;
63
item.Caption := StrPas(Buffer);
64
end;
65
DragFinish(Drop);
66
end;
67
end;
68
69
end.
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
1.引用 ShellAPI单元
2.定义AppOnMessage,拦截处理拖拽文件操作
3.设置接收拖拽文件的对象。DragAcceptFiles(listview1.Handle, True);
4.定义对拖拽文件的具体操作WMDropFiles(var Msg: TWMDropFiles);