1.添加引用ShellAPI。
2.运行程序时初始化 接受外部拖放。
3.接收事件
代码如下:
2.运行程序时初始化 接受外部拖放。
3.接收事件
代码如下:
1unit mainForm;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ComCtrls,StrUtils, WinSkinData, SkinCaption,ShellAPI;//添加ShellAPI引用
8
9type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 OpenDialog1: TOpenDialog;
13 procedure FormCreate(Sender: TObject);
14 procedure Button1Click(Sender: TObject);
15 private
16 { Private declarations }
17 public
18 { Public declarations }
19 procedure DropFiles(var Msg: TMessage); message WM_DROPFILES;
20
21 end;
22
23var
24 Form1: TForm1;
25
26implementation
27
28{$R *.dfm}
29
30{ TForm1 }
31//显示文件路径
32procedure TForm1.Button1Click(Sender: TObject);
33begin
34 if OpenDialog1.Execute then
35 begin
36 ShowMessage('当前文件路径为:'+OpenDialog1.FileName);
37 end;
38end;
39//功能同上,显示文件路径
40procedure TForm1.DropFiles(var Msg: TMessage);
41var
42 buffer: array[0..1024] of Char;
43begin
44 inherited;
45 buffer[0] := #0;
46 DragQueryFile(Msg.WParam, 0, buffer, sizeof(buffer)); //第一个文件
47 ShowMessage('当前文件路径为:'+buffer);
48end;
49//初始化,接受外部拖放
50procedure TForm1.FormCreate(Sender: TObject);
51begin
52 DragAcceptFiles(Handle,True);//第二个参数为False时,不启用文件拖放
53end;
54
55end.
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ComCtrls,StrUtils, WinSkinData, SkinCaption,ShellAPI;//添加ShellAPI引用
8
9type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 OpenDialog1: TOpenDialog;
13 procedure FormCreate(Sender: TObject);
14 procedure Button1Click(Sender: TObject);
15 private
16 { Private declarations }
17 public
18 { Public declarations }
19 procedure DropFiles(var Msg: TMessage); message WM_DROPFILES;
20
21 end;
22
23var
24 Form1: TForm1;
25
26implementation
27
28{$R *.dfm}
29
30{ TForm1 }
31//显示文件路径
32procedure TForm1.Button1Click(Sender: TObject);
33begin
34 if OpenDialog1.Execute then
35 begin
36 ShowMessage('当前文件路径为:'+OpenDialog1.FileName);
37 end;
38end;
39//功能同上,显示文件路径
40procedure TForm1.DropFiles(var Msg: TMessage);
41var
42 buffer: array[0..1024] of Char;
43begin
44 inherited;
45 buffer[0] := #0;
46 DragQueryFile(Msg.WParam, 0, buffer, sizeof(buffer)); //第一个文件
47 ShowMessage('当前文件路径为:'+buffer);
48end;
49//初始化,接受外部拖放
50procedure TForm1.FormCreate(Sender: TObject);
51begin
52 DragAcceptFiles(Handle,True);//第二个参数为False时,不启用文件拖放
53end;
54
55end.