Autor Beitrag
nn1234
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

WIN XP
Delphi 7
BeitragVerfasst: Di 20.12.05 22:13 
Ich versteh das nicht! Ich möchte mir eine Unit anlegen, die einige nützliche Prozeduren enthällt. Der Quelltext dazu lautet wie folgt:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
unit uNuetzlich;
interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     Dialogs;

procedure Line(x1,y1,x2,y2 : Integer);

implementation

procedure Line(x1,y1,x2,y1 : Integer);
begin
  Canvas.MoveTo(x1,y1);
  Canvas.LineTo(x2,y2);
end;

end.


Warum ist 'Canvas' in dieser Unit nicht bekannt. Wenn ich sie compilieren möchte kommt folgender Fehler:

[Fehler] uNuetzlich.pas(13): Undefinierter Bezeichner: 'Canvas'

Muss ich da was in der Uses Klausel ändern?
Ich hab keine Ahnung. Schon öfter hatte ich das Problem und bin bisher immer drum rum geschlängelt. Was mach ich hier flasch?

Vielen Dank für eure Hilfe!

nn
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 20.12.05 22:14 
Die uses-Klausel ist schon richtig, aber wo soll die Canvas denn her kommen? Du hast nirgendwo eine Variable Canvas deklariert. :-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Tilman
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1405
Erhaltene Danke: 51

Win 7, Android
Turbo Delphi, Eclipse
BeitragVerfasst: Di 20.12.05 22:33 
Probiers mal so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
unit uNuetzlich;
interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     Dialogs;

procedure Line(canvas: TCanvas; x1,y1,x2,y2 : Integer);

implementation

procedure Line(canvas: TCanvas; x1,y1,x2,y1 : Integer);
begin
  canvas.MoveTo(x1,y1);
  Canvas.LineTo(x2,y2);
end;

end.


Natürlich muss dann beim Aufruf die Canvas-Instanz übergeben werden. Z.B.:

Line(form1.canvas,50,50,100,100);

oder

Line(paintbox1.canvas,50,50,100,100);

_________________
Bringe einen Menschen zum grübeln, dann kannst du heimlich seinen Reis essen.
(Koreanisches Sprichwort)
nn1234 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

WIN XP
Delphi 7
BeitragVerfasst: Di 20.12.05 22:39 
Hab jetzt den Quelltext wie folgt geändert:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
unit uNuetzlich;
interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     Dialogs;

procedure Line(x1,y1,x2,y2 : Integer);

implementation

uses uAnfang;

procedure Line(x1,y1,x2,y2 : Integer);
begin
  fAnfang.Canvas.MoveTo(x1,y1);
  fAnfang.Canvas.LineTo(x2,y2);
end;

end.


Doch so kann ich die Prozedur nur auf diesem einen Formular nutzen. Gibt's da nicht auch ne Möglichkeit, sie zu nutzen, wann immer ich sie brauch - unabhängig vom Formular?
Danke trotzdem für den Tipp. So geht's vorläufig. Bin nur noch nicht ganz zufrieden...aber wann ist man das schon :lol:

nn

~~~EDIT: @Tilman: Hatte geschrieben, bevor ich dein Porting gelesen hatte. Vielen Dank, genau das hatte ich gesucht!


Zuletzt bearbeitet von nn1234 am Di 20.12.05 22:42, insgesamt 1-mal bearbeitet
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 20.12.05 22:41 
Tilman hat Dir die optimale Lösung präsentiert. Du übergibst dann einfach fAnfang.Canvas als ersten Parameter.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
nn1234 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

WIN XP
Delphi 7
BeitragVerfasst: Di 20.12.05 22:43 
Danke! Problem geklärt! Ziemlich schnell, wie ich finde... :D

nn