Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   TForm: Höhe dynamisch setzen anhand eines TLabel Textes (https://www.delphipraxis.net/184843-tform-hoehe-dynamisch-setzen-anhand-eines-tlabel-textes.html)

user0815 24. Apr 2015 12:58

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:
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;
Gibt es eine Möglichkeit zu berechnen wie hoch die Form sein müsste damit der Text ganz angezeigt wird ?

BadenPower 24. Apr 2015 13:41

AW: TForm: Höhe dynamisch setzen anhand eines TLabel Textes
 
Zitat:

Zitat von user0815 (Beitrag 1299130)
Gibt es eine Möglichkeit zu berechnen wie hoch die Form sein müsste damit der Text ganz angezeigt wird ?

Du kannst die Text-Dimensionen mit der Windows.DrawText()-Funktion ermitteln.

DeddyH 24. Apr 2015 13:58

AW: TForm: Höhe dynamisch setzen anhand eines TLabel Textes
 
Genauer: DT_CALCRECT ist genau dafür da.

user0815 24. Apr 2015 14:18

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 15:50 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz