Solved

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

Posted on 2006-11-27
8
555 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:
ciuly earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 28

Expert Comment

by:TName
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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:ciuly
Comment Utility
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
Comment Utility
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
Comment Utility
Ciuly and TName thanks for your help.
250p each.

Greetings,

Peter Kiers
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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