Autor Beitrag
Jakob Schöttl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Sa 11.11.06 10:53 
Hallo,
ich will eine Funktion TextWidth haben, mit den Parametern Text: string und Font: TFont, die mir die Breite des Texts in Pixel zurückgibt. Ich hab schon herausgefunden, dass TCanvas diese Methode besitzt, aber ich will meine Funktion ohne TCanvas aufrufen können zB so:
ausblenden Delphi-Quelltext
1:
Edit1.Width := TextWidth(Edit1.Text,Edit1.Font) + 4;					


Wenn ich aber in meiner eigenen Funktion TCanvas erstelle (um die gleichnamige Methode zu benutzen), dann kommt eine Exception 'Leinwand/Zeichenfläche erlaubt kein Zeichnen.'
ist mir klar, ich brauche ein Objekt, dass die zeichenfläche "besitzt".

Dann wollte ich einen zusätzlichen Parameter meiner Funktion hinzufügen: "Dummy" vom T?

Also hier meine Frage: Ab welcher Klasse in der Hirachie ist TCanvas implementiert? Nicht TObject, TControl, TWinControl... Ich finds einfach nicht!

Hat jemand einen besseren Vorschlag oder eine Antwort parat?
Danke schon mal...
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Sa 11.11.06 10:56 
TGraphicControl dem Brunder von TWinControl

_________________
Markus Kinzler.
Jakob Schöttl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Sa 11.11.06 10:59 
Danke,
Aber wenn ich mir in der Delphi Hilfe die Hirachie von TEdit anschaue, dann steht da TGraphicControl nicht drin?
Und was heißt Brunder oder Bruder?

Und wenn ich in TGraphicControl Canvas aufrufe, dann kommt 'Undefinierter Bezeichner'
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Sa 11.11.06 11:17 
Du kannst dir ein TControlCanvas erstellen. Macht TForm so.

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Jakob Schöttl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Sa 11.11.06 11:27 
Bei dieser Funktion
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function TextWidth(const Text: string; const Font: TFont): integer;  //funktioniert noch nicht: Canvas kann nicth zeichnen..
var Canvas: TControlCanvas;
begin
  Canvas := TControlCanvas.Create;
  try  
    Canvas.Font := Font;
    Result := Canvas.TextWidth(Text);
  finally
    Canvas.Free;
  end;
end;

kommt aber die gleiche exception wie bei TCanvas.

Exception: Leinwand/Bild erlaubt kein Zeichnen.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 11.11.06 11:33 
ja, naja, das Canvas hat ja gar keine Ausgabemöglichkeit, da es keinem visuellen Elememnt zugeordnet ist! Und daher kann man auch keine Schriftart zuordnen...
Jakob Schöttl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Sa 11.11.06 12:08 
Ja, das hab ich mir schon gedacht, aber was kann ich denn nun dagegen machen?
ToZie
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Sa 11.11.06 12:20 
Als von Natur aus fauler Programmierer würd' ich ja erstmal meinen Text an ein unsichtbares TLabel zuweisen, gucken wie breit das wird und mit spannenderen Stellen meines Programms weitermachen :)
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Sa 11.11.06 12:21 
Canvas.Control:= self; setzen ;)
(gleich nach dem Create)

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Jakob Schöttl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Sa 11.11.06 13:13 
Gut, Self ist dann in meinem Fall Dummy, weil meine Funktion TextWidth keine Methode ist. Aber so gehts, Vielen Dank allen!
UGrohne
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Veteran
Beiträge: 5502
Erhaltene Danke: 220

Windows 8 , Server 2012
D7 Pro, VS.NET 2012 (C#)
BeitragVerfasst: Sa 11.11.06 13:15 
Vielleicht hilft Dir diese Funktion weiter, ich verwende sie, wenn ich mich noch recht entsinne, für ein DBGrid:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function TrueFontWidth(fnt: TFont; const text:string): Integer;
var
   dc: hdc;    //uses Windows
   tsize : Windows.TSize;
begin
   dc := GetDC(0);
   SelectObject(DC, fnt.Handle);
   GetTextExtentPoint32(dc, PChar(text), Length(text), tsize);
   ReleaseDC(0, DC);
   Result := tsize.cx;
end;
Jakob Schöttl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 929
Erhaltene Danke: 1


Delphi 7 Professional
BeitragVerfasst: Sa 11.11.06 13:59 
Juhu, Danke Uwe...
Das ist eigentlich genau das was ich gesucht hab.
Ich war grad nahe am verzweifeln, weil es die ganze zeit Zugriffsverletzungen gab, weil die Canvas doch noch kein übergeordnetes Element hatte und so weiter...