type
TMainForm=
class(TForm)
TreeView1 : TTreeView;
.....
private
{ Private-Deklarationen }
OldLBWindowProc: TWndMethod;
public
{ Public-Deklarationen }
procedure WMDropFiles(
var Msg: TMessage);
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
//save old windoproc:
OldLBWindowProc := TreeView1.WindowProc;
TreeView1.WindowProc := WMDropFiles;
//enable drag and drop of treeview:
DragAcceptFiles(TreeView1.Handle, True);
end;
procedure TMainForm.OnClose(Sender: TObject;
var Action: TCloseAction);
begin
//disable drag and drop of treeview:
DragAcceptFiles(TreeView1.Handle, False);
end;´
//drag and drop:
procedure TMainForm.WMDropFiles(
var Msg: TMessage);
var
DropH: HDROP;
// drop handle
DroppedFileCount: Integer;
// number of files dropped
FileNameLength: Integer;
// length of a dropped file name
FileName:
string;
// a dropped file name
I: Integer;
// loops thru all dropped files
DropPoint: TPoint;
// point where files dropped
AnItem : TTreeNode;
begin
// Store drop handle from the message
case Msg.Msg
of
WM_DROPFILES :
begin
DropH := Msg.WParam;
try
// Optional: Get point at which files were dropped
DragQueryPoint(DropH, DropPoint);
// ... do something with drop point here
AnItem := TreeView1.GetNodeAt(DropPoint.X, DropPoint.Y) ;
//only handle the drop if node was under drop mouse:
if AnItem <>
nil then
begin
// Get count of files dropped
DroppedFileCount := DragQueryFile(DropH, $FFFFFFFF,
nil, 0);
// Get name of each file dropped and process it
for I := 0
to Pred(DroppedFileCount)
do
begin
// get length of file name
FileNameLength := DragQueryFile(DropH, I,
nil, 0);
// create string large enough to store file
// (Delphi allows for #0 terminating character automatically)
SetLength(FileName, FileNameLength);
// get the file name
DragQueryFile(DropH, I, PWideChar(FileName), FileNameLength + 1);
// process file name (application specific)
// ... processing code here
end;
end;
finally
// Tidy up - release the drop handle
// don't use DropH again after this
DragFinish(DropH);
end;
// Note we handled message
Msg.Result := 0;
end;
// WM_DROPFILES : begin
//forward message to original WndProc:
else OldLBWindowProc(Msg);
end;
//case Msg.Msg of
end;