让窗体接受拖放, 并获取拖过来的文件信息
2011-06-06 12:14
405 查看
原理分析: 这需要用到 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[0..255] of Char; i,count: Integer; begin {先获取拖拽的文件总数} count := DragQueryFile(message.Drop, $FFFFFFFF, nil, 0); {分别获取文件名} for i := 0 to count-1 do begin DragQueryFile(message.Drop, i, p, SizeOf(p)); Memo1.Lines.Add(p); {既然知道了文件名, 当然也可以随手打开它} end; end; end. -------------------------------------------------------------------------------- 窗体文件: -------------------------------------------------------------------------------- object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 154 ClientWidth = 261 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object Memo1: TMemo Left = 0 Top = 0 Width = 261 Height = 129 Align = alTop Lines.Strings = ( 'Memo1') ScrollBars = ssBoth TabOrder = 0 end end
相关文章推荐
- 让窗体接受拖放, 并获取拖过来的文件信息
- 让窗体接受拖放, 并获取拖过来的文件信息 - 回复 "海浪问" 的问题
- C#文件拖放至窗体的ListView控件获取文件类型
- xml-使用dom去遍历xml文件和获取指定节点信息
- 获取一个目录下的文件信息
- PHP文件操作(一)获取文件信息,读取文件
- asp.net C#获取程序文件相关信息
- 关闭主窗体而不退出主程序 以及如何获取操作系统的关闭、注销信息
- Android studio 获取aar包 获取签名文件信息
- Android通过aapt命令获取apk详细信息(包括:文件包名,版本号,SDK等信息)
- 获取文件版本信息
- 获取MP3和M4A音乐文件的歌曲信息以及专辑图片
- ASP.NET获取IP及电脑名等信息的简单方法+通用类文件源码
- 【转】windows c++获取文件信息——_stat函数的使用
- 三、获取文件的属性信息
- 操作 Wave 文件(4): 获取 Wave 文件主块与子块的信息
- C#获取一个文件的相关信息
- BAT - 通过脚本直接从apk文件获取包名等信息
- 获取一个文件的版本信息
- VerQueryValue获取文件-属性-详细信息