AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein TForm: Höhe dynamisch setzen anhand eines TLabel Textes
Thema durchsuchen
Ansicht
Themen-Optionen

TForm: Höhe dynamisch setzen anhand eines TLabel Textes

Ein Thema von user0815 · begonnen am 24. Apr 2015 · letzter Beitrag vom 24. Apr 2015
Antwort Antwort
Benutzerbild von user0815
user0815

Registriert seit: 5. Okt 2007
331 Beiträge
 
Delphi XE2 Professional
 
#1

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

  Alt 24. Apr 2015, 14:18
Passt

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;
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:15 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-2025 by Thomas Breitkreuz