---------------------------
哈哈,这个是某个大神的,直接搬过来;
------------本人加了一点东西,加了一个图标,试了一下,Exe图标可以放上来,提供一个思路
-----------------------------
---------------Unit开始
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Panel1: TPanel;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
protected
procedure WndProc(var Message: TMessage); override;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ShellAPI;
{ DragAcceptFiles 和DragQueryFile 是声明在ShellAPI 单元的}
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True); {将Form1 注册为文件拖放接收控件}
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DragAcceptFiles(Handle, False); {注销Form1}
end;
{覆盖Form1 的窗口过程WndProc,这样就可以捕获WM_DROPFILES 消息}
procedure TForm1.WndProc(var Message: TMessage);
var
Count, Index, hDrop: Integer;
PFileName: PChar;
begin
if Message.Msg = WM_DROPFILES then
begin
hDrop := Message.WParam; {取得系统drop 结构的句柄,在后面要用到它}
GetMem(PFileName, MAX_PATH);
{取得文件个数}
Count := DragQueryFile(hDrop, MAXDWORD, PFileName, MAX_PATH-1);
Memo1.Clear;
//ListBox1.Items.Clear;
for Index := 0 to Count-1 do
begin
DragQueryFile(hDrop, Index, PFileName, MAXBYTE);
{取得每个文件的路径并放入缓冲区PFileName }
//ListBox1.Items.Add(PFileName);
Memo1.Lines.Add(PFileName);
if Index=0 then
begin
Image1.Picture.Icon.Handle := ExtractIcon(Handle,PFileName,0);
end;
end;
FreeMem(PFileName);
DragFinish(hDrop);
{当WM_DROPFILES 被处理后,应该调用DragFinish 释放资源}
end else
inherited;
end;
end.
--------------Unit结束
---------------Form开始
object Form1: TForm1
Left = 600
Top = 196
Width = 902
Height = 675
Caption = ‘Form1‘
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = ‘MS Sans Serif‘
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 0
Top = 0
Width = 886
Height = 161
Align = alTop
ImeName = ‘中文(简体) - 搜狗拼音输入法‘
ScrollBars = ssBoth
TabOrder = 0
end
object Panel1: TPanel
Left = 0
Top = 161
Width = 886
Height = 476
Align = alClient
TabOrder = 1
object Image1: TImage
Left = 8
Top = 16
Width = 265
Height = 145
Center = True
Stretch = True
end
end
end
---------------Form结束