Entwickler-Ecke

Grafische Benutzeroberflächen (VCL & FireMonkey) - Firemonkey, TCanvas.TextWidth gibt falsche Werte


jjturbo - Di 14.01.20 12:19
Titel: Firemonkey, TCanvas.TextWidth gibt falsche Werte
Moin Forum,

ich möchte die Breite des Textes auf einem TButton ermitteln:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm7.Button3Click(Sender: TObject);
var x :Single;
    b :TButton;
begin
  b := Sender as TButton;
  x := b.Canvas.TextWidth(b.Text);
  Caption := b.Text + '  ' + FormatFloat('0.0',(x));
end;

Ausgeben in der Caption der Form.

Ich beschrifte den Button mit "aaa", TextSettings.Font.Size=10, ich bekomme eine Breite von 17,5. Wenn ich jetzt den Text auf 60er Größe setze, bekomme ich immer noch 17,5. Aber laut Hilfe sollte ich doch die Textbreite in Pixeln bekommen?


Th69 - Di 14.01.20 12:56

Laut TCanvas.TextWidth [http://docwiki.embarcadero.com/Libraries/Rio/en/FMX.Graphics.TCanvas.TextWidth] ist der Wert abhängig von TCanvas.Font [http://docwiki.embarcadero.com/Libraries/Rio/en/FMX.Graphics.TCanvas.Font]:
Zitat:
Returns the width, in pixels, of a string rendered in the current font [http://docwiki.embarcadero.com/Libraries/Rio/en/FMX.Graphics.TCanvas.Font].

Überprüfe also mal, ob in b.Canvas.Font derselbe Font wie in b.TextSettings.Font drinsteht - ansonsten mußt du die Eigenschaften entsprechend kopieren.


jjturbo - Di 14.01.20 13:15

Jau, das war´s, danke! :)
Dachte dass da das Gleiche drin stünde.