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