4

I am trying to put a very long filename on a TLabel using the MinimizeName function from Vcl.FileCtrl unit but I can't figure out how to get the MaxLen parameter used by the function If I hardcode a value I can see a valid result. But since the form can be resized I would like it to be dynamic = changing on resize event.

Some of the things I have tried is lblLicenseFile.Width // string is too long lblLicenseFile.Width - 10 //string is too long Trunc(lblLicenseFile.Width / lblLicenseFile.Font.Size) // string is very short

There must be some method of calculating this number of pixels

MinimizeName(const Filename: TFileName; Canvas: TCanvas; MaxLen: Integer): TFileName; MaxLen is the lenght, in pixels, available for drawing the file name on the canvas.

OZ8HP
  • 1,443
  • 4
  • 31
  • 61
  • I don't get this question. Why don't you just call the `MinimizeName` from the `OnResize` event of your form ? – TLama May 06 '13 at 10:52
  • If I don't add the correct value for MaxLen the text is not looking OK - either to long for the label or to short. – OZ8HP May 06 '13 at 11:00
  • The labels autosize is set to false and my current code is like following - not working. The string is too long for the label Filename := MinimizeName(Settings.License.LicenseFile, lblLicenseFile.Canvas, lblLicenseFile.Width); lblLicenseFile.Caption := Filename; – OZ8HP May 06 '13 at 11:19

2 Answers2

3

To get rid of dependencies of form resizing, resize could also happen if you using e.g. splitters, you can override the CanResize Event to adapt your caption.

as example:

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TLabel = Class(StdCtrls.TLabel)
  private
    FFullCaption: String;
    procedure SetFullname(const Value: String);
  published
    function CanResize(var NewWidth, NewHeight: Integer): Boolean; override;
    property FullCaption: String read FFullCaption Write SetFullname;
  End;

  TForm3 = class(TForm)
    FileNameLabel: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

uses FileCtrl;
{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
begin
  FileNameLabel.FullCaption := 'C:\ADirectory\ASubDirectory\ASubSubDirectory\AFileN.ame'
end;

{ TLabel }

function TLabel.CanResize(var NewWidth, NewHeight: Integer): Boolean;
begin
  inherited;
  if Assigned(Parent) then
    Caption := MinimizeName(FFullCaption, Canvas, NewWidth)
end;

procedure TLabel.SetFullname(const Value: String);
begin
  FFullCaption := Value;
  Caption := MinimizeName(FFullCaption, Canvas, Width)
end;

end.
bummi
  • 27,123
  • 14
  • 62
  • 101
3

To let the label control automatically shorten path, you can set the AutoSize property to False and the EllipsisPosition property to epPathEllipsis if you're using a recent version of Delphi.

TLama
  • 75,147
  • 17
  • 214
  • 392
  • That I had not been thinking of - but it does the trick and I get rid of the Vcl.FileCtrl unit - thanks – OZ8HP May 06 '13 at 11:32