Solved

Fit this path into this edit please!

Posted on 2006-07-10
14
420 Views
Last Modified: 2010-04-05
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
Comment
Question by:PeterdeB
  • 7
  • 4
  • 2
  • +1
14 Comments
 

Author Comment

by:PeterdeB
ID: 17073203
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
 
LVL 28

Expert Comment

by:2266180
ID: 17074953
I am officially confused
0
 
LVL 6

Accepted Solution

by:
House_of_Dexter earned 125 total points
ID: 17075016

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
Independent Software Vendors: 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!

 

Author Comment

by:PeterdeB
ID: 17075849
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
 
LVL 28

Expert Comment

by:2266180
ID: 17075869
in that case, next time, it is better to post the link ;) (will better help future readers)
0
 

Author Comment

by:PeterdeB
ID: 17075913
I think this will help future readers as well. They will find my post and wil find the answer as well.

Regards Paul
0
 
LVL 28

Expert Comment

by:2266180
ID: 17075977
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
 

Author Comment

by:PeterdeB
ID: 17076160
Ok thanks for your advice then Ciuly, I suppose I could still post the link right?

Regards Paul
0
 
LVL 28

Expert Comment

by:2266180
ID: 17076200
well sure :) there are no rules for this afaik. that is why I said it was an advice (suggestion) :)
0
 

Author Comment

by:PeterdeB
ID: 17076291
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17085699
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
 

Author Comment

by:PeterdeB
ID: 17117534
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
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17118397
that's ok, i just added it in case others were to search for this solution
0
 

Author Comment

by:PeterdeB
ID: 17122618
Ok thanks for the feedback.

Regards Paul
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question