unit DropFiles; { DropFiles support for any Windows Control ! (C)2K By Paul TOTH http://tothpaul.free.fr } { This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. } { Usage : for a DropFile support on ListView1:TListView Uses ...,DropFiles; Type TForm1=class(TForm) ... private procedure OnDropFile(Sender:TObject; AFileName:string); ... procedure TForm1.FormCreate(Sender: TObject); begin SetDropFiles(ListView1,OnDropFile); // Add DropFile support end; procedure TForm1.FormDestroy(Sender: TObject); begin SetDropFiles(ListView1,nil); // Remove DropFile support end; procedure TForm1.OnDropFile(Sender:TObject; AFileName:string); begin ShowMessage(AFileName+' Droped'); end; } interface uses Windows,Messages,Controls,ShellAPI,Dialogs; Type TDropFileEvent=procedure(Sender:TObject; AFileName:string) of object; procedure SetDropFiles(AControl:TWinControl; AMethod:TDropFileEvent); implementation Type TDropInfo=record Control:TWinControl; Method :TDropFileEvent; WndProc:pointer; end; function DropProc(Handle,Msg,wParam,lParam:integer):integer; stdcall; far; export; var DropInfo:^TDropInfo; Count :integer; Size :integer; Name :PChar; begin longint(DropInfo):=GetProp(Handle,'OnDrop'); if Msg=WM_DROPFILES then begin Count:=DragQueryFile(wParam,$FFFFFFFF,nil,0); while Count>0 do begin Dec(Count); Size:=DragQueryFile(wParam,Count,nil,0)+1; GetMem(Name,Size); DragQueryFile(wParam,Count,Name,Size); DropInfo.Method(DropInfo.Control,Name); FreeMem(Name,Size); end; Result:=0; end else begin // Result:=CallWindowProc(DropInfo.WndProc,Handle,Msg,wParam,lParam); Result:=DropInfo.Control.Perform(Msg,wParam,lParam); end; end; procedure SetDropFiles(AControl:TWinControl; AMethod:TDropFileEvent); var DropInfo:^TDropInfo; begin if @AMethod=nil then begin DragAcceptFiles(AControl.Handle, False); integer(DropInfo):=GetProp(AControl.Handle,'OnDrop'); SetWindowLong(AControl.HAndle,GWL_WNDPROC,integer(DropInfo.WndProc)); Dispose(DropInfo); end else begin New(DropInfo); DropInfo.Control:=AControl; DropInfo.Method :=AMethod; DropInfo.WndProc:=pointer(GetWindowLong(AControl.Handle,GWL_WNDPROC)); SetProp(AControl.Handle,'OnDrop',Integer(DropInfo)); SetWindowLong(AControl.Handle,GWL_WNDPROC,longint(@DropProc)); DragAcceptFiles(AControl.Handle, True); end; end; end.