?
Solved

CopyToClipboard

Posted on 2002-03-04
5
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 27

Expert Comment

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

Accepted Solution

by:
OryxConLara earned 100 total points
ID: 6839347
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
ID: 6839537
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
ID: 6840829
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
ID: 6840838
the edit was only a placeholder for any stringvar
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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