[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

Drag an drop a text file to a delphi form

Hello,

I want do do this :

- I get some mails with a particular text file (REG.TXT) attached.

- I want to do this : Open the mail in OutLook Express, click on the REG.TXT icon (attached file), drag and drop the REG.TXT on my Delphi app.

- Then read the contents of REG.TXT in my app.

How do this ? I want some working sample ...

Best regards,

Jean-Pol
0
jpdupont
Asked:
jpdupont
  • 3
1 Solution
 
LischkeCommented:
Hi Jean-Pol,

the easiest way is surely to use the drop files API (another but much more complex option would be to use interfaces).

During initialization of your program make your main window accepting dropped files by calling:

  DragAcceptFiles(Handle, True);

The write a message handler for this window (form):

type
  TMyForm = class(TForm)
  private
    procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
  end;

procedure TMyForm.WMDropFiles(var Msg: TWMDropFiles);

// the common game here if a file has been dropped on the application

var
  Buffer: array[0..MAX_PATH] of Char;
  Count, I: Integer;
  List: TStringList;

begin
  Count := DragQueryFile(Msg.Drop, DWORD(-1), nil, 0);
  List := TStringList.Create;
  for I := 0 to Count - 1 do
  begin
    DragQueryFile(Msg.Drop, I, Buffer, MAX_PATH);
    List.Add(Buffer);
  end;
  AddProjectFiles(List);
  List.Free;
  DragFinish(Msg.Drop);
  Msg.Result := 0;
end;


AddProjectFiles is the place you need to act. For instance you can use a memo like:

  Memo1.Lines.LoadFromFile(List[0]);

Ciao, Mike
0
 
kretzschmarCommented:
or get just the drag&drop components from

www.melander.dk

meikl
0
 
LischkeCommented:
..adding OLE and COM/ActiveX stuff just to accept a text file :-)

Ciao, Mike
0
 
jpdupontAuthor Commented:
Meikl,

thanks for the link.

===

Mike,

I try your code : fine !!!
I give you the points and the A grade.

1/ I don't understand your comment with OLE and COM ... Could you explain me ?

.... I just set "uses shellapi" in my code.

2/ What's the best value for MAX_PATH ?

Thanks,

Jean-Pol
0
 
LischkeCommented:
Well, Anders Melander's stuff is great for general purpose drag'n drop between applications. This involves, though, to include units into your application which will add the OLE stuff. If this is not already used by your application then you will get a large overhead just for a small task.

MAX_PATH is already defined (being 260 if I recall correctly). Look into Windows.pas.

Ciao, Mike
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

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