Search code examples
delphidelphi-7

Show last line in a TLabel


I have a TLabel with fixed height and word wrap. The problem is that when the caption text exceeds the label's height, I can't see the last lines of text. I search entire internet for label components that can scroll down and show the last lines of text that exceeds the height of caption.

As you can see in this picture, line 7 is half visible and line 8 is not even shown:

enter image description here

I want line 1 to disappear or go up and line 8 be fully visible.


Solution

  • You can override TLabel's DoDrawText virtual method. something like this (example using interposer class):

    TLabel = class(StdCtrls.TLabel)
    protected
      procedure DoDrawText(var Rect: TRect; Flags: Longint); override;
    end;
    
    ... 
    
    procedure TLabel.DoDrawText(var Rect: TRect; Flags: Longint);
    var
      R: TRect;      
      TextHeight: Integer;
    begin
      if (Flags and DT_CALCRECT = 0) then
      begin
        R := ClientRect;
        Canvas.Font := Font;
        DrawText(Canvas.Handle, PChar(Text), -1, R, DT_EXPANDTABS or DT_CALCRECT or DT_WORDBREAK);        
        TextHeight := R.Bottom - R.Top;    
        if TextHeight > ClientHeight then
          Rect.Top := Rect.Top - (TextHeight - ClientHeight);
      end;
      inherited DoDrawText(Rect, Flags);
    end;
    

    enter image description here