Solved

CopyToClipboard

Posted on 2002-03-04
5
214 Views
Last Modified: 2010-05-18
If I have var S: String;
How to copy string in S to Clipboard ?

Thanks
0
Comment
Question by:cobramania
  • 3
5 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
there is a clipbrd-unit,
just include into the uses clause
0
 
LVL 1

Accepted Solution

by:
OryxConLara earned 25 total points
Comment Utility
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
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
 
LVL 1

Author Comment

by:cobramania
Comment Utility
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
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
the edit was only a placeholder for any stringvar
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now