• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

CopyToClipboard

If I have var S: String;
How to copy string in S to Clipboard ?

Thanks
0
cobramania
Asked:
cobramania
  • 3
1 Solution
 
kretzschmarCommented:
there is a clipbrd-unit,
just include into the uses clause
0
 
OryxConLaraCommented:
WELLLLL...

It's not exactly that straighforward. It's not hard, but working with the clipboard takes some getting used tol
Consider the following code, Copy(s) which writes to the Clipboard, and Paste(var s) which reads from the Clipboard into the variable and returns the number of characters read. "Clipboad" is a method of unit Clipbrd.

uses
    Clipbrd;

procedure Copy(s :string);
var
    pch :Pchar;
begin
    getmem(pch, length(s)+1);

    try
        StrPCopy(pch,s);
        (pch+length(s))^ := #0;
        Clipboard.SetTextBuf(pch)
     finally
        freemem(pch)
    end
end;

function Paste(var s :string) :integer;
// returns the number of chars read from the Clipboard
var
    syze, numread   :longint;
    pch     :Pchar;
const
    BUFFSYZE    =32767;

begin with Clipboard do
begin
    Result := 0;
    syze := 0;
    if   HasFormat(CF_TEXT)
       then
    begin
        repeat
            inc(syze, BUFFSYZE);
            getmem(pch, syze);
                numread := GetTextBuf(pch, syze);
            if numread = syze-1 then
                freemem(pch)
        until numread<syze-1;
        s := pch;
        Result := numread;
        freemem(pch)
    end
    else
    begin
        s := '(Clipboard does not contain TEXT)')
    end
end end;

The catch is that the Clipboard may contain more than just text. Do a FIND DEF on CF_TEXT, to see the list in the Windows unit, then look at the definitions in the Win32 help. You can use data in these formats, or even make up your own. If you want to know about that, ask :)

0
 
kretzschmarCommented:
or in short,

just from the delphi help
get text from clipboard

procedure TForm1.Button1Click(Sender: TObject);
begin
if Clipboard.HasFormat(CF_TEXT) then
  Edit1.Text := Clipboard.AsText
else
  MessageDlg('There is no text on the Clipboard', mtInformation,
        [mbOK],0);

end;

or
set text to clipboard

 Clipboard.AsText :=   Edit1.Text;

more about asText

Represents the content of the clipboard as a string.

property AsText: string;

Description

Use the AsText property to place text in and retrieve text from the clipboard. The clipboard must contain a string or an exception occurs. To determine if the clipboard contains a string, pass CF_TEXT to the HasFormat method.

meikl ;-)
0
 
cobramaniaAuthor Commented:
Thanks, it works fine, I just need the copy function :)

For kretzschmar, what I need is copy string from a variable to clipboard, not from TEdit. Thanks anyway :)
0
 
kretzschmarCommented:
the edit was only a placeholder for any stringvar
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now