?
Solved

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

Posted on 2006-11-27
8
Medium Priority
?
651 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 1000 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

750 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