Link to home
Start Free TrialLog in
Avatar of mdlittle
mdlittle

asked on

Accept dragged item from the desktop

How does a form accept dragged items from the desktop? I am especially interested in accepting links from the desktop.

Mike
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As a full walkthrough:

1) Download the 3 unit files:
  DragDropFilesEx.pas
  DragDrop.pas
  PIDL.pas

2.) Place these files in your {delphi}\lib path, or a suitable location that is in the IDE for delphi.

3.) Edit the DragDropFilesEx unit to add the component register. (not sure why the original author did not do this). The additions are as noted below:
 
---- ADDITION before implementation section ----
procedure Register;
---- END ADDITION ----

implementation

- and at the end of the unit before the end. statement -

---- ADDITION ----
procedure Register;
begin
  RegisterComponents('Samples', [TDragDropFilesEx]);
end;
---- END ADDITION ----

end.

4.) Save this unit source, then select Component | Install Component | and Browse to the unit file DragDropFilesEx. Install this component which will end up on your Samples tab.

5.) Drop the component on your form, and set the DragDropControl = Form1. The rest is very basic (it may take a few minutes to set up, but its worth it)

Example unit code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DragDrop, DragDropFilesEx;

type
  TForm1               =  class(TForm)
     DragDropFilesEx1: TDragDropFilesEx;
     procedure         DragDropFilesEx1DragEnter(DataObj: IDataObject; grfKeyState: Integer; pt: TPoint; var dwEffect: Integer; var Accept: Boolean);
     procedure         DragDropFilesEx1ProcessDropped(Sender: TObject; grfKeyState: Integer; pt: TPoint; dwEffect: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.DragDropFilesEx1DragEnter(DataObj: IDataObject; grfKeyState: Integer; pt: TPoint; var dwEffect: Integer; var Accept: Boolean);
begin
  Accept:=True;
end;

procedure TForm1.DragDropFilesEx1ProcessDropped(Sender: TObject; grfKeyState: Integer; pt: TPoint; dwEffect: Integer);
var  dwIndex:       Integer;
begin
  for dwIndex:=0 to Pred(DragDropFilesEx1.FileList.Count) do
     ShowMessage(DragDropFilesEx1.FileList.Items[dwIndex].Name);
end;

end.

-- and form dfm code ---

object Form1: TForm1
  Left = 339
  Top = 326
  Width = 401
  Height = 211
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object DragDropFilesEx1: TDragDropFilesEx
    AcceptOwnDnD = False
    BringToFront = False
    DragDropControl = Owner
    ScrollDetectOptions.AreaWizard = '(Click here to start)'
    SourceCompatibility = []
    SourceEffects = [deCopy]
    TargetPopupMenu = True
    TargetEffects = [deCopy]
    DragDetect.Automatic = True
    DragDetect.DeltaX = 4
    DragDetect.DeltaY = 4
    OnDragEnter = DragDropFilesEx1DragEnter
    OnProcessDropped = DragDropFilesEx1ProcessDropped
    NeedValid = [nvFilename]
    Left = 12
    Top = 8
  end
end

---

Regards,
Russell


2.) that is in the IDE **path**

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

  Drop a ListBox on your form, then handle wm_DropFiles message, and it will accept the files, try this sample


unit Unit1;

interface

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

type
  TForm4 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
   procedure DropFiles (var Msg: TWmDropFiles);  message wm_DropFiles;
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}
uses
  ShellAPI;
procedure TForm4.DropFiles(var Msg: TWmDropFiles);
var
  FileName: string;
begin
  SetLength(FileName,200);
  DragQueryFile(Msg.Drop,0,Pchar(FileName),200);
  ListBox1.Items.Add(FileName)
end;

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

Regards,
Mohammed
Avatar of Haggard1
Haggard1

Hi rllibby,

Can you have a look at this post for me you answered something similar last year, your help would be most appreciated.
https://www.experts-exchange.com/questions/21406728/Change-Windows-XP-Dektop-Theme.html

Cheers

Haggard1
What wrong with my post cwwkie?
> What wrong with my post cwwkie?

I did not put you in the split because you posted a link to a link of a zipfile. So a lot to do before before it can be read.

To be honest, I did not download it before, but I have done now. And I see there is an error in the code from mnasman which is not in the code you link to (mnasman: DragFinish is not called, so there is a small memory leak). So I will post a new recommendation.

I hope everybody agrees.
Thanks.
I know it was trouble to check it but i posted the link to give credits to the author.
DarthMod what happened to the :
Split: rllibby {http:#13896602} & CodedK {http:#13898530}

???