Solved

Canvas in function

Posted on 2001-08-08
7
270 Views
Last Modified: 2010-04-06
Hello everybody!
Can somebody tell me why I can't call Canvas in a function or procedure?:

function Something(x,y:integer):boolean;
begin
Canvas.Moveto(x,y);
end;
0
Comment
Question by:dluedi
7 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 6363647
its just not a object method

guessing you mean the canvas of your form
two alternatives
1. keep a none object method

function Something(x,y:integer):boolean;
begin
  Form1.Canvas.Moveto(x,y);
end;

2. make a object-method

function TForm1.Something(x,y:integer):boolean;
begin
  Canvas.Moveto(x,y);
end;

(you must include this into the type-declaration of tform:
function Something(x,y:integer):boolean;
meybe in the private section)

meikl ;-)
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 6363714
To add to kretzschmar's answer, a third alternative is to pass the Canvas in as a parameter (I do this often).

function Something(Canvas :TCanvas; x,y:integer):boolean;
begin
Canvas.Moveto(x,y);
end;

and you would call it like this:

Something(Form1.Canvas, x, y);
0
 
LVL 21

Expert Comment

by:ziolko
ID: 6363730
Too late again :-((
ziolko.
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 5

Expert Comment

by:scrapdog
ID: 6363736
22 minutes too late =)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6363770
yes, scrap, you're right a third alternative
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6363968
>> Can somebody tell me why I can't call Canvas in a function or procedure?

To really understand the problem you should ask the other way round: Why can you use Canvas in a method? There is no function or variable that is called Canvas! So why can you use it in a method? The reason is simply: A method looks like this:

procedure TSomeObject.SomeMethod([Self: TObject;] parameters: ...);
begin
  [with Self do begin]
    Canvas.MoveTo(...);
  [end;]
end;

The expressions in those [] brackets are not visible in Delphi, it is what Delphi does internally for each and every method. So you see, when using Canvas in a method you in reality are using "TSomeObject.Canvas". That is why you can use Canvas in a method - but only, if the object (to which the method belongs), has a Canvas property. If not, you can't use Canvas in such a method, either.

Canvas in Delphi is always a property of an object. So you can't use it alone. Now read what meikl and scrapdog have written to learn how to solve the problem.

Regards, Madshi.
0
 
LVL 6

Expert Comment

by:edey
ID: 6365077
Oooooh - I have a fourth, though pretty stinky, alternative :).  You can create a global instance of a canvas that's attached to the same control, like so:

var
  Form1: TForm1;
  canvas : TControlCanvas;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
     unit1.canvas := TControlCanvas.create;
     unit1.canvas.Control := form1;
end;

procedure line(x,y : integer);
begin
     canvas.lineTo(x,y);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     line(random(clientWidth),random(clientHeight));
end;

Not that I'd ever recommend such a solution.

GL
Mike
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 111
tidtcpserver connection lost handle 2 110
How to convert memory stream to PDF file 6 180
Sending files from  idTcpServer Socket to idTcpClient 2 52
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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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