Autor Beitrag
soisseteben
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111

Win XP
D7
BeitragVerfasst: Mi 20.12.06 17:06 
Gibt es eine Möglichkeit die Breite eines (z.B. in einem Label) ausgegebenen String in Pixeln zu ermitteln. Ich errinere mich grau, das es hier mal einen Thread dazu gab, nur gibt die Suchfuktion leider nichts her.

Ziel ist es eine Label unabhängig vom Inhalt immer in der Horizontrale zentral auf dem Formular zu platzieren. Wenn ich es über die Breite des Labels versuche, klappt es nicht genau:

ausblenden Delphi-Quelltext
1:
lbl1.Left := (Form1.Width - lbl1.Width) div 2					


Danke schonmal.
Soisset


Moderiert von user profile iconChristian S.: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Mi 20.12.2006 um 17:19

_________________
„Wer nur nach dem Zweck der Dinge fragt, wird ihre Schönheit nie entdecken.“ - (Halldór Laxness)
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 20.12.06 17:09 
ausblenden Delphi-Quelltext
1:
2:
Canvas.Font := Label1.Font;
Breite := Canvas.TextWidth(Label1.Caption);

Das müsste gehen;

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
soisseteben Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 111

Win XP
D7
BeitragVerfasst: Mi 20.12.06 17:17 
mm, da bekomm ich jetzt das selbe ungenaue Ergebnis wie vorher.

EDIT: Also ich rechne dann wie oben mit "Breite" und das Label steht zu weit rechts..
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Mi 20.12.06 19:02 
nimm mal
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
{--------------------------------}
{ berechnet Textbreite und -höhe }
{--------------------------------}
function CalcTextExtent (const DC: HDC; const Text: String): TSize;
var
  R: TRect;
begin
  SetRectEmpty(R);
  DrawText(DC, PChar(Text), Length(Text), R, dt_CalcRect);
  Result.cx := R.Right-R.Left;
  Result.cy := R.Bottom-R.Top;
end;

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 20.12.06 19:25 
user profile iconsoisseteben hat folgendes geschrieben:
Wenn ich es über die Breite des Labels versuche, klappt es nicht genau:

ausblenden Delphi-Quelltext
1:
lbl1.Left := (Form1.Width - lbl1.Width) div 2					
Nimm mal Form1.ClientWidth anstatt Form1.Width, dann sollte das klappen.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mi 20.12.06 19:28 
Grad wollt ich es schreiben :lol:.

Aber da gabs noch was anderes. Irgendnen Wert, der wirklich nur die Breite ohne Rand angibt... Mh...

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)