Visual Basic File API Routines
PathCompactPath: Adding Ellipses to a File Path
Posted:   Thursday May 20, 2004
Updated:   Monday December 26, 2011
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   See prerequisites below
Author:   VBnet - Randy Birch

DrawText: Add Ellipses to a File Path
PathCompactPath: Adding Ellipses to a File Path
PathCompactPathEx: Adding Ellipses to a File Path

Version 4.71 and later of Shlwapi.dll.
Requires Windows 2000 or later (or Windows NT 4.0 with Internet Explorer 4.0 or later).
Requires Windows 98/ME (or Windows 95 with Internet Explorer 4.0 or later).

There are three methods to adding ellipses to file paths ...  PathCompactPath, PathCompactPathEx and DrawText.
  • PathCompactPath creates a final ellipsed string in a length specified in pixels.
  • PathCompactPathEx creates the ellipsed string by specifying the size of the desired string in characters.
  • DrawText with the DT_PATH_ELLIPSIS style creates the ellipsed string to fit within a specific RECT, e.g. the RECT created by a text box or other control.

All three are equally effective, and all three have specific uses. And all three create strings with the ellipses placed at an approximate midpoint inside the string (when possible) to ensure the output string shows both the start and end of the path. DrawText also offers the DT_END_ELLIPSIS flag which forces the ellipses to the end of the string instead.

This page shows how to call PathCompactPath to ellipse a common string to a number of different widths, all based on the size of the control. Much of the code below creates the controls and handles the calls to the function ... the actual PathCompactPath call is quite compact - no pun intended.

You will note as well that the first ellipsed string in the demo does not show the original string's ".vbp" extension. It's not actually missing - the ellipsed string includes the file extension but the final ellipsed string exceeds the width of the textbox. This is a limitation of the small text box size and demonstrates that it may be possible to obtain unexpected results when the ellipsed string can not be logically broken at a path marker.

 BAS Module Code

 Form Code
Add a text box (Text1), a second text box (Text2) with it's Index set to 0 to create a control array, and a command button (Command1) to a form. The code will create the required array of Text2 controls for the demo. Add the following code:

Option Explicit
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
Private Const MAX_PATH As Long = 260

Private Declare Function PathCompactPath Lib "shlwapi.dll" _
   Alias "PathCompactPathA" _
  (ByVal hdc As Long, _
   ByVal lpszPath As String, _
   ByVal dx As Long) As Long

Private Declare Function lstrlenW Lib "kernel32" _
  (ByVal lpString As Long) As Long

Private Sub Form_Load()

   Dim cnt As Long
  'set up a few text boxes of varying widths
   For cnt = 0 To 9
      If cnt > 0 Then
         Load Text2(cnt)
      End If
      With Text2(cnt)
         .Text = ""
         .Left = Text1.Left
         .Top = (Text1.Top + Text1.Height) + (210 + (300 * cnt))
         .Height = 285
         'change the 400 to another value
         'to create wider or narrower controls
         .Width = 1600 + (cnt * 400)
         .Visible = True

      End With
   With Command1
      .Move Text1.Left, Text2(9).Top + 600, 2400, 345
      .Caption = "PathCompactPath by Pixels"
   End With
  'a nice long path
   Text1.Text = "d:\vbasic\vbnet development" & _
                " projects\shell\filesystem\" & _
                "paths\pathrelativepathto\" & _

End Sub

Private Sub Command1_Click()

  'Truncate the file path to fit within
  'a given pixel width by replacing path
  'components with ellipses.
   Dim cnt As Long
   Dim ctlwidth As Long
   Dim buff As String
   buff = Text1.Text
   For cnt = 0 To 9
     'calc the control with and tweak
     'a bit smaller to allow for the
     'control border and text box white
     'space at the left and right of the text
      ctlwidth = (Text2(cnt).Width / Screen.TwipsPerPixelX) * 0.96
      Text2(cnt).Text = MakeCompactedPathPixels(buff, Me.hdc, ctlwidth)
End Sub

Private Function MakeCompactedPathPixels(ByVal sPath As String, _
                                         dwHdc As Long, _
                                         dwPixels As Long) As String

  'Truncates a file path to fit within
  'a given pixel width by replacing path
  'components with ellipses.
   Dim ret As Long
   Dim buff As String
  'the path to compact and the
  'return buffer are the same string
  'and must be MAX_PATH in length
   buff = sPath & Chr$(0) & Space$(MAX_PATH - Len(sPath) - 1)
   ret = PathCompactPath(dwHdc, buff, dwPixels)
   MakeCompactedPathPixels = TrimNull(buff)
End Function

Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
End Function


PayPal Link
Make payments with PayPal - it's fast, free and secure!


Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy


Hit Counter