这个技巧是参考Delphi的剪贴板类的实现来完成的。将一个流的内容放入剪贴板,
首先要注册你自已的格式,使用RegisterClipboardFormat()函数
然后做下面三步:
1.创建一个内容流,并将内容写进去
2.创建一个全局的内容区,并将流的内容写入
3.调用ClipBoard.SetAsHandle()将内容写入剪贴板
将内容写入剪贴板中
var
hbuf : THandle;
bufptr : Pointer;
mstream : TMemoryStream;
begin
mstream := TMemoryStream.Create;
try
{-- 处理流的代码 --}
hbuf := GlobalAlloc(GMEM_MOVEABLE, mstream.size);
try
bufptr := GlobalLock(hbuf);
try
Move(mstream.Memory^, bufptr^, mstream.size);
Clipboard.SetAsHandle(CF_MYFORMAT, hbuf);
finally
GlobalUnlock(hbuf);
end;
except
GlobalFree(hbuf);
raise;
end;
finally
mstream.Free;
end;
end;
请注意不要将分配的全局缓冲区释放,这个工作由剪贴板来完成,在读出数据中
你应该将它复制后处理。
将剪贴板内容读出来
var
hbuf : THandle;
bufptr : Pointer;
mstream : TMemoryStream;
begin
hbuf := Clipboard.GetAsHandle(CF_MYFORMAT);
if hbuf <> 0 then begin
bufptr := GlobalLock(hbuf);
if bufptr <> nil then begin
try
mstream := TMemoryStream.Create;
try
mstream.WriteBuffer(bufptr^, GlobalSize(hbuf));
mstream.Position := 0;
{-- 处理流的代码 --}
finally
mstream.Free;
end;
finally
GlobalUnlock(hbuf);
end;
end;
end;
end;
********************************************
gototop99 (巴蒂) :怎样拷贝 特殊记录? 200分相送!!!
如:拷贝
TMyddd=record
a1:string;
a2:integer;
end;
类型的记录到 剪贴板中,怎么做啊??
回复人: laihecongxi(兴哥) ( ) 信誉:100 2002-12-02 11:22:00 得分:0
//delphi6测试通过
type
TMyddd=record
a1:string;
a2:integer;
end;
................
var
Myddd : Tmyddd;
pMyddd : ^Tmyddd;
procedure .............................
var
mHnd : Thandle;
begin
//记录赋值
Myddd.a1 := 'AAA';
Myddd.a2 := 222;
//存数据
OurFormat:=RegisterClipboardFormat('CF_TMyddd');
if OpenClipboard(Handle) then
EmptyClipboard;
mHnd := GlobalAlloc(GMEM_DDESHARE or GMEM_MOVEABLE,SizeO(TMyddd));
pMyddd := GlobalLock(mHnd);
new(pMyddd);
pMyddd^.a1 := Myddd.a1;
pMyddd^.a2 := Myddd.a2;
GlobalUnLock(mHnd);
SetClipboardData(OurFormat,mHnd);
end;
var MemberInClip : Thandle;
begin //读数据
if Clipboard.HasFormat(OurFormat) then begin
if OpenClipboard(Handle) then
begin
MemberInClip:=GetClipboardData(OurFormat);
//new(PMyddd);
//pMyddd := GlobalLock(MemberInClip);
Myddd.a1 := pMyddd^.a1;
Myddd.a2 := pMyddd^.a2;
GlobalUnLock(MemberInClip);
CloseClipboard();
with Memo1.Lines do begin
Clear;
Add('Clipboard has TMember data:');
Add(Myddd.a1);
Add(inttostr(Myddd.a2));
end;
end;
end;
end;