The new vbAccelerator Site - more VB and .NET Code and Controls

Compact a Long Path Name to fit a given space


Steve McMahon(





Other Tips
All Tips
By Date
By Subject

API (33)
Manipulation (3)

Clipboard (3)
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Box (2)

Windows (11)
Controls (10)


Often there is not enough room to display all of a long path name. A neat way to format a path name in a given space is to show some of the beginning of the path and the filename itself, whilst missing out some of the central folders and replacing them with ellipses (..) This method is used in many Microsoft applications.

This tip shows how to use the either the DrawText or the PathCompactPath API functions to get this effect.

Start a new project in VB. Add a new module, and add the following code:

Private Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Const DT_BOTTOM = &H8&
Private Const DT_CENTER = &H1&
Private Const DT_LEFT = &H0&
Private Const DT_CALCRECT = &H400&
Private Const DT_WORDBREAK = &H10&
Private Const DT_VCENTER = &H4&
Private Const DT_TOP = &H0&
Private Const DT_TABSTOP = &H80&
Private Const DT_SINGLELINE = &H20&
Private Const DT_RIGHT = &H2&
Private Const DT_NOCLIP = &H100&
Private Const DT_INTERNAL = &H1000&
Private Const DT_EXTERNALLEADING = &H200&
Private Const DT_EXPANDTABS = &H40&
Private Const DT_CHARSTREAM = 4&
Private Const DT_NOPREFIX = &H800&
Private Const DT_EDITCONTROL = &H2000&
Private Const DT_PATH_ELLIPSIS = &H4000&
Private Const DT_END_ELLIPSIS = &H8000&
Private Const DT_MODIFYSTRING = &H10000
Private Const DT_RTLREADING = &H20000
Private Const DT_WORD_ELLIPSIS = &H40000

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

Public Function CompactedPath( _
ByVal sPath As String, _
ByVal lMaxPixels As Long, _
ByVal hDC As Long _
) As String
Dim tR As RECT
tR.right = lMaxPixels
CompactedPath = sPath
End Function

Public Function CompactedPathSh( _
ByVal sPath As String, _
ByVal lMaxPixels As Long, _
ByVal hDC As Long _
) As String
Dim lR As Long
Dim iPos As Long
lR = PathCompactPath(hDC, sPath, lMaxPixels)
iPos = InStr(sPath, Chr$(0))
If iPos 0 Then
CompactedPathSh = left$(sPath, iPos - 1)
CompactedPathSh = sPath
End If
End Function

To try out the code, add a command button to your test project's main form. Then add a two label controls to the form, sizing the second one to the size you'd like the path to appear as. Type an obnoxiously long path containing spaces into the caption of the first label control, for example:

C:\Program Files\Microsoft DevStudio\Visual Basic\Samples\Unsupported Samples\Etc

Then add the following code to demonstrate the path compacting:

Private Sub Command1_Click()
' Use either the CompactedPath or CompactedPathSh functions - they ' work exactly the same. Label2.Caption = CompactedPathSh(Label1.Caption, Label2.Width \ Screen.TwipsPerPixelX, Me.hDC) End Sub

Run the project. When you click the command button, the second label control will display a neatly compacted version of the original path.


Related Tips and Articles:



AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( All Rights Reserved.
Last updated: 17/08/99