Inno Setup shorten file path string to fit Filenam

2020-03-06 03:52发布

问题:

On the WizardForm.InstallingPage, the WizardForm.FilenameLabel automatically truncates the string containing the file path, to include the drive letter at the beginning, followed by the maximum displayable path, truncating from the top level of the path, to fit the size of the label, so that the file name is always shown e.g. C:\...\LongFilePathNameWithMultipleSubDirectories\Filename.ext.

I have looked at the Length, Pos, Copy and ExtractFileDrive String Functions, but given that there are an almost infinite number path and file name possibilities, as the user can choose any installation path they wish, I am struggling to work out a way to show the maximum possible length of the path, without truncating the file name.

I think I need a function like Pos that returns the number of matches and the index for every \ it finds, which would mean I could use Length between each index to determine where to Copy from, but I haven't been able to find any way of doing this.

回答1:

The MinimizePathName() function is available to scripting, it takes a string & a font and returns a reformatted string that will fit in a given number of pixels in the given font.



回答2:

You can use MinimizePathName function.

Something like this:

YourLabelControl.Caption :=
   MinimizePathName(
     'C:\First Folder\Second Folder\Third Folder\Filename.ext',
     YourLabelControl.Font, YourLabelControl.Width);

(untested)