Link to home
Start Free TrialLog in
Avatar of rclay
rclayFlag for United States of America

asked on

Shortened pathname with \...\

How do I shorten pathnames to fit a certain length space using an ellipsis (...) between backslashes?
Avatar of viktornet
viktornet
Flag of United States of America image

How about something similar to this.,.....

var
  Path : string;
procedure TForm1.Button1Click(Sender: TObject);
begin
  if Length(path) > 10 then
    Path := Copy(path, 1, 10) + '\...\';
  Caption := Path;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
  Path := 'C:\windows\desktop\Viktor\Delphi';
  Caption := Path;
end;

Hope it helps! There are other ways to do that with the API, but it's not exactly what you are looking for     it does like this  C:\windows\desktop\vik...  and not C:\windows\desktop\vik\...\...\and so on.....

Regards,
Viktor Ivanov
Avatar of BlackMan
BlackMan

If you want to do it the "Windows Way", let me know and I'll show you how to do it..
Avatar of rclay

ASKER

Viktornet: What I'm looking for is a function or procedure that shortens the path by omitting its middle part, substituting an ellipsis. But it would break the pathname at a '\', not at some arbitrary point such as at the 10th character. Also the total length of the resulting pathname should be adjustable at design- or run-time by the programmer.

Blackman: Yes, I would like to know how to do it the windows way.
Hi

The DrawTextEx API function will fill in the ellipsis for you.  You give the function a string (PChar), a rectangle and a canvas, and if the text doesn't fit into the rectangle then it uses ellipsis.

E.g.
DrawTextEx(Canvas.Handle, PChar(SomeStr), Length(SomeStr), SomeRect, DT_PATH_ELLIPSIS or DT_MODIFYSTRING, nil);

Unfortunately you don't get the result string (with ellipsis) back.

JB
ASKER CERTIFIED SOLUTION
Avatar of BlackMan
BlackMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rclay

ASKER

Thanks for the input Viktornet, JimBob, and BlackMan!