Solved

How to Copy & Paste text to/from a canvas.

Posted on 2006-11-27
8
568 Views
Last Modified: 2010-04-05
Dear experts,

How can I Copy & Paste text to/from a canvas.

I have made a little example and put it on my site.

url: http://members.home.nl/peterkiers/
(beneath the Under Construction bar, you see a floppy-disc,here
you can download the example).

When you choose menu-item Test ->>Fillbuf. 256 characters
will be displayed on the canvas. When you choose Select all
all the charachters will be selected (the font will be reversed).
now, how can I copy the text to clipboard and from clipboard
to canvas.

Can someone help me with this problem  ?

Thanks in advanced,

Peter Kiers

0
Comment
Question by:peterkiers
  • 3
  • 3
  • 2
8 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 250 total points
ID: 18020562
it depends a lot on your applicaiotn. I have written the following rutines:

procedure TForm1.Copy1Click(Sender: TObject);
var
  i, j: integer ;
  s:string;
begin
  s:='';
  for i := 1 to SCRROWS do
  begin
    for j := 1 to SCRCOLS do
      if (ScrBuf[i,j].Hilite = Char(chReverse)) and
      (ScrBuf[i,j].Data in ['A'..'Z','a'..'z','0'..'9']) then// change this so it gets all printable characters
        s:=s+ScrBuf[i,j].Data;
    if s<>'' then s:=s+#13#10;
  end;
  with Clipboard do
  begin
    open;
    astext:=s;
    close;
  end;
end;

procedure TForm1.Paste1Click(Sender: TObject);
var s:string;
begin
  with Clipboard do
  begin
    open;
    s:=astext;
    close;
  end;
  if
    you have selected text, then replace it with the contents of s
  else
    insert s at the current cursor position
end;


if you copy teh selected text (that is only alphanumerical characters) you can paste it in an editor. so copy to clipboard works
pasting is a little difficult since I don't know how your application works. try to follow my instructions from there and you should be able to do it. it's not that much of a foilosofy, it's just the need of inserting the string in the buffer at the right position (with or without overriding any current selection)
0
 
LVL 28

Assisted Solution

by:TName
TName earned 250 total points
ID: 18020868
Hi, to continue ciuly's example, you could paste the contents like this:
(as you can see, all non-printable characters are gone)

procedure TForm1.Paste1Click(Sender: TObject);
var
  i, j, k: integer ;
  ch: char ;
  s:string;
begin
  with Clipboard do
  begin
    open;
    s:=astext;
    close;
  end;

  ClearBuf;
  k := 0 ;
  for i := 1 to SCRROWS do
    for j := 1 to SCRCOLS do begin
        if Length(s)>=k then begin
          ch := s[k] ;
          ScrBuf[i,j].data := ch ;
          k := k + 1 ;
          if k > 255 then
            k := 0 ;
        end;
    end ;
  ShowBuf;
end ;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 18020901
Oke, about the Paste-part, I have aded a TImage-mousedown-procedure:

procedure TForm1.ScrImage1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  c,r: integer ;
    begin
      c := (x+FontWidthPix) div FontWidthPix ;
      r := (y+FontHeightPix) div FontHeightPix ;
      if (c <= SCRCOLS) and (r <= SCRROWS) then
        begin
          { redraw cell that cursor inhabited }
          DispCell(CsrCol,CsrRow,true) ;
          { determine new cursor position and draw }
          CsrCol := c ;
          CsrRow := r ;
          DrawCursor(CsrCol,CsrRow) ;
        end ;
    end ;

I can now point an exact cell with my arrow, how can I paste text that is in the clipboard on the canvas
at the location of the cursor?

Peter
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 28

Expert Comment

by:TName
ID: 18020927
BTW, delete this 2 lines if you only want to paste what's on the clipboard once  (as it should be):
if k > 255 then  
            k := 0 ;

So it's:

ClearBuf;
  k := 0 ;
  for i := 1 to SCRROWS do
    for j := 1 to SCRCOLS do begin
        if Length(s)>=k then begin
          ch := s[k] ;
          ScrBuf[i,j].data := ch ;
          k := k + 1 ;    
        end;
    end ;
  ShowBuf;

And just one remark:
You can't copy or paste anything from the canvas. All data is stored in ScrBuf, and all operations have to be done with (the data in) this array. Then ShowBuf paints that data on the canvas.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 18021087
I am very pleased about the Copy and Paste examples.
I have only 1 commend: How can I paste the text at cursorlocation?

greetings,

Peter
0
 
LVL 28

Expert Comment

by:2266180
ID: 18021190
you start the counting from the cursor location. something like this.

i:=cursor_x;
k:=cursor_y;
j:=1;
while j<=length(s) do
begin
  scrbuf[k, i].data:=s[j]
  inc(i);
  if i=scrcols+1 then
  begin
    i:=1;
    inc(k);
  end;
  inc(j);
end;

you can also implement code to interpret the carriage return (#13 #10) when pasting.
(not tested, written directly on page)
0
 
LVL 28

Expert Comment

by:TName
ID: 18021393
Hi, ciuly, your version works just fine (just adapted the variable names), and it's nicer than mine ;)
(mine's below)

i:=CsrCol;
k:=CsrRow;
j:=1;
while j<=length(s) do
begin
  scrbuf[k, i].data:=s[j];
  inc(i);
  if i=scrcols+1 then
  begin
    i:=1;
    inc(k);
  end;
  inc(j);
end;


my version:

procedure TForm1.Paste1Click(Sender: TObject);
var
  i, j, k: integer ;
  ch: char ;
  s:string;
begin
  with Clipboard do  //ciuly's clipboard code
  begin
    open;
    s:=astext;
    close;
  end;
  k := 1 ;
  for i := 1 to SCRROWS do
    for j := 1 to SCRCOLS do begin
       if ((j>CsrCol) and (i>=CsrRow)) or (i>CsrRow) then begin
          if Length(s)>=k then begin
          ch := s[k] ;
          ScrBuf[i,j].data := ch ;
          k := k + 1 ;
          end;
       end;
    end ;
  ShowBuf;
end ;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 18021656
Ciuly and TName thanks for your help.
250p each.

Greetings,

Peter Kiers
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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