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

无人久伴 提交于 2019-12-07 16:59:35

问题


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;

回答1:


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.



回答2:


You can also use DropMaster from Raize software.




回答3:


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



回答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).



来源:https://stackoverflow.com/questions/25425806/how-can-i-drag-drop-a-file-from-the-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!