Solved

TMemo drag and drop

Posted on 2004-03-30
13
727 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
  • 7
  • 3
  • 2
  • +1
13 Comments
 

Expert Comment

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

Author Comment

by:__alex
Comment Utility
For the sake of simplicity: Yes.
0
 
LVL 11

Accepted Solution

by:
shaneholmes earned 125 total points
Comment Utility
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
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 11

Expert Comment

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

Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yeah, i type faster than i think...<smile>

Shane
0
 
LVL 2

Author Comment

by:__alex
Comment Utility
Works great, thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now