1.添加引用ShellAPI。
2.运行程序时初始化 接受外部拖放。
3.接收事件
代码如下:
unit mainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls,StrUtils, WinSkinData, SkinCaption,ShellAPI;//添加ShellAPI引用
type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure DropFiles(var Msg: TMessage); message WM_DROPFILES;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
//显示文件路径
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
ShowMessage('当前文件路径为:'+OpenDialog1.FileName);
end;
end;
//功能同上,显示文件路径
procedure TForm1.DropFiles(var Msg: TMessage);
var
buffer: array[0..1024] of Char;
begin
inherited;
buffer[0] := #0;
DragQueryFile(Msg.WParam, 0, buffer, sizeof(buffer)); //第一个文件
ShowMessage('当前文件路径为:'+buffer);
end;
//初始化,接受外部拖放
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle,True);//第二个参数为False时,不启用文件拖放
end;
end.