faqts : Computers : Programming : Languages : Delphi

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

26 of 29 people (90%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Delphi: File: How to drag/drop a Microsoft Windows Explorer or desktop file on a memo?

Oct 20th, 2006 10:10
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 19 Juny 2005 - 02:22 am -----------------------
Delphi: File: How to drag/drop a Microsoft Windows Explorer or desktop 
file on a memo?
---
Steps: Overview:
 1. -Open Delphi
 2. -Create a new application
 3. -Put a memo on the form
     1. -Choose this memo from the palette 'standard'
 4. -Double click on the form
 5. -Fill or copy in the following source code
--- cut here: begin --------------------------------------------------
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 declarations }
 private
  procedure WMDROPFILES( var Message: TWMDROPFILES );
  message WM_DROPFILES;
  { Public declarations }
 public
end;
var
  Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate( Sender: TObject );
begin
 {Inform the Microsoft Windows API 'DragAcceptFiles' that this Delphi 
component will accept dropped files}
 DragAcceptFiles( Form1.Handle, True );
end;
procedure TForm1.WMDROPFILES( var Message: TWMDROPFILES );
var fileTotalI : longint;
var I : longint;
var uint : longint;
var buffer : array[0..255] of char;
begin
 uint := $FFFFFFFF;
 {How many files are totally being dropped by you (you can select more 
than 1 icon, e.g. on the destkop,
  by using <CTRL> or <SHIFT>)}
 fileTotalI := DragQueryFile( Message.Drop, uint, nil, 0 );
 {For first to last dropped icon, get its information (in this case 
its filename).
  The first icon has number 0, the second icon number 1, the third 
icon number 2, ...,
  the last icon number N - 1}
  for I := 1 to ( fileTotalI ) do begin
   {using the Microsoft Windows API 'DragQueryFile', store the 
filename in a buffer}
   DragQueryFile( Message.Drop, I - 1, @buffer, sizeof( buffer ) );
   {and put this filename at the end of the memo}
   Form1.Memo1.Lines.Add( buffer );
  end;
end;
end.
--- cut here: end ----------------------------------------------------
 6. -If you run this code and drag and drop icons (you can select more
     than 1 file, by using e.g. <CTRL><SHIFT> while selecting these
     files, thus 'NumFiles' can be 0, 1, 2, 3, ... depending how many
     files you select) from the desktop or files icons from Internet
     explorer in the memo, it will add the filename of that icon in the
     memo.
        +------------------------+
        |form                    |
        |                        |
        |  memo                  |
        | +--------------------+ |
        | |                    | |
        | | someFilename1      | |
        | | someFilename2      | |
        | | ...                | |
        | |                    | |
        | |                    | |
        | |                    | |
        | |                    | |
        | +--------------------+ |
        |                        |
        |                        |
        |                        |
        +------------------------+
---
---
Tested successfully on
Microsoft Windows XP Professional (service pack 2),
running
Delphi v7
---
---
Internet: see also:
---
Drag and Drop with Files
[Internet: source: http://community.borland.com/article/0%2C1410%
2C15529%2C00.html]
---
Dragging and dropping
http://delphi.about.com/library/weekly/aa080399.htm
---
Microsoft Windows: API: Function: DragAcceptFiles
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/shellcc/platform/shell/reference/functions/dragacceptfiles.asp
---
Microsoft Windows: API: Function: DragQueryFile
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/shellcc/platform/shell/reference/functions/dragqueryfile.asp
---
Delphi: Drag/drop: Link: Can you give an overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/36803/fid/175
---
BBCBASIC: Windows: File: How to drag/drop a Microsoft Windows Explorer 
or desktop file on a form?
http://www.faqts.com/knowledge_base/view.phtml/aid/42680/fid/768
----------------------------------------------------------------------