Autor Beitrag
jjturbo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 516

Win2000 prof., WinXP prof.
D4 Stand., D5 Prof, D7 Prof, D2007 Prof.
BeitragVerfasst: Di 14.01.20 12:19 
Moin Forum,

ich möchte die Breite des Textes auf einem TButton ermitteln:
ausblenden 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?

_________________
Windows XP: Für die einen nur ein Betriebssystem - für die anderen der längste Virus der Welt...
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 14.01.20 12:56 
Laut TCanvas.TextWidth ist der Wert abhängig von TCanvas.Font:
Zitat:
Returns the width, in pixels, of a string rendered in the current 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 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 516

Win2000 prof., WinXP prof.
D4 Stand., D5 Prof, D7 Prof, D2007 Prof.
BeitragVerfasst: Di 14.01.20 13:15 
Jau, das war´s, danke! :)
Dachte dass da das Gleiche drin stünde.

_________________
Windows XP: Für die einen nur ein Betriebssystem - für die anderen der längste Virus der Welt...