首页  编辑  

保存剪切版内容

Tags: /超级猛料/OS.操作系统/Clipboard.剪贴板/   Date Created:

如何将剪贴板的数据保存到一个临时文件中,但是不损坏其格式。

例如:在WORD中复制一个表格,可以将其保存到临时文件,需要时

再从文件读取,再粘帖到WORD中,仍然是一个表格。不知那位大侠有

办法?

分不够可以另开帖给。

/// By Kingron

procedure SaveClipoardToStream ( Stream : TStream );

var

 FormatCount , i , iSize : integer ;

 hData : THandle ;

 hMem : Pointer ;

 PrevFormat : Cardinal ;

 Buffer : array [ 0 .. 255 ] of char ;

begin

 PrevFormat := 0 ;

 OpenClipboard ( GetActiveWindow );

 FormatCount := CountClipboardFormats ;

 Stream . Write ( FormatCount , SizeOf ( FormatCount ));

  for i := 1 to FormatCount do

  begin

   iSize := 0 ;

   PrevFormat := EnumClipboardFormats ( PrevFormat );

   GetClipboardFormatName ( PrevFormat , Buffer , SizeOf ( Buffer ));

   SaveStringToStream ( Stream , StrPas ( Buffer ));

   hData := GetClipboardData ( PrevFormat );

    if hData <> 0 then

    begin

     hMem := GlobalLock ( hData );

     iSize := GlobalSize ( hData );

     Stream . Write ( iSize , SizeOf ( iSize ));

     Stream . Write ( hMem ^, iSize );

     GlobalUnlock ( hData );

    end

    else

    begin

     Stream . Write ( iSize , SizeOf ( iSize ));

    end ;

  end ;

 CloseClipboard ;

end ;

procedure LoadClipoardFromStream ( Stream : TStream );

var

 i , iSize , FormatCount : integer ;

 Format : Cardinal ;

 hMem : THandle ;

 hData : Pointer ;

 FormatName : string ;

begin

 OpenClipboard ( GetActiveWindow );

 Stream . Read ( FormatCount , SizeOf ( FormatCount ));

  for i := 1 to FormatCount do

  begin

   FormatName := LoadStringFromStream ( Stream );

   Format := RegisterClipboardFormat ( PChar ( FormatName ));

   

    if Stream . Read ( iSize , SizeOf ( iSize )) <> SizeOf ( iSize ) then

      raise Exception . Create ( 'Error read stream' );

   hMem := GlobalAlloc ( GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT , iSize );

   hData := GlobalLock ( hMem );

   Stream . Read ( hData ^, iSize );

   GlobalUnlock ( hMem );

   SetClipboardData ( Format , hMem );    

   GlobalFree ( hMem );

  end ;

 CloseClipboard ;

end ;

---------------------------------------

来自:陈健松, 时间:2003-11-10 16:24:00, ID:2284447

干脆贴出来吧,我为你做了一个,试验了一个表格可以使用:

procedure TClipboard.SaveClipDataToStream(var AMem: TMemoryStream);

var

 Data: THandle;

 DataPtr: Pointer;

 MemStream, DataStream: TMemoryStream;

 Writer: TWriter;

 i, tmpInt: Integer;

 tmpStrings: TStringList;

 tmpstr: string;

begin

 clipbrd.Clipboard.Open;

 DataStream := TMemoryStream.Create;

 tmpStrings := TStringList.Create;

 try

   for i := 0 to Clipboard.FormatCount - 1 do

   begin

     Data := GetClipboardData(Clipboard.Formats[i]);

     if Data = 0 then Continue;

     DataPtr := GlobalLock(Data);

     try

       if DataPtr = nil then Continue;

       MemStream := TMemoryStream.Create;

       try

         tmpInt := GlobalSize(Data);

         if tmpInt = 0 then

           beep;

         MemStream.WriteBuffer(DataPtr^, GlobalSize(Data));

         MemStream.Position := 0;

         tmpStrings.Add(IntToStr(Clipboard.Formats[i]) + '=' + IntToStr(GlobalSize(Data)));

         DataStream.WriteBuffer(MemStream.Memory^, MemStream.Size);

       finally

         MemStream.Free;

       end;

     finally

       GlobalUnlock(Data);

     end;

   end;

   if tmpStrings.Count > 0 then

   begin

     AMem.Clear;

     tmpInt := Length(tmpStrings.Text);

     AMem.WriteBuffer(tmpInt, SizeOf(Integer));

     tmpInt := DataStream.size;

     AMem.WriteBuffer(tmpInt, Sizeof(Integer));

     MemStream := TMemoryStream.Create;

     try

       tmpStrings.SaveToStream(MemStream);

       ShowMessage(IntToStr(MemStream.Size));

       AMem.WriteBuffer(MemStream.Memory^, MemStream.Size);

     finally

       MemStream.Free;

     end;

     AMem.WriteBuffer(DataStream.Memory^, DataStream.Size);

   end;

 finally

   clipbrd.Clipboard.Close;

   tmpStrings.Free;

   DataStream.Free;

 end;

end;

procedure TClipboard.LoadClipDataFromStream(var AMem: TMemoryStream);

var

 Data: THandle;

 DataPtr: Pointer;

 MemStream, DataStream: TMemoryStream;

 Writer: TWriter;

 i, FormatListLen, DataStreamLen, tmpInt: Integer;

 tmpStrings: TStringList;

 tmpstr: string;

begin

 clipbrd.Clipboard.Open;

 Clipboard.Clear;

 DataStream := TMemoryStream.Create;

 tmpStrings := TStringList.Create;

 try

   AMem.Position := 0;

   AMem.ReadBuffer(FormatListLen, SizeOf(Integer));

   AMem.ReadBuffer(DataStreamLen, SizeOf(Integer));

   //AMem.ReadBuffer(DataStream, FormatListLen);

   DataStream.CopyFrom(AMem, FormatListLen);

   //if DataStream.Size > 0 then beep;

   //ShowMessage(IntToStr(DataStream.Size));

   //ShowMessage(IntToStr(AMem.position));

   DataStream.position := 0;

   tmpStrings.LoadFromStream(DataStream);

   DataStream.Clear;

   DataStream.CopyFrom(AMem, DataStreamLen);

   DataStream.position := 0;

   //AMem.ReadBuffer(DataStream.Memory^, DataStreamLen);

   if DataStream.Size > 0 then DataStream.Position := 0;

   for i := 0 to tmpStrings.Count - 1 do

   begin

     FormatListLen := StrToInt(tmpStrings.Names[i]);

     DataStreamLen := StrToInt(tmpStrings.Values[tmpStrings.Names[i]]);

     MemStream := TMemoryStream.Create;

     try

       MemStream.CopyFrom(DataStream, DataStreamLen);

       MemStream.Position := 0;

       //SetClipboardData(FormatListLen, )

       SetBuffer(FormatListLen, MemStream.Memory^, DataStreamLen);

     finally

       MemStream.Free;

     end;

   end;

 finally

   clipbrd.Clipboard.Close;

   tmpStrings.Free;

   DataStream.Free;

 end;

end;

来自:crazymoon, 时间:2003-11-10 18:12:00, ID:2284881

 SetBuffer(FormatListLen, MemStream.Memory^, DataStreamLen);

     finally

       MemStream.Free;

     end;

   end;

 finally

   clipbrd.Clipboard.Close;

   tmpStrings.Free;

   DataStream.Free;

 end;

end;

调试有问题?

来自:陈健松, 时间:2003-11-11 8:06:00, ID:2285509

没有问题的,你可以写入系统的clipbrd.pas单元中或继承自TClipboard进行扩展就行了。