Shortened version of a filename

The length of whole path and file name could be too long to show it in title bar of application or some label. Solution? Show shortened version of a filename using dots instead of folders.

You don’t need to make up some complicated algorithm, just use MinimizeName function, which can be found in FileCtrl unit.

procedure TForm1.FormCreate(Sender: TObject);
var dir: string;
begin
  GetDir(0, dir);
  Label1.Caption := MinimizeName(dir, Label1.Canvas, Label1.Width);
end;

The first parameter of the function is the full file name. Second parameter is the drawing surface (canvas) on which the result should be drawn. And the last parameter is max. length in pixels, available for drawing the file name on the canvas (in our example, we use Width of Label).