1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| {}
{ Project : crDrop.dpk }
{ Comment : }
{ }
{ Date : 08/12/2008 20:33:02 }
{ Author : Cirec }
{}
{ Last modified }
{ Date : 09/12/2008 01:25:18 }
{ Author : Cirec }
{}
unit crFileDrops;
interface
uses
Windows, SysUtils, Classes, Messages, Controls, ShellApi;
type
TBeforeDrop = procedure(Sender: TObject; out DropPoint: TPoint) of Object;
TAfterDrop = procedure(Sender: TObject; DropContainer: TWinControl; out FileList: TStrings) of Object;
TcrFileDrop = class(TComponent)
private
{ Déclarations privées }
DefWndProc: TWndMethod;
FDropContainer: TWinControl;
FFileList: TStrings;
FOnAfterDrop: TAfterDrop;
FDropPoint: TPoint;
FOnBeforeDrop: TBeforeDrop;
procedure SetDropContainer(const Value: TWinControl);
protected
{ Déclarations protégées }
procedure DropWndProc(var Message: TMessage);
procedure RemoveDropContainer;
procedure SetHackWndProc;
public
{ Déclarations publiques }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property DropPoint: TPoint read FDropPoint {write FDropPoint};
published
{ Déclarations publiées }
property DropContainer: TWinControl read FDropContainer write SetDropContainer;
property OnBeforeDrop: TBeforeDrop read FOnBeforeDrop write FOnBeforeDrop;
property OnAfterDrop: TAfterDrop read FOnAfterDrop write FOnAfterDrop;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Cirec DelphiFr', [TcrFileDrop]);
end;
{ TcrFileDrop }
constructor TcrFileDrop.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDropContainer := nil;
DefWndProc := nil;
FFileList := TStringList.Create;
end;
destructor TcrFileDrop.Destroy;
begin
RemoveDropContainer;
FFileList.Free;
inherited Destroy;
end;
procedure TcrFileDrop.DropWndProc(var Message: TMessage);
const
DQF_COUNTFILES = $FFFFFFFF;
var FileCount, FileNameLen, I: integer;
FileName : string;
begin
if message.Msg = WM_DropFiles then
begin
FFileList.Clear;
//nombre de fichiers dans la queue
FileCount := DragQueryFile(message.wParam, DQF_COUNTFILES, nil, 0);
// récupère la position de la souris lors du Drop si besoin
DragQueryPoint(message.wParam, FDropPoint);
for I := 0 to pred(FileCount) do
begin
// Longueur du nom du fichier
FileNameLen := DragQueryFile(message.wParam, I, Nil, 0) + 1;
// ajuste la taille
SetLength(FileName, FileNameLen);
// récupère les données
DragQueryFile(message.wParam, I, PChar(FileName), FileNameLen);
// ajout à la Liste
FFileList.Add(FileName);
end;
// fin du drop
DragFinish(message.wParam);
if Assigned(FOnBeforeDrop) then
FOnBeforeDrop(Self, FDropPoint);
// si l'évènement OnAfterDrop est assigné et si il y a au moins
// un fichier dans la liste on le déclanche
if Assigned(FOnAfterDrop) and (FileCount > 0) then
FOnAfterDrop(Self, FDropContainer, FFileList);
end
else
// traitement par défaut
DefWndProc(message);
end;
procedure TcrFileDrop.RemoveDropContainer;
begin
if not (csDesigning in ComponentState) and Assigned(FDropContainer)
and Assigned(DefWndProc) then
begin
FDropContainer.WindowProc := DefWndProc;
if not (csDestroying in ComponentState) then
DragAcceptFiles(FDropContainer.Handle, False);
FDropContainer := nil;
DefWndProc := nil;
end;
end;
procedure TcrFileDrop.SetDropContainer(const Value: TWinControl);
begin
if Value <> FDropContainer then
begin
RemoveDropContainer;
FDropContainer := Value;
SetHackWndProc;
end;
end;
procedure TcrFileDrop.SetHackWndProc;
begin
if Assigned(FDropContainer) and not (csDesigning in ComponentState) then
begin
DefWndProc := FDropContainer.WindowProc;
FDropContainer.WindowProc := DropWndProc;
DragAcceptFiles(FDropContainer.Handle, true);
end;
end;
end. |
Partager