Solved

Canvas in function

Posted on 2001-08-08
7
268 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 154
Dev express lookupcombo 3 34
RESTRequest Parameter 4 41
MS Access from Delphi 31 59
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 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…
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…

791 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