[Delphi]从外部拖拽文件
1
unit Unit1;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
interface
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
uses
6
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7
Dialogs, ComCtrls, StdCtrls;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
var
22
Form1: TForm1;
23![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
implementation
25![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
uses ShellAPI;
27![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
{$R *.dfm}
29![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
procedure TForm1.FormCreate(Sender: TObject);
46
begin
47
DragAcceptFiles(listview1.Handle, True);
48
Application.OnMessage := AppOnMessage;
49
end;
50![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
end.
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1.引用 ShellAPI单元
2.定义AppOnMessage,拦截处理拖拽文件操作
3.设置接收拖拽文件的对象。DragAcceptFiles(listview1.Handle, True);
4.定义对拖拽文件的具体操作WMDropFiles(var Msg: TWMDropFiles);
示例下载