Link to home
Start Free TrialLog in
Avatar of PeterdeB
PeterdeBFlag for Netherlands

asked on

Fit this path into this edit please!

Hi my dear friends!

After numerous attempts to fit this path into my edit, I turn to you! This is my code....

//Code

var
  lpbi: TBrowseInfo;
  pidlStart: PItemIDList;
  Malloc: IMalloc;
  sSelected: string;
  pidlSelected: PItemIDList;
  edt: TAHMStoreEdit;
begin
  SHGetSpecialFolderLocation(Handle, $00, pidlStart);
  SHGetMalloc(Malloc);
  with lpbi do
  begin
    hwndOwner := Handle;
    pidlRoot := pidlStart;
    GetMem(pszDisplayName, MAX_PATH);
    lpszTitle := PChar('Selecteer folder');
    ulFlags := $00000001;
    lpfn := nil;
  end;
  pidlSelected := SHBrowseForFolder(lpbi);
  if pidlSelected <> nil then
  begin
    if SHGetPathFromIDList(pidlSelected, lpbi.pszDisplayName) then
    sSelected := StrPas(lpbi.pszDisplayName);
     if (Sender is TspeedButton) then
    begin
      Edt := FindComponent('edt' + Copy((Sender as TspeedButton).Name, 4, 255)) as
        TAHMSTOREEdit;
        Edt.Text := sSelected;

<<<<< something here I guess? >>>>>

    Malloc.Free(pidlSelected);
  end;
  FreeMem(lpbi.pszDisplayName);
  Malloc.Free(pidlStart);
end;

//Code

When the string is too large for my edit it should be truncated into something like:

 C:\...My Hidden Folder\Jenna Jameson...:) instead of
C:\Nothing Special Here\Just Normal Folders\Really Nothing Abnormal Here\Believe Me Please\My Hidden Folder\Jenna Jameson :)

Regards Paul

Ps working samples do the trick...my numerous attempts resulted in nearly eating my LCD screen :)
Avatar of PeterdeB
PeterdeB
Flag of Netherlands image

ASKER

Ok, lets try this again.  I just re-read your original request and realized I was only giving you the filename and not the last directory as well.  The following should work.

procedure Shorten(var fPath : string);
var
 ii : integer;
 count : integer;
 startPos,stop : integer;
 prevStop : integer;
begin
 count := 0;
 startPos := 0;
 stop := 0;
 prevStop := 0;
 for ii := 1 to length(fPath) do begin
   // look for all the separators
   if fPath[ii] = '\'  then begin
     count := count + 1;  // count them
     if count = 2 then startPos := ii + 1  // 2nd is start
     else begin
       prevStop := stop;
       stop := ii;
     end;
   end;
 end;
 if (count > 3) and (prevStop > (startPos + 2)) then begin
   // we had at least 4 separators and can shorten
   fPath[startPos] := '.';
   fPath[startPos+1] := '.';
   fPath[startPos+2] := '.';
   startPos := startPos + 3;  // elipses
   for ii := prevStop to length(fPath) do begin
     // copy the last separator and the filename
     fPath[startPos] := fPath[ii];
     startPos := startPos + 1;
   end;
   setLength(fPath,startPos - 1);  // shorten the string
 end;
end;

From jbshumate!

Just tried it and it works!

Regards Paul :)
I am officially confused
ASKER CERTIFIED SOLUTION
Avatar of House_of_Dexter
House_of_Dexter
Flag of United States of America image

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
Hi folks!

Rightly after I posted my question I went through EE's database and found a similar post to mine and a reply from jbshumate. I will now try House of Dexters reply!

Regards Paul :)
in that case, next time, it is better to post the link ;) (will better help future readers)
I think this will help future readers as well. They will find my post and wil find the answer as well.

Regards Paul
true, but it is better to include the link so that they can see the whole conversation. it sometimes happens that someone doesn't look for this particular solution, but has another related problem, and it happens that that exact problem is being discussed in similar questions ;)

it's just the best thing to do :) but of course it's your decision, I only gave you an advice ;)
Ok thanks for your advice then Ciuly, I suppose I could still post the link right?

Regards Paul
well sure :) there are no rules for this afaik. that is why I said it was an advice (suggestion) :)
Avatar of TheRealLoki
FWIW the term for this shortening (where you put 3 dots in the middle) is called "ellipsis"

eg. here is how you would do it on a canvas

 procedure TForm1.PaintBox1Paint(Sender: TObject);
  var
    DTParams: DrawTextParams;
    Rect : TRect;
  begin
    with DTParams do begin
      cbSize := SizeOf(DrawTextParams);
      iTabLength := 6;
      iLeftMargin := 2;
      iRightMargin := 2;
    end;
    Rect := Paintbox1.ClientRect;
    Paintbox1.Canvas.Rectangle(Rect);
    DrawTextEx(Paintbox1.Canvas.Handle, PChar(Paintbox1.Hint), -1,
      Rect, DT_CENTER + DT_VCENTER + DT_SINGLELINE + DT_END_ELLIPSIS,
      @DTParams);
  end;
Hi my dear friends!

I just tried House_of_Dexters reply and this works perfect. Since I already had the FileCtrl unit in my uses clause, his answer beats the shorten procedure from jbshumate.

TheRealLoki > thanks for your reply anyway! I did not further test it since the reply from House_of_Dexter seems the easiest way to do this.

Regards Paul :)
that's ok, i just added it in case others were to search for this solution
Ok thanks for the feedback.

Regards Paul