Solved

TMemo drag and drop

Posted on 2004-03-30
13
789 Views
Last Modified: 2010-04-16
Supposed to be easy: how to drag and drop a file from windows explorer in a TMemo?

Thank you!
0
Comment
Question by:__alex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +1
13 Comments
 

Expert Comment

by:rafamvc
ID: 10714620
why tmemo? are u planning to receive a txt file?
0
 
LVL 2

Author Comment

by:__alex
ID: 10714671
For the sake of simplicity: Yes.
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 125 total points
ID: 10714862
NOTE:  Uses SHellAPI

Shane



unit Unit1;

interface

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


type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure WMDROPFILES(var Message: TWMDROPFILES); message WM_DROPFILES;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WMDROPFILES(var Message: TWMDROPFILES);
var
  NumFiles : longint;
  i : longint;
  buffer : array[0..255] of char;
begin
 {How many files are being dropped}
  NumFiles := DragQueryFile(Message.Drop,
                            0,
                            nil,
                            0);
 {Accept the dropped files}
  for i := 0 to (NumFiles - 1) do begin
    DragQueryFile(Message.Drop,
                  i,
                  @buffer,
                  sizeof(buffer));
    Form1.Memo1.Lines.Add(buffer);
  end;

end;


procedure TForm1.FormCreate(Sender: TObject);
begin
 {Let Windows know we accept dropped files}
  DragAcceptFiles(Form1.Handle, True);
end;

end.
0
Industry Leaders: 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!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 10715522
shane,
i guess alex wants to open the file which was dropped
(in case of a text-file),
not a filelist

still listening . . .

meikl ;-)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10715555
Have you tried the code meikl,

it will allow you to drag a text file and drop it into your TMemo.
I didn't provide the Error checking, so this assumes that the file is a Text File

Shane
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10715974
no, i didn't tried, i guess it works,
but i see that you fill the memo with the dropped filenames,
and i guess, alex want to fill the memo with content of a dropped text-file

meikl ;-)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10716069
Oh, I see .. i had a brain fart - <smile>

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10716083
So I guess he could alter it,  to load the file versus add the file name to it.

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10716103
Off the top of my head, something like:

Shane

procedure TForm1.WMDROPFILES(var Message: TWMDROPFILES);
var
   buffer : array[0..255] of char;
begin
   DragQueryFile(Message.Drop,
                  i,
                  @buffer,
                  sizeof(buffer));
    Form1.Memo1.Lines.LoadFromFile(buffer);
  end;

end;
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10716127
sorry

procedure TForm1.WMDROPFILES(var Message: TWMDROPFILES);
var
   buffer : array[0..255] of char;
begin
   DragQueryFile(Message.Drop,
                  0,
                  @buffer,
                  sizeof(buffer));
    Form1.Memo1.Lines.LoadFromFile(buffer);
end;

Shane

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10717090
yep, only a smart change ;-))

but i owuld left out form1 here

>Form1.Memo1.Lines.LoadFromFile(buffer);

just

>Memo1.Lines.LoadFromFile(buffer);

(becaue you are in the TForm-object with this method,
whereas form1 may point to another instance,is  nil or points to somewhere)

meikl ;-)
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10717147
yeah, i type faster than i think...<smile>

Shane
0
 
LVL 2

Author Comment

by:__alex
ID: 10721490
Works great, thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 113
Magic Software info 18 152
Why can't I create any project with my delphi EXE file? 6 57
Microsoft Access 97 and Delphi XE2 9 84
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question