Solved

CopyToClipboard

Posted on 2002-03-04
5
215 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
ID: 6839191
there is a clipbrd-unit,
just include into the uses clause
0
 
LVL 1

Accepted Solution

by:
OryxConLara earned 25 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 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

12 Experts available now in Live!

Get 1:1 Help Now