?
Solved

Canvas in function

Posted on 2001-08-08
7
Medium Priority
?
276 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
[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
7 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 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
Technology Partners: 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

743 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