从外部拖拽文件

 1unit Unit1;
 2
 3interface
 4
 5uses
 6  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7  Dialogs, ComCtrls, StdCtrls;
 8
 9type
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
21var
22  Form1: TForm1;
23
24implementation
25
26uses ShellAPI;
27
28{$R *.dfm}
29
30procedure TForm1.AppOnMessage(var Msg: TMsg; var Handled: Boolean);
31var  
32  WMD: TWMDropFiles;
33begin  
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;
43end;
44
45procedure TForm1.FormCreate(Sender: TObject);
46begin
47  DragAcceptFiles(listview1.Handle, True);
48  Application.OnMessage := AppOnMessage;
49end;
50
51procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
52var  
53  N: Word;
54  buffer: array[0..180of Char;
55  item: TListItem;
56begin
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;
67end;
68
69end.

 

1.引用 ShellAPI单元

2.定义AppOnMessage,拦截处理拖拽文件操作

3.设置接收拖拽文件的对象。DragAcceptFiles(listview1.Handle, True);

4.定义对拖拽文件的具体操作WMDropFiles(var Msg: TWMDropFiles);

posted @ 2012-08-23 16:02  马儿快跑  阅读(385)  评论(0编辑  收藏  举报