Einzelnen Beitrag anzeigen

Benutzerbild von user0815
user0815

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

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

  Alt 24. Apr 2015, 15: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