unit dragf; (* dragf - example to show how to accept filenames form other application. This form accepts draggin from other program cabable to drag filenames (f.ex Explorer). The names are just shown in a listbox. In real application replace ListBox.Items.Add(name) by something real, like opening the file. This form also demonstrates the use of Ander Melander's excellent Drag and Drop component Suite http://www.melander.dk. Vesa Lappalainen 28.7.1999 *) interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ToolWin, ComCtrls, DropSource, DropTarget, kDragTarget; type TFormDrag = class(TForm) ListBoxNames: TListBox; DropFileSource1: TDropFileSource; DropFileTarget1: TDropFileTarget; List1: TListBox; Target1: TkDragTarget; TargetForm: TkDragTarget; ToolBar1: TToolBar; ButtonClear: TButton; ButtonAccept: TButton; ButtonFromAccept: TButton; procedure ButtonClearClick(Sender: TObject); procedure ListBoxNamesMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormCreate(Sender: TObject); procedure DropFileTarget1Drop(Sender: TObject; ShiftState: TShiftState; Point: TPoint; var Effect: Integer); procedure Target2Drop(Sender: TkDragTarget; pt:TPoint); procedure TargetFormDrop(Sender: TkDragTarget; pt:TPoint); procedure ButtonAcceptClick(Sender: TObject); procedure ButtonFromAcceptClick(Sender: TObject); private AlreadyDragging : boolean; DragPoint: TPoint; public end; var FormDrag: TFormDrag; implementation {$R *.DFM} procedure TFormDrag.ButtonClearClick(Sender: TObject); begin ListBoxNames.Clear; List1.Clear; end; procedure TFormDrag.ListBoxNamesMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var i: integer; ListView1 : TListBox; begin if ( not (Sender is TListBox) ) then exit; ListView1 := TListBox(Sender); if AlreadyDragging then exit; //Filter out all but the mouse buttons from 'Shift'... Shift := (Shift * [ssLeft,ssRight]); //Make sure a mouse button is pressed before starting the drag ... //and the mouse has moved at least 10 pixels //(DragPoint is set in the ListView1MouseDown event) if (Shift = []) or ((abs(DragPoint.X - X) <10) and (abs(DragPoint.Y - Y) <10)) then exit; //If no files selected then exit... if Listview1.SelCount = 0 then exit; //Delete any filenames left from a previous dragdrop... DropFileSource1.Files.clear; //Fill DropSource1.Files with selected filenames in ListView1... for i := 0 to Listview1.items.Count-1 do if (Listview1.Selected[i]) then DropFileSource1.Files.Add(Listview1.items[i]); //Stop this event being re-executed once a drag has been initiated... AlreadyDragging := true; //Start the dragdrop... //Note: DropFileSource1.DragTypes = [dtCopy] set at design time. DropFileSource1.execute; //Note: Execute does not return until the drop has finished - //either sucessfully with a copy,move of link operation or cancelled. AlreadyDragging := false; end; procedure TFormDrag.FormCreate(Sender: TObject); begin DropFileTarget1.register(ListBoxNames); end; procedure TFormDrag.DropFileTarget1Drop(Sender: TObject; ShiftState: TShiftState; Point: TPoint; var Effect: Integer); var i,n:integer; drop:TDropFileTarget; begin if ( not ( Sender is TDropFileTarget ) ) then exit; drop := TDropFileTarget(Sender); n := drop.Files.Count; for i:=0 to n-1 do begin ListBoxNames.Items.Add(drop.files[i]); // Add the name to listbox end; end; procedure TFormDrag.Target2Drop(sender: TkDragTarget; pt:TPoint); var i:integer; List : TListBox; begin if not ( sender.Window is TListBox ) then exit; List := TListBox(sender.Window); for i := 0 to sender.Count-1 do List.Items.Add(sender.DropName[i]); end; procedure TFormDrag.TargetFormDrop(Sender: TkDragTarget; pt:TPoint); begin Sender.AddTo(ListBoxNames.Items); end; procedure TFormDrag.ButtonAcceptClick(Sender: TObject); begin Target1.Accept := not Target1.Accept; end; procedure TFormDrag.ButtonFromAcceptClick(Sender: TObject); begin TargetForm.Accept := not TargetForm.Accept; end; end.