Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Canvas in function

Posted on 2001-08-08
7
Medium Priority
?
278 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
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

609 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