delphi从外部拖拽文件  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.03407096862793 seconds