How can I drag & drop a file from the shell? [duplicate]
Asked Answered
S

4

5

I am trying to drag and drop a video file (like .avi) from desktop But ı can not take it to the my program.But when ı try to drag and drop inside my program it works fine.For ex: I have an edittext and a listbox inside my pro and ı can move text that inside edittext to listbox.I could not get what is the difference ??

I take the video using openDialog.But ı wanna change it with drag and drop.

procedure TForm1.Button1Click(Sender: TObject);
   begin
     if OpenDialog1.Execute then
       begin
          MediaPlayer1.DeviceType:=dtAutoSelect;
          MediaPlayer1.FileName := OpenDialog1.FileName;
          Label1.Caption := ExtractFileExt(MediaPlayer1.FileName);
          MediaPlayer1.Open;
          MediaPlayer1.Display:=Self;
          MediaPlayer1.DisplayRect := Rect(panel1.Left,panel1.Top,panel1.Width,panel1.Height);
          panel1.Visible:=false;
          MediaPlayer1.Play;
       end;

   end;
Sennet answered 21/8, 2014 at 11:59 Comment(1)
The really old linked question doesn't really have much to offer except for Ander Melander's yet this question is being closed. Only two answers to this problem??? Please get over the shortsightedness and think from everyone's perspective.Rafiq
S
18

Here is a simple demo how to drag&drop files from Windows Explorer into a ListBox (for Delphi XE):

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  protected
    procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses ShellAPI;

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

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DragAcceptFiles(Handle, False);
end;

procedure TForm1.WMDropFiles(var Msg: TMessage);
var
  hDrop: THandle;
  FileCount: Integer;
  NameLen: Integer;
  I: Integer;
  S: string;

begin
  hDrop:= Msg.wParam;
  FileCount:= DragQueryFile (hDrop , $FFFFFFFF, nil, 0);

  for I:= 0 to FileCount - 1 do begin
    NameLen:= DragQueryFile(hDrop, I, nil, 0) + 1;
    SetLength(S, NameLen);
    DragQueryFile(hDrop, I, Pointer(S), NameLen);

    Listbox1.Items.Add (S);
  end;

  DragFinish(hDrop);
end;

end.
Stride answered 21/8, 2014 at 13:24 Comment(2)
I try your code , but it's not working , I try to drag and drop TxT file , but nothing happen.Heathen
@sami Works here. Did you connect up the OnCreate and OnDestroy events to the handlers.Cheeseparing
C
4

You can catch the WM_DROPFILES message.

First, set that your form will "accept" files from dragging in the FormCreate procedure:

DragAcceptFiles(Self.Handle, True);

After, declare the procedure in the desired form class:

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

Finally, fill the procedure body as follows:

procedure TForm1.WMDropFiles(var Msg: TMessage);
begin
  // do your job with the help of DragQueryFile function

  DragFinish(Msg.WParam);
end
Compo answered 21/8, 2014 at 12:18 Comment(2)
DragAcceptFiles(Self.Handle, True); is not working.Undeclareted identifer.Please tell me which uses you use for that ?Sennet
Its in the ShellApi (Winapi.ShellAPI).Compo
J
4

Alternatively, check out "The Drag and Drop Component Suite for Delphi" by Anders Melander. It works as-is with 32-bit and with some tweaking can be made to work with 64-bit as well (read the blog - it has been upgraded by 3rd parties).

Johanajohanan answered 21/8, 2014 at 12:33 Comment(5)
The last released 2010.Not working Delphi XE6.I am gonna try another one.Sennet
You didn't specify a Delphi version in your question, so it was impossible to gauge the usability of the last official release. However, I have a version working on my XE5. It took a little bit of tweaking to get it working, but it is certainly doable. Updated your question with XE6 tag.Johanajohanan
I downloaded the suit from Melander, but none of the demos work. In none of the demos I am allowed to drop files from the explorer. They all compile butt all keep giving the not allowed dragimage. There is no documentation so do you have any suggestions ?Firsthand
@GuidoG: You are not running the application (or explorer) as an Admin? Drag'n'Drop between Admin and non-Admin programs are not supported (unless you specifically allow for it in your code, which is not done in the Drag'n'Drop Suite).Johanajohanan
@Heeartware it seems it Does not works when running from Delphi. Stand alone it does work. It took me some time to realize that.Firsthand
P
4

You can also use DropMaster from Raize software.

Peterkin answered 21/8, 2014 at 12:41 Comment(1)
I try to avoid using extra components to do things that are easily done with a couple lines of code.Teacart

© 2022 - 2024 — McMap. All rights reserved.