Author: Tomas Rutkauskas
Let them drag and drop files on your program
Answer:
If you want to let your users drag and drop files on your program from the File
Manager and Windows Explorer, simply add the code inside //>>> and //<<< to your
program as in the following example:
1 unit dropfile;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes,
7 Graphics, Controls, Forms, Dialogs;
8
9 type
10 TForm1 = class(TForm)
11 procedure FormCreate(Sender: TObject);
12 private
13 { Private declarations }
14 public
15 { Public declarations }
16
17 //>>>
18
19 // declare our DROPFILES message handler
20 procedure AcceptFiles(var msg: TMessage);
21 message WM_DROPFILES;
22 //<<<
23 end;
24
25 var
26 Form1: TForm1;
27
28 implementation
29
30 uses
31 //>>>
32 //
33 // this unit contains certain
34 // functions that we'll be using
35 //
36 ShellAPI;
37 //<<<
38
39 {$R *.DFM}
40
41 //>>>
42
43 procedure TForm1.AcceptFiles(var msg: TMessage);
44 const
45 cnMaxFileNameLen = 255;
46 var
47 i,
48 nCount: integer;
49 acFileName: array[0..cnMaxFileNameLen] of char;
50 begin
51 // find out how many files we're accepting
52 nCount := DragQueryFile(msg.WParam,
53 $FFFFFFFF,
54 acFileName,
55 cnMaxFileNameLen);
56
57 // query Windows one at a time for the file name
58 for i := 0 to nCount - 1 do
59 begin
60 DragQueryFile(msg.WParam, i,
61 acFileName, cnMaxFileNameLen);
62
63 // do your thing with the acFileName
64 MessageBox(Handle, acFileName, '', MB_OK);
65 end;
66
67 // let Windows know that you're done
68 DragFinish(msg.WParam);
69 end;
70 //<<<
71
72 procedure TForm1.FormCreate(Sender: TObject);
73 begin
74 //>>>
75 //
76 // tell Windows that you're
77 // accepting drag and drop files
78 //
79 DragAcceptFiles(Handle, True);
80 //<<<
81 end;
82
83 end.
Now you can drag and drop files on the form that you registered as a recipient of dropped files by calling the "DragAcceptFiles()" function as in the above example.
|