procedure TForm1.DoDroped(Sender: TObject; Msg: Pchar);
begin
memo1.Lines.Add(msg)
end;
procedure TForm1.FormCreate(Sender: TObject);
var
dd: TTMyDrop;
res : HResult;
res1: HResult;
begin
OleInitialize(NIL);
dd := TTMyDrop.Create;
dd.OnDroped:=DoDroped;
res1 := CoLockObjectExternal(dd, true, false);
res := RegisterDragDrop(Handle, IDropTarget(dd));
//DragAcceptFiles(handle,True);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
RevokeDragDrop(Handle);
OleUninitialize;
end;
procedure TForm1.WMDropFiles(var msg: TWMDropFiles);
var
numfile: integer;
i: integer;
buffer: array[0..255] of char;
begin
numfile := dragqueryfile(msg.Drop,0,nil,0);
for i := 0 to numfile-1 do
DragQueryFile(msg.Drop,i,@buffer,sizeof(buffer));
Memo1.Lines.Add(buffer);
end;
function TTMyDrop.DragEnter(const dataObj: IDataObject;
grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
var
enumFormatEtc: IEnumFormatEtc;
f:TFORMATETC;
count:Integer;
Found:boolean;
begin
dataObj.EnumFormatEtc(DATADIR_GET,enumFormatEtc);
Found:=false;
while (enumFormatEtc.Next(1,f,@count)=S_OK)and (count>0) do
begin
if (f.cfFormat=CF_TEXT) then
begin
Found:=true;
Break;
end;
end;
if Found then
Result:=S_OK
else
begin
result:=E_INVALIDARG;
dwEffect:=DROPEFFECT_NONE;
end;
end;
function TTMyDrop.DragLeave: HResult;
begin
result := S_OK;
end;
function TTMyDrop.DragOver(grfKeyState: Integer; pt: TPoint;
var dwEffect: Integer): HResult;
begin
result := S_OK;
end;
function TTMyDrop.Drop(const dataObj: IDataObject; grfKeyState: Integer;
pt: TPoint; var dwEffect: Integer): HResult;
var
enumFormatEtc: IEnumFormatEtc;
f:TFORMATETC;
count:Integer;
Found:boolean;
medium: TStgMedium;
begin
dataObj.EnumFormatEtc(DATADIR_GET,enumFormatEtc);
Found:=false;
while (enumFormatEtc.Next(1,f,@count)=S_OK)and (count>0) do
begin
if (f.cfFormat=CF_TEXT) then
begin
Found:=true;
Break;
end;
end;
if not Found then
begin
result:=E_INVALIDARG;
dwEffect:=DROPEFFECT_NONE;
Exit;
end;
dataObj.GetData(f,medium);
if medium.tymed =1 then
begin
if Assigned(fOnDroped) then
begin
fOnDroped(Self,PChar(GlobalLock(medium.hglobal)));
GlobalUnLock(medium.hglobal);
end;
result := S_OK;
end;
end;
procedure TTMyDrop.SetOnDroped(const Value: TDropEvent);
begin
FOnDroped := Value;
end;