一拖即亮!Delphi 文件拖拽功能引爆你的开发体验

请添加图片描述

前言

在很多工具类程序中,拖拽文件到窗口 是一个非常实用的功能。
例如:

  • 拖拽图片自动导入;
  • 拖拽文本文件直接打开;
  • 拖拽视频文件开始转换……

这些操作都能让程序更“顺手”,用户体验更好。

调用代码

var
  Form1: TForm1;
  DropForm: TDropTargetHelper;
  DropPanel: TDropTargetHelper;
  DropMemo: TDropTargetHelper;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  // 开启Form拖拽
  DropForm := TDropTargetHelper.Create(Self);
  DropForm.OnDropFiles := FormDropFiles;
  // 只允许拖拽图片文件
  DropForm.AddAllowedExt('.jpg');
  DropForm.AddAllowedExt('.png');
  DropForm.AddAllowedExt('.bmp');  
  
  // 开启Panel拖拽
  DropPanel := TDropTargetHelper.Create(Panel1);
  DropPanel.OnDropFile := PanelDropFile;
  
  // 开启Memo拖拽
  DropMemo := TDropTargetHelper.Create(Memo1);
  DropMemo.OnDropFile := MemoDropFile;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DropForm.Free;
  DropPanel.Free;
  DropMemo.Free;
end;

// 拖拽多个文件到窗体
procedure TForm1.FormDropFiles(Sender: TObject; const FileNames: TStrings);
begin
  ShowMessage('窗体收到文件:' + sLineBreak + FileNames.Text);
end;

// 拖拽单个文件到 Panel1
procedure TForm1.PanelDropFile(Sender: TObject; const FileName: string);
begin
  ShowMessage('Panel1收到文件:' + FileName);
end;

// 拖拽单个文件到 Memo
procedure TForm1.MemoDropFile(Sender: TObject; const FileName: string);
begin
  ShowMessage('Memo1收到文件:' + FileName);
end;

// 关闭拖拽
procedure TForm1.Button1Click(Sender: TObject);
begin
  DropForm.Disable;
  DropPanel.Disable;
  DropMemo.Disable;
end;

// 开启拖拽
procedure TForm1.Button2Click(Sender: TObject);
begin
  DropForm.Enable;
  DropPanel.Enable;
  DropMemo.Enable;
end;

下载源码

点击下载:https://download.csdn.net/download/gust2013/92205874

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NolanDing

请我喝杯咖啡吧,鼓励一下创作!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值