unit Main;
{$MODE Delphi}
interface
uses
DragDrop,
DropTarget,
DragDropFile,
LCLIntf, LCLType, LMessages, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, DropSource, Types, Windows, JwaPsApi;
type
{ TForm1 }
TForm1 = class(TForm)
DropDummy1: TDropDummy;
DropFileSource1: TDropFileSource;
DropFileTarget1: TDropFileTarget;
ListView1: TListView;
procedure DropFileSource1GetDragImage(Sender: TObject;
const DragSourceHelper: IDragSourceHelper; var Handled: boolean);
procedure DropFileTarget1Drop(Sender: TObject; ShiftState: TShiftState;
Point: TPoint; var Effect: Integer);
procedure DropFileTarget1Enter(Sender: TObject; ShiftState: TShiftState;
APoint: TPoint; var Effect: Longint);
procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ActiveX, CommCtrl;
{$R *.lfm}
procedure TForm1.DropFileSource1GetDragImage(Sender: TObject;
const DragSourceHelper: IDragSourceHelper; var Handled: boolean);
var
Pt: TPoint;
begin
GetCursorPos(Pt);
Handled := Succeeded(DragSourceHelper.InitializeFromWindow(Listview1.Handle, Pt, TCustomDropSource(Sender) as IDataObject));
end;
procedure TForm1.DropFileTarget1Drop(Sender: TObject;
ShiftState: TShiftState; Point: TPoint; var Effect: Integer);
var
i: integer;
Item: TListItem;
begin
// called when the user drag and drop files onto your application.
// Display mapped names if present.
// Mapped names are usually only present when dragging from the recycle bin
// (try it).
if (DropFileTarget1.MappedNames.Count > 0) then
Listview1.Columns[1].Width := 100
else
Listview1.Columns[1].Width := 0;
// Copy the file names from the DropTarget component into the list view.
for i := 0 to DropFileTarget1.Files.Count-1 do
begin
Item := Listview1.Items.Add;
Item.Caption := DropFileTarget1.Files[i];
// Display mapped names if present.
if (DropFileTarget1.MappedNames.Count > i) then
Item.SubItems.Add(DropFileTarget1.MappedNames[i]);
end;
// Reject "moved" files
if (Effect = DROPEFFECT_MOVE) then
Effect := DROPEFFECT_NONE;
end;
procedure TForm1.DropFileTarget1Enter(Sender: TObject; ShiftState: TShiftState;
APoint: TPoint; var Effect: Longint);
var
Window: HWND;
ProcessID: Dword;
H: Handle;
buffer: array[0..MAX_PATH-1] of WideChar;
Size: DWord;
AppExe: String;
begin
Window := GetCapture;
if Window = 0 then Exit;
GetWindowThreadProcessId(Window, ProcessId);
H := OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, ProcessID);
size := MAX_PATH;
Windows.QueryFullProcessImageNameW(H, 0, buffer, @Size);
AppExe := buffer;
if AppExe = Application.ExeName then Effect := DROPEFFECT_NONE;
end;
procedure TForm1.ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var i: integer;
begin
// Wait for user to move mouse before we start the drag/drop.
// Note:
// Due to some internal mouse message juggling inside TListView we will not
// get the MouseDown event until the mouse is either moved or the mouse button
// is released.
// Remember this when it appears that DragDetectPlus isn't working...
if (Listview1.SelCount > 0) and (DragDetectPlus(TWinControl(Sender))) then
begin
// Delete anything from a previous drag.
DropFileSource1.Files.Clear;
// Fill DropSource1.Files with selected files from ListView1.
for i := 0 to Listview1.Items.Count-1 do
if (Listview1.items.Item[i].Selected) then
DropFileSource1.Files.Add(Listview1.items.Item[i].Caption);
// Start the drag operation.
DropFileSource1.Execute;
end;
end;
end.