【转】Delphi 文件拖放
阅读原文时间:2023年07月15日阅读:1

转自:万一的博客。

原理分析:

这需要用到 ShellAPI 单元的两个函数: DragAcceptFiles、DragQueryFile;


DragAcceptFiles(窗口句柄, True); 以让窗口能够接受拖放;

然后就等待 WM_DROPFILES 消息, 并用 DragQueryFile
函数处理消息参数, 从而获取信息.


代码文件:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
protected
procedure WMDropFiles(var Message: TWMDropFiles); message WM_DROPFILES;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses ShellAPI;

procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
end;

procedure TForm1.WMDropFiles(var Message: TWMDropFiles);
var
p: array[..] of Char;
i,count: Integer;
begin
{先获取拖拽的文件总数}
count := DragQueryFile(message.Drop, $FFFFFFFF, nil, );

{分别获取文件名}
for i := to count- do
begin
DragQueryFile(message.Drop, i, p, SizeOf(p));
Memo1.Lines.Add(p); {既然知道了文件名, 当然也可以随手打开它}
end;
end;

end.


窗体文件:


object Form1: TForm1
Left =
Top =
Caption = 'Form1'
ClientHeight =
ClientWidth =
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch =
TextHeight =
object Memo1: TMemo
Left =
Top =
Width =
Height =
Align = alTop
Lines.Strings = (
'Memo1')
ScrollBars = ssBoth
TabOrder =
end
end


手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器