Getting the MaxLen parameter for use with MinimizeName

I am trying to put a very long file name in TLabel using the MinimizeName function from the Vcl.FileCtrl module, but I can’t figure out how to get the MaxLen parameter used by the function. If I hardcode the value, I can see the actual result. But since the shape can be resized, I would like it to be dynamic = resize event change.

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

There must be some method of calculating this number of pixels

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

+4
source share
2 answers

To control label automatically shorten the path, you can set the AutoSize property to False and EllipsisPosition to epPathEllipsis if you are using the latest version of Delphi.

+3
source

To get rid of the dependencies of resizing the form, resizing can also occur if you use, for example, splitters, you can override the CanResize event to adapt the header.

as an 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. 
+3
source

Source: https://habr.com/ru/post/1479342/


All Articles