用 DRAG & DROP COMPONENT SUITE VERSION 3.5 吧
可以做到, http://www.melander.dk可以当
也可以用下面的消息,然后由DRAGQUERFILE返回值得到文件多少,然后LPSZFILE得到
文件名。stringlist
查看DELPHI原码可以看到定义。
WM_DROPFILES
hDrop = (HANDLE) wParam; // handle of internal drop structure
UINT DragQueryFile(
HDROP hDrop, // handle to structure for dropped files
UINT iFile, // index of file to query
LPTSTR lpszFile, // buffer for returned filename
UINT cch // size of buffer for filename
);
Parameters
hDrop
Identifies the structure containing the filenames of the dropped files.
iFile
Specifies the index of the file to query. If the value of the iFile parameter is 0xFFFFFFFF, DragQueryFile returns a count of the files dropped. If the value of the iFile parameter is between zero and the total number of files dropped, DragQueryFile copies the filename with the corresponding value to the buffer pointed to by the lpszFile parameter.
lpszFile
Points to a buffer to receive the filename of a dropped file when the function returns. This filename is a null-terminated string. If this parameter is NULL, DragQueryFile returns the required size, in characters, of the buffer.
cch
Specifies the size, in characters, of the lpszFile buffer.
Return Values
When the function copies a filename to the buffer, the return value is a count of the characters copied, not including the terminating null character.
If the index value is 0xFFFFFFFF, the return value is a count of the dropped files.
If the index value is between zero and the total number of dropped files and the lpszFile buffer address is NULL, the return
也可以这样:
//需要use ShellAPI
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
PROCEDURE FileIsDropped ( VAR Msg : TMessage ) ;
Message WM_DropFiles ;//消息处理
public
{ Public declarations }
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles( Handle,True ) ;
end;
PROCEDURE TForm1.FileIsDropped ( VAR Msg : TMessage ) ;
VAR
hDrop : THandle ;
fName : ARRAY[0..254] OF CHAR ;
NumberOfFiles : INTEGER ;
fCounter : INTEGER ;
Names : TSTRINGLIST ;
BEGIN
hDrop := Msg.WParam ;
NumberOfFiles := DragQueryFile(hDrop,-1,fName,254);
FOR fCounter := 1 TO NumberOfFiles DO
BEGIN
DragQueryFile(hDrop,fCounter,fName,254);
Names.Add(fName) ;
END ;
ShowMessage('Droped '+IntToStr(NumberOfFiles) + ' Files : ' +
Names.Text );
//你想干吗就干吗吧
DragFinish ( hDrop);
end;
end.
下面的程序可以实现将文件从资源管理器中拖放到应用程序中,Listbox中显示文
件名和路径。
在Form1上放置一个Listbox.
TForm1定义如下:
type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure WMDropFiles(VAR Msg: TWMDropFiles);message WM_DROPFILES;
procedure AppOnMessage(VAR Msg: TMsg;VAR Handled : Boolean);
public
{ Public declarations }
end;
WMDropFiles,AppOnMessage如下,
procedure TForm1.WMDropFiles(VAR Msg: TWMDropFiles);
VAR
N : Word;
buffer : ARRAY[0..180] OF Char;
BEGIN
WITH Msg DO
BEGIN
FOR N := 0 TO DragQueryFile(Drop, $FFFFFFFF, buffer,1)-1 DO
BEGIN
DragQueryFile(Drop, N, Buffer, 80);
ListBox1.Items.Add(StrPas(Buffer));
END;
DragFinish(Drop);
END;
END;
procedure TForm1.AppOnMessage(VAR Msg: TMsg;
VAR Handled : Boolean);
VAR WMD : TWMDropFiles;
BEGIN
IF Msg.message = WM_DROPFILES then
BEGIN
MessageBeep(0);
WMD.Msg := Msg.message;
WMD.Drop := Msg.wParam;
WMD.Unused := Msg.lParam;
WMD.Result := 0;
WMDropFiles(WMD);
Handled := TRUE;
END;
END;
TForm1的create事件:
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
DragAcceptFiles(Application.Handle, True);
Application.OnMessage := AppOnMessage;
end;
差不多就这些了。