I use this method to capture (receive) files dragged into TWinControl from Explorer.
You can check it out for your control. The standard TTreeView works fine.
Add ShellAPI to use.
In the private section:
private originalEditWindowProc : TWndMethod; procedure EditWindowProc(var Msg:TMessage); // accept the files dropped procedure FilesDrop(var Msg: TWMDROPFILES);
In OnCreate of your form:
// Assign procedures originalEditWindowProc := TreeView1.WindowProc; TreeView1.WindowProc := EditWindowProc; // Aceptar ficheros arrastrados // Accept the files ShellAPI.DragAcceptFiles(TreeView1.Handle, True);
And these two procedures:
// Al arrastrar ficheros sobre el TV. On drop files to TV procedure TForm1.FilesDrop(var Msg: TWMDROPFILES); var i:Integer; DroppedFilename:string; numFiles : longInt; buffer : array[0..MAX_PATH] of char; begin // Número de ficheros arrastrados // Number of files numFiles := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0) ; // Recorrido por todos los arrastrados // Accept all files for i := 0 to (numFiles - 1) do begin DragQueryFile(Msg.Drop, i, @buffer, sizeof(buffer)); // Proteccion try DroppedFilename := buffer; // HERE you can do something with the file... TreeView1.Items.AddChild(nil, DroppedFilename); except on E:Exception do begin // catch end; end; end; end; procedure TForm1.EditWindowProc(var Msg: TMessage); begin // if correct message, execute the procedure if Msg.Msg = WM_DROPFILES then begin FilesDrop(TWMDROPFILES(Msg)) end else begin // in other case do default... originalEditWindowProc(Msg) ; end; end;
I hope this is helpful to you.
Sincerely.
Germán Estévez -Neftalí-
source share