Re:Implementing Drag'n'Drop from external document
Quote
Nils Zonneveld wrote:
> I'm new to Delphi and I want to implement Drag 'n' Drop functionality.
> Drag and Drop within the Application works fine, though I want to Drag
> an external document to the Delphi application to open that document
> within that application.
> It would be even nicer when I could drag a document to the program-icon
> to start the program with the document.
Hi Nils,
You need some code.
Drag/Drop from outside looks like this:
1. Put 'ShellAPI' in Your uses clause .
2. Put this forward declaration in the 'protected' clause of the header:
type
TForm1 = class(TForm)
protected
procedure WMDROPFILES (var Msg: TMessage); message WM_DROPFILES;
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
3. Put the routine in the implementation part:
procedure TForm1.WMDROPFILES (var Msg: TMessage);
var i,counts,size : integer;
theFileName : PChar;
begin
inherited;
counts := DragQueryFile(Msg.WParam, $FFFFFFFF, theFileName, 255);
for i := 0 to (counts - 1) do begin
size := DragQueryFile(Msg.WParam, i , nil, 0) + 1;
theFileName:= StrAlloc(size);
DragQueryFile(Msg.WParam,i , theFileName, size);
Form1.Caption := StrPas(theFileName);
StrDispose(theFileName);
end;
DragFinish(Msg.WParam);
end;
4. Add one line to the OnCreate event:
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Form1.Handle, true);
end;
That's it. At the forms caption You should see the docs path and file
name in the long file name convention.
For drag a document to the program-icon to start it look at the online
help for the ParamCount and ParamStr functions:
procedure TForm1.FormClick(Sender: TObject);
begin
if ParamCount = 0 then
Canvas.TextOut(10, 10, 'No parameters on command line')
else
Canvas.TextOut(10, 10, ParamStr(1));
end;
This will give your app the doc path and name in the 8.3 chars
convention!! If You need to convert it to the long file name ask me for
some more code :-)
Pat