Recent

Author Topic: paste text from clipboard sample please  (Read 12298 times)

Martin V

  • Full Member
  • ***
  • Posts: 139
paste text from clipboard sample please
« on: May 15, 2010, 10:34:42 am »
Hello,

copying to clipboard works already fine, but I did not managed to simply paste text from clipboard into buffer / ansistring. I tried Clipboard.SupportedFormats and it was empty. I assume it is more simple that I mean. Please write some lines of sample code. I even compared my code with the synedit code, nothing helps.

I tried:
    Clipboard.Open;
    ClipboardStream := tMemorystream.Create;
    Clipboard.GetFormat (cf_text, ClipboardStream);
and then
    ClipboardStream.Write (Zero, 1);
    result := pointer (ClipboardStream.Memory);
alternate code
    result := Pointer (Clipboard.AsText);

The code should fit for all platforms. Thank you for any hint.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: paste text from clipboard sample please
« Reply #1 on: May 15, 2010, 12:02:57 pm »
This should be enough:

Code: [Select]
uses Clipbrd;

procedure TForm1.Button1Click(Sender: TObject);
begin
   Memo1.text:=Clipboard.AsText;
end;       

Martin V

  • Full Member
  • ***
  • Posts: 139
Re: paste text from clipboard sample please
« Reply #2 on: May 16, 2010, 09:05:29 pm »
Theo,

thanks for this simple hint. Clipboard.AsText alone works fine indeed. In
clipbrd.inc tClipboard.GetAsText
I could see how it works and the following code also works fine and I can change the format type of the clipboard.

Code: [Select]
    ClipboardStream := tMemorystream.Create;
    if Clipboard.GetFormat (PredefinedClipboardFormat(pcfText), ClipboardStream) then begin
      ClipboardStream.Write (Zero, 1); {adding terminating #0}
      result := pointer (ClipboardStream.Memory);
    end
    else begin
      result := nil;
    end;
    ClipboardStream.Free;


Clipboard.Open / Clipboard.Close let the functionality fail.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: paste text from clipboard sample please
« Reply #3 on: May 17, 2010, 07:33:38 am »
Many controls have methods called PasteFromClipBoard, CopyToClipBoard and CutToClipBoard.
They will do the job for you, so you do not have to mess with the underlying code, to change the format of the clipboard.

Bart


 

TinyPortal © 2005-2018