• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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
0
mdlittle
Asked:
mdlittle
2 Solutions
 
Russell LibbySoftware Engineer, Advisory Commented:
Its a matter of handling the IDropTarget interface (COM/OLE based). An excellent unit for this can be found here:

http://www.koders.com/delphi/fid73F3EE2E480AE06F315DE609BE435B966640129C.aspx

Hope this helps,
Russell
0
 
Russell LibbySoftware Engineer, Advisory Commented:
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


0
 
Russell LibbySoftware Engineer, Advisory Commented:
2.) that is in the IDE **path**

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mohammed NasmanSoftware DeveloperCommented:
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
0
 
Haggard1Commented:
Hi rllibby,

Can you have a look at this post for me you answered something similar last year, your help would be most appreciated.
http://www.experts-exchange.com/Operating_Systems/Q_21406728.html

Cheers

Haggard1
0
 
CodedKCommented:
What wrong with my post cwwkie?
0
 
cwwkieCommented:
> 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.
0
 
CodedKCommented:
Thanks.
I know it was trouble to check it but i posted the link to give credits to the author.
0
 
CodedKCommented:
DarthMod what happened to the :
Split: rllibby {http:#13896602} & CodedK {http:#13898530}

???
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now