Delphi从外部拖拽文件


本文整理自网络,侵删。

 

1.引用 ShellAPI单元

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

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

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

unit Unit1; //Delphi从外部拖拽文件

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, ComCtrls, StdCtrls;

type

TForm1 = class(TForm)

ListView1: TListView;

procedure FormCreate(Sender: TObject);

private

{ Private declarations }

procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;

procedure AppOnMessage(var Msg: TMsg; var Handled: Boolean);

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

uses ShellAPI;

{$R *.dfm}

procedure TForm1.AppOnMessage(var Msg: TMsg; var Handled: Boolean);

var

WMD: TWMDropFiles;

begin

if Msg.message = WM_DROPFILES then

begin

WMD.Msg := Msg.message;

WMD.Drop := Msg.wParam;

WMD.Unused := Msg.lParam;

WMD.Result := 0;

WMDropFiles(WMD);

Handled := TRUE;

end;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

{ UAC权限 使用这三行

ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);

ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);

ChangeWindowMessageFilter(WM_COPYGLOBALDATA , MSGFLT_ADD);

}

DragAcceptFiles(listview1.Handle, True);

Application.OnMessage := AppOnMessage;

end;

procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);

var

N: Word;

buffer: array[0..180] of Char;

item: TListItem;

begin

with Msg do

begin

for N := 0 to DragQueryFile(Drop, $FFFFFFFF, buffer, 1) - 1 do

begin

DragQueryFile(Drop, N, Buffer, 80);

Item := ListView1.Items.Add;

item.Caption := StrPas(Buffer);

end;

DragFinish(Drop);

end;

end;

end.

相关阅读 >>

Delphi 按钮躲避鼠标点击

Delphi if pos 多条件判断用法交流

Delphi中使用自定义字体

Delphi 获取一个文件夹下的所有文件

Delphi sql 语句(常见) 新建,删除,修改表结构

Delphi xe5 json

Delphi xe6 取得app自己的版本号(横跨4个平台)

indy tidtcpclient 在网络掉线时的处理方法

Delphi 汉字与区位码

Delphi xe更改ttrayicon系统任务栏图标(无模糊)

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...