• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 442
  • Last Modified:

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 :)
0
PeterdeB
Asked:
PeterdeB
  • 7
  • 4
  • 2
  • +1
1 Solution
 
PeterdeBAuthor Commented:
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 :)
0
 
2266180Commented:
I am officially confused
0
 
House_of_DexterCommented:

uses

  FileCtrl

 
...


var
  a_Dir: string;
begin
  Canvas.Font := Edit1.Font;
  a_edit1.text:= MinimizeName('C:\my very very longdirectory\myshortdir\', Canvas, Edit1.width);
end;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterdeBAuthor Commented:
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 :)
0
 
2266180Commented:
in that case, next time, it is better to post the link ;) (will better help future readers)
0
 
PeterdeBAuthor Commented:
I think this will help future readers as well. They will find my post and wil find the answer as well.

Regards Paul
0
 
2266180Commented:
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 ;)
0
 
PeterdeBAuthor Commented:
Ok thanks for your advice then Ciuly, I suppose I could still post the link right?

Regards Paul
0
 
2266180Commented:
well sure :) there are no rules for this afaik. that is why I said it was an advice (suggestion) :)
0
 
TheRealLokiSenior DeveloperCommented:
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;
0
 
PeterdeBAuthor Commented:
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 :)
0
 
TheRealLokiSenior DeveloperCommented:
that's ok, i just added it in case others were to search for this solution
0
 
PeterdeBAuthor Commented:
Ok thanks for the feedback.

Regards Paul
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now