unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls,
ActiveX, ShlObj, ComObj, StdCtrls;
type
TfrmTPDrop =
class(TForm, IDropTarget)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
function DragEnter(
const dataObj: IDataObject;
grfKeyState: Longint;
pt: TPoint;
var dwEffect: Longint): HResult;
stdcall;
function DragOver(grfKeyState: Longint;
pt: TPoint;
var dwEffect: Longint): HResult;
stdcall;
function DragLeave: HResult;
stdcall;
function Drop(
const dataObj: IDataObject;
grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HResult;
stdcall;
function _AddRef: Integer;
stdcall;
function _Release: Integer;
stdcall;
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
frmTPDrop: TfrmTPDrop;
implementation
{$R *.dfm}
uses
ShellAPI;
procedure SaveDropDataToFile(Data:Pointer;Filename:
String);
var
fs: TFileStream;
sa: IStream;
begin
fs := TFileStream.Create(Filename,fmCreate);
try
sa := TStreamAdapter.Create(fs, soReference)
as IStream;
sa.
Read(Data,SizeOf(Data),0);
finally
fs.Free;
end;
end;
function TfrmTPDrop.DragEnter(
const dataObj: IDataObject;
grfKeyState: Integer; pt: TPoint;
var dwEffect: Integer): HResult;
begin
dwEffect := DROPEFFECT_COPY;
Result := S_OK;
end;
function TfrmTPDrop.DragLeave: HResult;
begin
Result := S_OK;
end;
function TfrmTPDrop.DragOver(grfKeyState: Integer; pt: TPoint;
var dwEffect: Integer): HResult;
begin
dwEffect := DROPEFFECT_COPY;
Result := S_OK;
end;
function TfrmTPDrop.Drop(
const dataObj: IDataObject; grfKeyState: Integer;
pt: TPoint;
var dwEffect: Integer): HResult;
var
aFmtEtc: TFORMATETC;
aStgMed: TSTGMEDIUM;
pData: Pointer;
begin
if (dataObj =
nil)
then
raise Exception.Create('
IDataObject-Pointer is not valid!');
with aFmtEtc
do
begin
cfFormat := CF_RIFF ;
ptd :=
nil;
dwAspect := DVASPECT_CONTENT;
lindex := -1;
tymed := TYMED_ISTREAM ;
end;
OleCheck(dataObj.GetData(aFmtEtc, aStgMed));
try
pData := GlobalLock(aStgMed.hGlobal);
SaveDropDataToFile(pData,'
c:\stream.doc');
finally
GlobalUnlock(aStgMed.hGlobal);
ReleaseStgMedium(aStgMed);
end;
Result := S_OK;
end;
procedure TfrmTPDrop.FormCreate(Sender: TObject);
begin
OleInitialize(
nil);
OleCheck(RegisterDragDrop(
Handle, Self));
end;
procedure TfrmTPDrop.FormDestroy(Sender: TObject);
begin
RevokeDragDrop(
Handle);
OleUninitialize;
end;
function TfrmTPDrop._AddRef: Integer;
begin
Result := 1;
end;
function TfrmTPDrop._Release: Integer;
begin
Result := 1;
end;
end.