![]() |
TForm: Höhe dynamisch setzen anhand eines TLabel Textes
Hallo,
bei dem nachfolgenden Code passt der const txt nicht so in den Label das er ganz angezeigt wird.
Delphi-Quellcode:
Gibt es eine Möglichkeit zu berechnen wie hoch die Form sein müsste damit der Text ganz angezeigt wird ?
procedure TForm1.Button1Click(Sender: TObject);
const txt = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata'; var p : TPanel; l : TLabel; begin p := TPanel.Create(Self); p.Parent := Self; p.Top := 0; p.Left := 0; p.Width := 200; p.Height := 50; p.Anchors := [akLeft, akTop, akRight, akBottom]; p.BevelOuter := bvRaised; l := TLabel.Create(Self); l.Parent := p; l.AutoSize := False; l.WordWrap := True; l.Align := alClient; l.Caption := txt; end; |
AW: TForm: Höhe dynamisch setzen anhand eines TLabel Textes
Zitat:
|
AW: TForm: Höhe dynamisch setzen anhand eines TLabel Textes
Genauer: DT_CALCRECT ist genau dafür da.
|
AW: TForm: Höhe dynamisch setzen anhand eines TLabel Textes
Passt :thumb:
Delphi-Quellcode:
procedure SetLabelCaptionAndHeight(aLabel: TLabel; s: String);
var aRect: TRect; bmp: TBitmap; begin with aLabel do begin aRect := Rect(0, 0, Width, 0); bmp := TBitmap.Create; try bmp.Canvas.Font.Assign(Font); Caption := s; Height := DrawText(bmp.Canvas.Handle, PChar(s), Length(s), aRect, DT_CALCRECT or DT_WORDBREAK); finally bmp.Free; end; end; end; procedure TForm1.Button1Click(Sender: TObject); const txt = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata'; var p: TPanel; l: TLabel; h: Integer; begin ReportMemoryLeaksOnShutdown := True; p := TPanel.Create(Self); p.Parent := Self; p.Top := 0; p.Left := 0; p.Width := 200; p.Height := 50; p.Anchors := [akLeft, akTop, akRight, akBottom]; p.BevelOuter := bvRaised; l := TLabel.Create(Self); l.Parent := p; l.AutoSize := False; l.WordWrap := True; l.Align := alClient; SetLabelCaptionAndHeight(l, txt); Self.Caption := IntToStr(l.Height); Self.Height := Self.Height + l.Height; end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:08 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz