问题来源: http://www.cnblogs.com/del/archive/2009/01/20/1353117.html#1435746
原理分析:
这需要用到 ShellAPI 单元的两个函数: DragAcceptFiles、DragQueryFile;
用 DragAcceptFiles(窗口句柄, True); 以让窗口能够接受拖放;
然后就等待 WM_DROPFILES 消息, 并用 DragQueryFile 函数处理消息参数, 从而获取信息.
代码文件:
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTForm1 = class(TForm)Memo1: TMemo;procedure FormCreate(Sender: TObject);protectedprocedure WMDropFiles(var Message: TWMDropFiles); message WM_DROPFILES;end;varForm1: TForm1;implementation{$R *.dfm}uses ShellAPI;procedure TForm1.FormCreate(Sender: TObject); beginDragAcceptFiles(Handle, True); end;procedure TForm1.WMDropFiles(var Message: TWMDropFiles); varp: array[0..255] of Char;i,count: Integer; begin{先获取拖拽的文件总数}count := DragQueryFile(message.Drop, $FFFFFFFF, nil, 0);{分别获取文件名}for i := 0 to count-1 dobeginDragQueryFile(message.Drop, i, p, SizeOf(p));Memo1.Lines.Add(p); {既然知道了文件名, 当然也可以随手打开它}end; end;end.
窗体文件:
object Form1: TForm1Left = 0Top = 0Caption = 'Form1'ClientHeight = 154ClientWidth = 261Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'Tahoma'Font.Style = []OldCreateOrder = FalseOnCreate = FormCreatePixelsPerInch = 96TextHeight = 13object Memo1: TMemoLeft = 0Top = 0Width = 261Height = 129Align = alTopLines.Strings = ('Memo1')ScrollBars = ssBothTabOrder = 0end end