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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComObj, ActiveX, ShlObj;
type
// Form that acts as IDropTarget
TForm1 = class(TForm, IDropTarget)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
// Private declarations
function SaveAsStg(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
function SaveAsStm(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
function SaveAsMem(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
protected
// Protected declarations
function DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; stdcall;
function DragOver(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; reintroduce; stdcall;
function DragLeave: HResult; stdcall;
function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult; stdcall;
public
// Public declarations
end;
// Acceptable format types
const
CF_Acceptable: Array [0..1] of String = ('RenPrivateMessages',
'Internet Message (rfc822/rfc1522)');
// Predefined clipboard formats
const
CF_PredfinedFormats: Array [1..15] of String = ('Text',
'Bitmap',
'MetaFile',
'SYLK',
'DIF',
'TIFF',
'OemText',
'DIB',
'Palette',
'Pen Data',
'RIFF',
'Wave',
'Unicode Text',
'Enhanced Metafile',
'HDrop');
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.DragEnter(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult;
var pEFETC: IEnumFORMATETC;
FmtETC: tagFORMATETC;
lpszFormat: Array [0..255] of Char;
szFormat: String;
dwAllow: Integer;
dwfetch: Integer;
bAllow: Boolean;
begin
// Clear the format string
szFormat:='';
// Determine if we want to support the drop of this type
bAllow:=False;
if (dataObj.EnumFormatEtc(DATADIR_GET, pEFETC) = S_OK) then
begin
// Start the fetch enumeration
while (pEFETC.Next(1, FmtETC, @dwfetch) = S_OK) do
begin
// Attempt to get the clipboard format name
if (FmtETC.cfFormat in [1..15]) then
szFormat:=CF_PredfinedFormats[FmtETC.cfFormat]
else if (GetClipboardFormatName(FmtETC.cfFormat, lpszFormat, SizeOf(lpszFormat)) > 0) then
szFormat:=lpszFormat
else
szFormat:=IntToStr(FmtETC.cfFormat);
// Compare formats
for dwAllow:=0 to High(CF_Acceptable) do
begin
if (CompareText(CF_Acceptable[dwAllow], szFormat) = 0) then
begin
bAllow:=True;
if bAllow then break;
end;
end;
if bAllow then break;
end;
// Release the enumerator
pEFETC:=nil;
end;
// Should we allow the enter?
result:=NOERROR;
if not(bAllow) then dwEffect:=DROPEFFECT_NONE;
end;
function TForm1.DragOver(grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult;
begin
// Allow the drag over
result:=NOERROR;
end;
function TForm1.DragLeave: HResult;
begin
// Allow the drag leave
result:=NOERROR;
end;
function TForm1.SaveAsStg(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
var medium: TStgMedium;
pvStg: IStorage;
begin
// Set default result
result:=False;
// Set tymed
fmt.tymed:=TYMED_ISTORAGE;
// Get the data
if (dataObj.GetData(fmt, medium) = S_OK) then
begin
// Create storage file
if (StgCreateDocfile(PWideChar(WideString(FileNameBase+'.msg')),
STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE , 0, pvStg) = S_OK) then
begin
// Copy from the dropped storage
result:=(IStorage(medium.stg).CopyTo(0, nil, nil, pvStg) = S_OK);
// Release the storage interface
pvStg:=nil;
end;
// We are responsible for releasing the storage medium
IStorage(medium.stg):=nil;
end;
end;
function TForm1.SaveAsStm(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
var msStream: TMemoryStream;
pvStm: IStream;
medium: TStgMedium;
stat: TStatStg;
dwSize: Integer;
begin
// Set default result
result:=False;
// Set tymed
fmt.tymed:=TYMED_ISTREAM;
// Get the data
if (dataObj.GetData(fmt, medium) = S_OK) then
begin
// Get the stream
pvStm:=IStream(medium.stm);
// Stat to get the size
if (pvStm.Stat(stat, STATFLAG_NONAME) = S_OK) then
begin
// Create memory stream for output
msStream:=TMemoryStream.Create;
msStream.Size:=stat.cbSize;
if (IStream(medium.stm).Read(msStream.Memory, stat.cbSize, @dwSize) = S_OK) then
begin
msStream.Size:=dwSize;
msStream.Position:=0;
msStream.SaveToFile(FileNameBase+'.eml');
result:=True;
end;
msStream.Free;
end;
// Release the stream interface
IStream(medium.stm):=nil;
end;
end;
function TForm1.SaveAsMem(FileNameBase: String; dataObj: IDataObject; fmt: tagFORMATETC): Boolean;
var msStream: TMemoryStream;
medium: TStgMedium;
stat: TStatStg;
lpMem: Pointer;
dwSize: Integer;
begin
// Set default result
result:=False;
// Set tymed
fmt.tymed:=TYMED_HGLOBAL;
// Get the data
if (dataObj.GetData(fmt, medium) = S_OK) then
begin
// Get the memory and lock it
lpMem:=GlobalLock(medium.hGlobal);
// Create memory stream for output
msStream:=TMemoryStream.Create;
msStream.Write(lpMem^, GlobalSize(medium.hGlobal));
msStream.SaveToFile(FileNameBase+'.eml');
result:=True;
msStream.Free;
// Unlock and free the memory
GlobalUnlock(medium.hGlobal);
GlobalFree(medium.hGlobal);
end;
end;
function TForm1.Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint; var dwEffect: Longint): HResult;
var pEFETC: IEnumFORMATETC;
FmtETC: tagFORMATETC;
msStream: TMemoryStream;
cfEml: Integer;
cfFile: Integer;
pvStg: IStorage;
medium: TStgMedium;
bHandle: Boolean;
dwFetch: Integer;
i64Size: Int64;
i64Move: Int64;
begin
// Set result
result:=NOERROR;
// Enumerate until we get are able to save the contents
bHandle:=False;
cfFile:=RegisterClipboardFormat(CFSTR_FILECONTENTS);
cfEml:=RegisterClipboardFormat('Internet Message (rfc822/rfc1522)');
if (dataObj.EnumFormatEtc(DATADIR_GET, pEFETC) = S_OK) then
begin
// Start the fetch enumeration
while (pEFETC.Next(1, FmtETC, @dwfetch) = S_OK) do
begin
// Check file contents
if (FmtETC.cfFormat = cfFile) then
begin
// Try as storage first, then stream
if ((FmtETC.cfFormat and TYMED_ISTORAGE) = TYMED_ISTORAGE) then
begin
if SaveAsStg('Test', dataObj, FmtEtc) then break;
end;
if ((FmtETC.cfFormat and TYMED_ISTREAM) = TYMED_ISTREAM) then
begin
if SaveAsStm('Test', dataObj, FmtEtc) then break;
end;
end
// Check for eml
else if (FmtETC.cfFormat = cfEml) then
begin
if SaveAsMem('Test', dataObj, FmtEtc) then break;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Register the form as a drop target
RegisterDragDrop(Handle, Self);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// Revoke the form as a drop target
RevokeDragDrop(Handle);
end;
initialization
// Initialize ole
OleInitialize(nil);
finalization
// Uninitialize ole
OleUninitialize;
end. |
Partager