剪贴板的流存储

(*//
标题:剪贴板的流存储
说明:如果你想借用剪贴板,有这套函数就方便了
设计:Zswang
日期:2003-11-14
支持:wjhu111@21cn.com
//*)

///Begin Source
uses Clipbrd;

type
  TDataIdnet = array[0..2] of Char;
  TClipboardFileHead = packed record
    rIdent: TDataIdnet;
    rCount: Word;
  end;

  TClipboardFileItem = packed record
    rFormat: Word;
    rSize: Longword;
    rData: Pointer;
  end;

const rDataIdnet: TDataIdnet = 'cbf';

function ClipboardSaveToStream(mStream: TStream): Boolean;
var
  vClipboardFileHead: TClipboardFileHead;
  vClipboardFileItem: TClipboardFileItem;
  I: Integer;
  vData: THandle;
begin
  Result := False;
  if not Assigned(mStream) then Exit;
  vClipboardFileHead.rIdent := rDataIdnet;
  vClipboardFileHead.rCount := Clipboard.FormatCount;
  mStream.Write(vClipboardFileHead, SizeOf(vClipboardFileHead));
  try
    Clipboard.Open;
    for I := 0 to Clipboard.FormatCount - 1 do begin
      vData := GetClipboardData(Clipboard.Formats[I]);
      vClipboardFileItem.rFormat := Clipboard.Formats[I];
      vClipboardFileItem.rSize := GlobalSize(vData);
      vClipboardFileItem.rData := GlobalLock(vData);
      try
        mStream.Write(vClipboardFileItem, SizeOf(vClipboardFileItem) -
          SizeOf(vClipboardFileItem.rData));
        mStream.Write(vClipboardFileItem.rData^, vClipboardFileItem.rSize);
      finally
        GlobalUnlock(vData);
      end;
    end;
  finally
    Clipboard.Close;
  end;
  Result := True;
end; { ClipboardSaveToStream }

function ClipboardLoadFromStream(mStream: TStream): Boolean;
var
  vClipboardFileHead: TClipboardFileHead;
  vClipboardFileItem: TClipboardFileItem;
  I: Integer;
  vData: THandle;
begin
  Result := False;
  if not Assigned(mStream) then Exit;
  if mStream.Size <= SizeOf(vClipboardFileHead) then Exit;
  mStream.Read(vClipboardFileHead, SizeOf(vClipboardFileHead));
  if vClipboardFileHead.rIdent <> rDataIdnet then Exit;
  Clipboard.Clear;
  Clipboard.Open;
  try
    for I := 0 to vClipboardFileHead.rCount - 1 do begin
      mStream.Read(vClipboardFileItem, SizeOf(vClipboardFileItem) -
        SizeOf(vClipboardFileItem.rData));
      vData := GlobalAlloc(GMEM_MOVEABLE + GMEM_DDESHARE,
        vClipboardFileItem.rSize);
      try
        vClipboardFileItem.rData := GlobalLock(vData);
        try
          mStream.Read(vClipboardFileItem.rData^, vClipboardFileItem.rSize);
          SetClipboardData(vClipboardFileItem.rFormat, vData);
        finally
          GlobalUnlock(vData);
        end;
      finally
        GlobalFree(vData);
      end;
    end;
  finally
    Clipboard.Close;
  end;
  Result := True;
end; { ClipboardLoadFromStream }
///End Source

///Begin Demo
procedure TFormClipboard.ButtonSaveClick(Sender: TObject);
var
  vMemoryStream: TMemoryStream;
begin
  if not SaveDialog1.Execute then Exit;
  if FileExists(SaveDialog1.FileName) and
    (MessageDlg(Format('是否覆盖文件 "%s"?', [SaveDialog1.FileName]),
    mtWarning, [mbYes, mbNo], 0) <> mrYes) then Exit;
  vMemoryStream := TMemoryStream.Create;
  try
    ClipboardSaveToStream(vMemoryStream);
    vMemoryStream.SaveToFile(SaveDialog1.FileName);
  finally
    vMemoryStream.Free;
  end;
end;

procedure TFormClipboard.ButtonLoadClick(Sender: TObject);
var
  vMemoryStream: TMemoryStream;
begin
  if not OpenDialog1.Execute then Exit;
  if not FileExists(OpenDialog1.FileName) then Exit;
  vMemoryStream := TMemoryStream.Create;
  try
    vMemoryStream.LoadFromFile(OpenDialog1.FileName);
    ClipboardLoadFromStream(vMemoryStream);
  finally
    vMemoryStream.Free;
  end;
end;
///End Demo
微信扫码订阅
UP更新不错过~
关注
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

zswangII

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值