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

【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.

http://www.xdnf.cn/news/10589.html

相关文章:

  • (Python网络爬虫);抓取B站404页面小漫画
  • Python-matplotlib库之核心对象
  • 设计模式——备忘录设计模式(行为型)
  • Kotlin 中 companion object 扩展函数详解
  • Java连接Redis和基础操作命令
  • 【Linux】Ubuntu 20.04 英文系统显示中文字体异常
  • 什么是线程上下文切换?
  • 【SpringBoot】| 接口架构风格—RESTful
  • 概率统计:AI大模型的数学支柱
  • Linux--进程概念
  • 【redis实战篇】第七天
  • 03- javascript的运行原理
  • 启动metastore时报错MetaException(message:Version information not found in metastore
  • 海底三维可视化平台
  • 使用Python进行函数作画
  • azure devops 系列 - 常用的task
  • C++面向对象编程:类与对象详解
  • CppCon 2014 学习:(Costless)Software Abstractions for Parallel Architectures
  • 已有的前端项目打包到tauri运行(windows)
  • Python应用range函数初解pro
  • arc3.2语言sort的时候报错:(sort < `(2 9 3 7 5 1)) 需要写成这种:(sort > (pair (list 3 2)))
  • EXCEL--累加,获取大于某个值的第一个数
  • DeepSeek模型高级应用:提示工程与Few-shot学习实战指南
  • 【深度学习-Day 21】框架入门:神经网络模型构建核心指南 (Keras PyTorch)
  • MySQL中的字符串分割函数
  • Python 训练营打卡 Day 33-神经网络
  • tex中的表格4:自动表格宽度自动换行tabularx宏包
  • 结构化控制语言(SCL) 与梯形图(LAD)相互转换的步骤指南
  • CppCon 2014 学习:EFFICIENCY PERFORMANCE ALGORITHMS DATA STRUCTURES
  • Linux中的System V通信标准-共享内存、消息队列以及信号量