Solved

CopyToClipboard

Posted on 2002-03-04
5
216 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

808 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