当前位置: 首页 > news >正文

【Delphi】接收windows文件夹中文件拖拽

本文根据EmailX45的视频文件,进行了优化改进,原文参见:Delphi: Drag and Drop Files from Explorer into TPanel / TMemo - YouTube

在Windows中,如果将选择的文件拖动到Delphi程序的控件上,有很多实现方法,特别是三方的成熟控件,但是这个方法最简单,无需使用第三方资源,简单使用windows API即可。

直接上代码入如下:

主窗体:

unit uDragFile_Form;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,uMyPanelWithDropFileEvents,uMyMemoWithDropFileEvents;typeTForm4 = class(TForm)Panel1: TPanel;Memo1: TMemo;Label1: TLabel;GestureManager1: TGestureManager;procedure FormCreate(Sender: TObject);privateprocedure OnDraopFileToPanel(Sender : TObject; FileName : string);     //重点,必须是这个类型 TOnDropFileprocedure OnDraopFileToMemo(Sender : TObject; FileName : string);public{ Public declarations }end;varForm4: TForm4;implementationusesWinapi.ShellAPI;{$R *.dfm}{ TForm4 }procedure TForm4.FormCreate(Sender: TObject);
beginPanel1.EnableFileDrop(OnDraopFileToPanel);Memo1.EnableFileDrop(OnDraopFileToMemo);end;procedure TForm4.OnDraopFileToMemo(Sender: TObject; FileName: string);
beginMemo1.Lines.Add(FileName);
end;procedure TForm4.OnDraopFileToPanel(Sender: TObject; FileName: string);
beginLabel1.Caption := Label1.Caption + sLineBreak + Filename;end;end.

以下两段代码分别是Panel和Memo的实现代码,需要在主窗体中引用。

{sensor 参考:https://www.youtube.com/watch?v=XaO0YWRcxMw
使用说明:
1. 主程序中引用这个单元
2. 主程序中必须定义 TOnDropFile 事件   ,例如:   procedure OnDraopFileToPanel(Sender : TObject; FileName : string);
3. 主程序CreateForm中:Panel1.EnableFileDrop(OnDraopFileToPanel);4. 如果不需要拖动文件: DisableFileDrop2025-06-02
}
unit uMyPanelWithDropFileEvents;interface
usesWinapi.Messages,VCL.ExtCtrls;typeTOnDropFile = procedure(Sender : TObject; FileName : string) of Object;TPanel = class(VCL.ExtCtrls.TPanel)privateFOnDropFile : TOnDropFile;procedure DropFileEvent(var MSG : TWMDropFiles); message WM_DROPFILES;publicprocedure EnableFileDrop(FOnDrop: TOnDropFile);procedure DisableFileDrop;property OnDropFile : TOnDropFile read FOnDropFile write FOnDropFile;end;implementationusesWinapi.Windows,Winapi.ShellAPI;constMY_FILE_COUNTING = $FFFFFFFF;{ TPanel }procedure TPanel.DisableFileDrop;
beginDragAcceptFiles(Self.Handle,False);FOnDropFile := nil;
end;procedure TPanel.DropFileEvent(var MSG: TWMDropFiles);
varLFileCounting : Cardinal;LFileName     : array[0..MAX_Path] of char;
beginLFileCounting := DragQueryFile(MSG.Drop,MY_FILE_COUNTING,LFileName, MAX_Path);if Assigned(FOnDropFile) thenbeginfor var i := 0 to LFileCounting - 1 dobeginDragQueryFile(MSG.Drop,i,LFileName, MAX_Path);FOnDropFile(Self, String(LFileName));end;end;DragFinish(MSG.Drop);
end;procedure TPanel.EnableFileDrop(FOnDrop: TOnDropFile);
beginDragAcceptFiles(Self.Handle,True);FOnDropFile := FOnDrop;
end;end.

Memo代码

unit uMyMemoWithDropFileEvents;interface
usesWinapi.Messages,VCL.StdCtrls;typeTOnDropFile = procedure(Sender : TObject; FileName : string) of Object;TMemo = class(VCL.StdCtrls.TMemo)privateFOnDropFile : TOnDropFile;procedure DropFileEvent(var MSG : TWMDropFiles); message WM_DROPFILES;publicprocedure EnableFileDrop(FOnDrop: TOnDropFile);procedure DisableFileDrop;property OnDropFile : TOnDropFile read FOnDropFile write FOnDropFile;end;implementationusesWinapi.Windows,Winapi.ShellAPI;constMY_FILE_COUNTING = $FFFFFFFF;{ TMemo }procedure TMemo.DisableFileDrop;
beginDragAcceptFiles(Self.Handle,False);FOnDropFile := nil;
end;procedure TMemo.DropFileEvent(var MSG: TWMDropFiles);
varLFileCounting : Cardinal;LFileName     : array[0..MAX_Path] of char;
beginLFileCounting := DragQueryFile(MSG.Drop,MY_FILE_COUNTING,LFileName, MAX_Path);if Assigned(FOnDropFile) thenbeginfor var i := 0 to LFileCounting - 1 dobeginDragQueryFile(MSG.Drop,i,LFileName, MAX_Path);FOnDropFile(Self, String(LFileName));end;end;DragFinish(MSG.Drop);
end;procedure TMemo.EnableFileDrop(FOnDrop: TOnDropFile);
beginDragAcceptFiles(Self.Handle,True);FOnDropFile := FOnDrop;
end;end.

相关文章:

  • 什么是阻抗匹配
  • 数学建模期末速成 多目标规划
  • Retrievers检索器+RAG文档助手项目实战
  • Linux指令:
  • 408考研逐题详解:2009年第28题
  • P12592题解
  • 京东轨迹验证码识别代码
  • 在 Linux 服务器上无需 sudo 权限解压/打包 .7z 的方法(实用命令)
  • 《高等数学》(同济大学·第7版)第一章第四节《无穷小与无穷大》的超级详细
  • 工作日记之权限校验-token的实战案例
  • 哈喽,我是钓鱼的肝
  • 计算机科技笔记: 容错计算机设计05 n模冗余系统 其他复杂结构
  • 深度理解与剖析:Odoo系统邮箱配置指南
  • MybatisPlus(含自定义SQL、@RequiredArgsConstructor、静态工具类Db)
  • BUUCTF之[ACTF2020 新生赛]BackupFile
  • cJSON简单使用
  • 前端面试高频问题通关指南--通用性问题
  • 洛谷-P3912素数个数题解
  • window/linux ollama部署模型
  • IPtables部署和使用
  • 妇女之家网站建设方案/宁德市属于哪个省份
  • 专业东莞网站建设报价/网站结构优化
  • 微积壹佰 网站建设/校园推广方案
  • 怎么提高网站流量/搜狗站长管理平台
  • 自助建站系统加盟/手机360优化大师官网
  • wordpress模版 使用教程/南昌seo网站排名