Drag&Drop files, from explorer to our form  

Send By: Q3 Team
Web : http://www.q3.nu
Email: dlib@q3.nu
Date: 16/07/00

Tip accessed 575 times

 


  • Add 'ShellApi' in the uses of your form
  • Put a ListBox (ListBox1) en tu form
  • Put this declaration in the private of your form:


         procedure WMDropFiles ( var Msg : TMessage ) ; message WM_DropFiles;
    



  • And the procedure in the implementation part:


     procedure TForm1.WMDropFiles ( var Msg : TMessage ) ;
     var
        hDrop : THandle ;
        fName : array [0..Max_Path] of char;
        FileCount : integer;
        i : integer;
     begin
        hDrop := Msg.WParam ;
        FileCount := DragQueryFile(hDrop,-1,fName,254);
        for i := 0 to FileCount-1 do begin
           DragQueryFile(hDrop,i,fName,254);
           {Aqui tienes los nombres de ficheros, uno a uno}
           {Here you have your file name 1 by 1}
           ListBox1.Items.add(fName);
        end;
        DragFinish ( hDrop);
     end;
    



  • Put this code in the OnCreate of the form:


     procedure TForm1.FormCreate(Sender: TObject);
     begin
        DragAcceptFiles( Handle,True ) ;
     end;
    




    Updated at 12/07/2000

    *Me han preguntado... ży si sólo quiero que sea ListBox1 la que acepte el Drag&Drop en lugar de la form entera?
    Have they asked me... and if I only want that it is ListBox1 the one that the Drag&Drop accepts instead of the whole form?
    It's very simple, simply change the event OnCreate of the example for this:


     procedure TForm1.FormCreate(Sender: TObject);
     begin
       DragAcceptFiles( ListBox1.Handle,True );
     end;
    





    Enviado por: "Arturo Garcia"

    Hace tiempo que llevo dandole vueltas , pues tal como esta el truco cuando arrastro ficheros , FileCount no contaba bien los ficheros seleccionados , lo he solucionado de este modo:



     private
       procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
     {...}
     procedure TForm1.FormCreate(Sender: TObject);
     begin
       DragAcceptFiles(Handle, TRUE);
     end;
     {...}
     procedure TForm1.WMDropFiles ( var Msg : TWMDropFiles ) ;
     var
       fName : array [0..Max_Path] of char;
       FileCount ,i: integer;
     begin
        FileCount := DragQueryFile( Msg.Drop,$FFFFFFFF,fName,MAX_PATH );
        for i := 0 to FileCount-1 do begin
           DragQueryFile( Msg.Drop,i,fName,MAX_PATH );
           ListBox1.Items.add( fName );
        end;
        DragFinish ( msg.Drop );
     end;
    




    Updated at 16/07/2000