Solved

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

Posted on 2006-11-27
8
594 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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