• Delphi Clipboard[2] SetTextBuf、GetTextBuf、AsText 文本操作


    {该文首发于博客园 滔Roy,无须授权即可转发,请自觉保留头部申明}

    Delphi Clipboard[2] SetTextBuf、GetTextBuf、AsText -文本操作

    Clipboard是全局类函数 

    1、AsText 属性(允许从剪贴板放置和检索文本)

    property AsText: string read GetAsText write SetAsText;
    
    function TClipboard.GetAsText: string;
    var
      Data: THandle;
    begin
      Open;
      Data := GetClipboardData(CF_TEXT);
      try
        if Data <> 0 then
          Result := PChar(GlobalLock(Data))
        else
          Result := '';
      finally
        if Data <> 0 then GlobalUnlock(Data);
        Close;
      end;
    end;
    
    procedure TClipboard.SetAsText(const Value: string);
    begin
      SetBuffer(CF_TEXT, PChar(Value)^, Length(Value) + 1);
    end;
    

    使用示例:

    Clipboard.AsText:='滔Roy ' + DateTimeToStr(Now);    //传值到剪贴板
    ShowMessage(Clipboard.AsText);                     //输出剪贴板的内容
    

    2、SetTextBuf  (将句柄以给定格式放置在剪贴板上)

    procedure TClipboard.SetTextBuf(Buffer: PChar);
    begin
      SetBuffer(CF_TEXT, Buffer^, StrLen(Buffer) + 1);
    end;
    

    使用示例:

    Clipboard.SetTextBuf(PChar('滔Roy ' + DateTimeToStr(Now)) );    //传值到剪贴板
    ShowMessage(Clipboard.AsText);                      //输出剪贴板的内容

    3、GetTextBuf  //检索、取回缓冲区的文本数据  

    function TClipboard.GetTextBuf(Buffer: PChar; BufSize: Integer): Integer;
    var
      Data: THandle;
    begin
      Open;
      Data := GetClipboardData(CF_TEXT);
      if Data = 0 then Result := 0 else
      begin
        Result := StrLen(StrLCopy(Buffer, GlobalLock(Data), BufSize - 1));
        GlobalUnlock(Data);
      end;
      Close;
    end;
    

    示例:

    var
      PArr:array[0..1024] of Char;
    begin
      Clipboard.AsText:='滔Roy ' + DateTimeToStr(Now);    //传值到剪贴板
      Clipboard.GetTextBuf(PArr,Length(PArr));          //获取缓冲区的内容
      ShowMessage(PArr);                //输出内容
    end;
    

      

      

    创建时间:2022.04.14  更新时间:

  • 相关阅读:
    348. Design Tic-Tac-Toe
    347. Top K Frequent Elements
    346. Moving Average from Data Stream
    345. Reverse Vowels of a String
    343. Integer Break
    342. Power of Four
    341. Flatten Nested List Iterator
    340. Longest Substring with At Most K Distinct Characters
    339. Nested List Weight Sum
    Python(九) Python的高级语法与用法
  • 原文地址:https://www.cnblogs.com/guorongtao/p/16143798.html
Copyright © 2020-2023  润新知