Mega Code Archive

 
Categories / Delphi / Strings
 

Shorten a string so that it fits into a certain width

Title: Shorten a string so that it fits into a certain width Question: Long filenames often don't fit into a cell or other fixed-width space. We want to use ellipsis to show the part that has been removed Answer: SysUtils has a function that can do this: function MinimizeName( const Filename: TFileName; Canvas: TCanvas; MaxLen: Integer): TFileName; Alternatively, there is the WinAPI call: DrawTextEx( aCanvas.Handle, MyString, -1, tempRect, DT_CALCRECT + DT_MODIFYSTRING + DT_PATH_ELLIPSIS + DT_SINGLELINE ); which changes the path contained in MyString to fit. It leaves the filename, while shortening the path. If you use DT_END_ELLIPSIS instead of DT_PATH_ELLIPSIS (i.e. for a string that is not a path), it will put the ellipsis at the end.