求助!请问如何将剪贴板的文件粘贴到指定路径目录下,要求编程实现(100分)

  • 主题发起人 主题发起人 小小小虾米
  • 开始时间 开始时间

小小小虾米

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何将剪贴板的文件粘贴到指定路径目录下,要求编程实现,
希望各位大侠能帮小弟一把!谢谢!
 
没人会啊!是不是分不够,不够可以加.
 
就是模拟文件的复制,粘贴.但我不希望用copyfile等函数实现,想用粘贴板实现.
 
对剪切板和流之间的数据交换:
copy the clipboard to a stream and restore it again?
uses
clipbrd;

procedure CopyStreamToClipboard(fmt: Cardinal;
S: TStream);
var
hMem: THandle;
pMem: Pointer;
begin

Assert(Assigned(S));
S.Position := 0;
hMem := GlobalAlloc(GHND or GMEM_DDESHARE, S.Size);
if hMem <> 0 then

begin

pMem := GlobalLock(hMem);
if pMem <> nil then

begin

try
S.Read(pMem^, S.Size);
S.Position := 0;
finally
GlobalUnlock(hMem);
end;

Clipboard.Open;
try
Clipboard.SetAsHandle(fmt, hMem);
finally
Clipboard.Close;
end;

end { If }
else

begin

GlobalFree(hMem);
OutOfMemoryError;
end;

end { If }
else

OutOfMemoryError;
end;
{ CopyStreamToClipboard }
procedure CopyStreamFromClipboard(fmt: Cardinal;
S: TStream);
var
hMem: THandle;
pMem: Pointer;
begin

Assert(Assigned(S));
hMem := Clipboard.GetAsHandle(fmt);
if hMem <> 0 then

begin

pMem := GlobalLock(hMem);
if pMem <> nil then

begin

try
S.Write(pMem^, GlobalSize(hMem));
S.Position := 0;
finally
GlobalUnlock(hMem);
end;

end { If }
else

raise Exception.Create('CopyStreamFromClipboard: could not lock global handle ' +
'obtained from clipboard!');
end;
{ If }
end;
{ CopyStreamFromClipboard }
procedure SaveClipboardFormat(fmt: Word;
writer: TWriter);
var
fmtname: array[0..128] of Char;
ms: TMemoryStream;
begin

Assert(Assigned(writer));
if 0 = GetClipboardFormatName(fmt, fmtname, SizeOf(fmtname)) then

fmtname[0] := #0;
ms := TMemoryStream.Create;
try
CopyStreamFromClipboard(fmt, ms);
if ms.Size > 0 then

begin

writer.WriteInteger(fmt);
writer.WriteString(fmtname);
writer.WriteInteger(ms.Size);
writer.Write(ms.Memory^, ms.Size);
end;
{ If }
finally
ms.Free
end;
{ Finally }
end;
{ SaveClipboardFormat }
procedure LoadClipboardFormat(reader: TReader);
var
fmt: Integer;
fmtname: string;
Size: Integer;
ms: TMemoryStream;
begin

Assert(Assigned(reader));
fmt := reader.ReadInteger;
fmtname := reader.ReadString;
Size := reader.ReadInteger;
ms := TMemoryStream.Create;
try
ms.Size := Size;
reader.Read(ms.memory^, Size);
if Length(fmtname) > 0 then

fmt := RegisterCLipboardFormat(PChar(fmtname));
if fmt <> 0 then

CopyStreamToClipboard(fmt, ms);
finally
ms.Free;
end;
{ Finally }
end;
{ LoadClipboardFormat }
procedure SaveClipboard(S: TStream);
var
writer: TWriter;
i: Integer;
begin

Assert(Assigned(S));
writer := TWriter.Create(S, 4096);
try
Clipboard.Open;
try
writer.WriteListbegin
;
for i := 0 to Clipboard.formatcount - 1do

SaveClipboardFormat(Clipboard.Formats, writer);
writer.WriteListend;

finally
Clipboard.Close;
end;
{ Finally }
finally
writer.Free
end;
{ Finally }
end;
{ SaveClipboard }
procedure LoadClipboard(S: TStream);
var
reader: TReader;
begin

Assert(Assigned(S));
reader := TReader.Create(S, 4096);
try
Clipboard.Open;
try
clipboard.Clear;
reader.ReadListbegin
;
while not reader.EndOfListdo

LoadClipboardFormat(reader);
reader.ReadListend;

finally
Clipboard.Close;
end;
{ Finally }
finally
reader.Free
end;
{ Finally }
end;
{ LoadClipboard }

// Examples:
{ Save Clipboard }
procedure TForm1.Button1Click(Sender: TObject);
var
ms: TMemoryStream;
begin

ms := TMemoryStream.Create;
try
SaveClipboard(ms);
ms.SaveToFile('c:/temp/ClipBrdSaved.dat');
finally
ms.Free;
end;
{ Finally }
end;

{ Clear Clipboard }
procedure TForm1.Button2Click(Sender: TObject);
begin

clipboard.Clear;
end;

{ Restore Clipboard }
procedure TForm1.Button3Click(Sender: TObject);
var
fs: TfileStream;
begin

fs := TFilestream.Create('c:/temp/ClipBrdSaved.dat',
fmopenread or fmsharedenynone);
try
LoadClipboard(fs);
finally
fs.Free;
end;
{ Finally }
end;
 
szhcracker:
我按你的方法做了,但不是很明白.
比如我想把一个目录下的某个文件复制到另一个目录下应该怎么做呢?
谢谢!
 
szhcracker:
还在吗?
 
没人回答了啊
 
和目录没关系,关键是你应该首先把源文件以流的方式读到剪贴板,然后再把剪贴板上的流写到一个目标文件即可。
 
谢谢szhcracker,结帖了!
 
后退
顶部