如何将粘贴板上的任意内容保存到一个变量,然后在以后使用中恢复到粘贴板.(100分)

  • 主题发起人 主题发起人 kingkong
  • 开始时间 开始时间
K

kingkong

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将粘贴板上的任意内容保存到一个变量,然后在以后使用中恢复到粘贴板.
 
举例:
选择Edit1的文本拷贝到粘贴板
Edit1.SelectAll;
Edit1.CopyToClipboard;
将粘贴板上的任意内容保存到Edit2
Edit2.PasteFromClipboard;

 
to yostgxf:
粘贴板不一定就是文本内容,有可能是图象,这怎么保存?
 
to yostgxf:
而且你的办法是将EDIT的内容放到粘贴板,我要的是将粘贴板的已有的内容放入变量,根本不是一会事嘛.
 
楼上的代码只考虑了字符,好象还有图象,其他的xxx
所以我觉的要用流之类的东东
 
Edit2.PasteFromClipboard;
str:=edit2.text;
 
这二个函数还可以用于TDBImage的。
至少我现在还不知道有现成的。
 
对剪切板和流之间的数据交换
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 - 1 do
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.EndOfList do
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:.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:.dat',
fmopenread or fmsharedenynone);
try
LoadClipboard(fs);
finally
fs.Free;
end
{ Finally }
end;
 


 
复制任意文件或文件夹到剪贴板
uses
ShlObj, ClipBrd;
procedure CopyFilesToClipboard(FileList: string);
var
DropFiles: PDropFiles;
hGlobal: THandle;
iLen: Integer;
begin
iLen := Length(FileList) + 2;
FileList := FileList + #0#0;
hGlobal := GlobalAlloc(GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT,
SizeOf(TDropFiles) + iLen);
if (hGlobal = 0) then raise Exception.Create('Could not allocate memory.');
begin
DropFiles := GlobalLock(hGlobal);
DropFiles^.pFiles := SizeOf(TDropFiles);
Move(FileList[1], (PChar(DropFiles) + SizeOf(TDropFiles))^, iLen);
GlobalUnlock(hGlobal);
Clipboard.SetAsHandle(CF_HDROP, hGlobal);
end;
end;
// Example:
procedure TForm1.Button1Click(Sender: TObject);
begin
CopyFilesToClipboard('C:.Txt'#0'C:.Bat');
end;
{
Separate the files with a #0.
}
********************************
沈前卫的回答:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,ShlObj;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
const FileName:string='c:.txt';
var
DataHandle: THandle;
DataPointer: PDROPFILES;
begin
DataHandle := GlobalAlloc(GMEM_DDESHARE or GMEM_MOVEABLE,SizeOf(DROPFILES)+2+Length(FileName));
DataPointer := PDROPFILES(GlobalLock(DataHandle));
FillChar(DataPointer^,SizeOf(DROPFILES)+2+Length(FileName),0);
DataPointer.pFiles:=SizeOf(DROPFILES);
DataPointer.pt:=Point(0,0);
DataPointer.fNC:=False;
DataPointer.fWide:=False;
Move(FileName[1],Pointer(Integer(DataPointer)+SizeOf(DROPFILES))^,Length(FileName));
GlobalUnlock(DataHandle);
OpenClipboard(Form1.Handle);
EmptyClipboard;
SetClipboardData(CF_HDROP, DataHandle);
CloseClipboard;
end;
end.
***************************
在Windows的资源管理器中,选中一个或多个文件,在文件上单击鼠标右键,在弹出菜单中选复制。再切换到另外的目录,单击鼠标右键,点粘贴。就这样执行了一次文件的拷贝操作,那么Windows在拷贝过程中执行了什么操作,是否将整个文件拷贝到剪贴板上了呢?当然没有。实际上,Windows只是将一个文件结构拷贝到了剪贴版,这个结构如下:
   tDropFile+文件1文件名+vbNullChar+文件2文件名+vbNullChar……+文件N文件名+vbNullChar,其中tDropFile是一个DROPFILES结构,这个结构在Windows API中有定义。在粘贴文件时,利用API函数 DragQueryFile 就可以获得拷贝到剪贴板的文件全路径名,然后就可以根据获得的文件名执行文件拷贝函数,实现对文件的粘贴操作。
那么如何从剪切板或取复制的文件内容呢?请参看下面的例子:
/// Author:Peter Below
uses
clipbrd, shellapi;
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
f: THandle;
buffer: array [0..MAX_PATH] of Char;
i, numFiles: Integer;
begin
if not Clipboard.HasFormat(CF_HDROP) then Exit;
Clipboard.Open;
try
f := Clipboard.GetAsHandle(CF_HDROP);
if f <> 0 then
begin
numFiles := DragQueryFile(f, $FFFFFFFF, nil, 0);
memo1.Clear;
for i := 0 to numfiles - 1 do
begin
buffer[0] := #0;
DragQueryFile(f, i, buffer, SizeOf(buffer));
memo1.Lines.Add(buffer);
end;
end;
finally
Clipboard.Close;
end;
end;
 
多人接受答案了。
 
后退
顶部