Solved

Canvas in function

Posted on 2001-08-08
7
254 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
Comment Utility
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
Comment Utility
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
Comment Utility
Too late again :-((
ziolko.
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 5

Expert Comment

by:scrapdog
Comment Utility
22 minutes too late =)
0
 
LVL 27

Expert Comment

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

Expert Comment

by:Madshi
Comment Utility
>> 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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now