Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / Visual Basic
Article

Use the .NET framework to shorten a path string with an ellipsis

Rate me:
Please Sign up or sign in to vote.
4.95/5 (21 votes)
8 Apr 2007CPOL2 min read 96.3K   42   16
Compact a path or any string using built in .NET framework API

Introduction

Remember the Windows API PathCompactPath[Ex] used to take a path and shorten it by inserting an ellipsis in the appropriate place to make it fit a specific pixel width? Ever wonder how to do it native .NET? There's a very simple way to do it with the TextRenderer.MeasureText method but it's relatively undocumented.

Background

If you're familiar with this problem you might already know about using Windows.Forms.TextRenderer.DrawText or Drawing.Graphics.DrawString methods to programmatically draw a shortened string using the framework. But I wanted a way to retrieve a shortened string in memory to later use that string inside an existing control (like a menu item).

Using the Code

Here's a simple function to do the trick, internally I copy the string (to keep it from affecting the source string), call Windows.Forms.TextRenderer.MeasureText with the magic bitwise parameter: TextFormatFlags.ModifyString, and return the string variable that was passed into the function.

VB.NET
Function CompactString(ByVal MyString As String, ByVal Width As Integer,
    ByVal Font As Drawing.Font,
    ByVal FormatFlags As Windows.Forms.TextFormatFlags) As String

Dim Result As String = String.Copy(MyString)

TextRenderer.MeasureText(Result, Font, New Drawing.Size(Width, 0),
    FormatFlags Or TextFormatFlags.ModifyString)

Return Result

End Function

To set a label to a compacted version of the string path you would call:

VB.NET
Label1.Text = CompactString(MyPath, Label1.Width, Label1.Font,
   TextFormatFlags.PathEllipsis)

For example, the string: c:\program files\test app\runme.exe might turn into:

c:\program files\...\runme.exe depending on the font and width.

Check out the Windows.Forms.TextFormatFlags enumerated type for other options on how to compact your string. TextFormatFlags.WordEllipsis for example will insert a "..." at the end of the string instead in between parts of a path.

Points of Interest

You might be thinking, why not just write your own algorithm to break apart a string and insert the ellipsis in a certain spot. And in fact I have seen programmers doing this but I would recommend using the framework to handle this for a couple reasons:

  1. Why reinvent the wheel? Microsoft already did it and it's probably well tested and efficient; do you really have extra time on your hands?
  2. What if the standard for where to put the ellipsis changes? Maybe someday the ellipsis will be commonly inserted just after the drive letter instead of just before the filename. Let MS think about these details and just follow their lead. If MS changes this implementation internally, your app will automatically follow suit.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am a software architect and engineer who has been working in the industry since 1994. Although I officially started writing software many years earlier at age 11 by authoring games, graphics demos, or whatever I found interesting. I even landed my first consulting job at age 16 implementing a newly invented mathimatical algorithm for a team of researchers at the local University.

I've designed and implemented hundreds if not thousands of applications: client, web, and hybrid of all scales and was a contributing author to the Black Belt programming column of Visual Basic Programmer's Journal.

My goals when coding are always to write clean, reusable and optimized code using the latest and technology, tools and techniques.

Comments and Discussions

 
GeneralMy vote of 5 Pin
John Pap6-Apr-11 5:52
John Pap6-Apr-11 5:52 
QuestionHow about WPF? Pin
GLLNS18-May-09 8:51
GLLNS18-May-09 8:51 
GeneralMore Information Pin
Andy Missico26-Feb-09 15:51
Andy Missico26-Feb-09 15:51 
I am providing more information regarding the PathCompactPath function to help programmers avoid its use in .NET Framework version 2.0.

http://www.missico.com/personal/support/visual_basic_tips.htm#PathCompactPath[^]

''' <summary>
''' Truncates a file path to fit within a given pixel width by replacing path components with ellipses.
''' </summary>
''' <param name="hdc">[in] A handle to the device context used for font metrics.</param>
''' <param name="lpszPath">[in, out] A pointer to a null-terminated string of length MAX_PATH that contains the path to be modified. On return, this buffer will contain the modified string.</param>
''' <param name="dx">[in] The width, in pixels, in which the string must fit.</param>
''' <returns>Returns TRUE if the path was successfully compacted to the specified width. Returns FALSE on failure, or if the base portion of the path would not fit the specified width.</returns>
''' <remarks>
''' <para>This Shell Lightweight Utility function uses the font currently selected in hDC to calculate the width of the text. This function will not compact the path beyond the base file name preceded by ellipses.</para>
''' <para>The .NET Framework version 2.0 contains the TextRenderer.MeasureText method, which can reproduce the functionality of the PathCompactPath function, as shown in the following example.</para>
''' <example>
''' <code>
'''    Public Function CompactPath(ByVal text As String, ByVal control As Control) As String
''' 
'''        'make a copy because the measure text call is instructed to modify string
'''        Dim sResult As String = String.Copy(text)
''' 
'''        TextRenderer.MeasureText(sResult, control.Font, control.ClientSize, TextFormatFlags.PathEllipsis Or TextFormatFlags.ModifyString)
'''  
'''        Return sResult
''' 
'''    End Function
''' </code>
''' </example>
''' </remarks>
<Obsolete("In .NET 2.0, use TextRenderer.MeasureText with TextFormatFlags.PathEllipsis", True)> _
Declare Auto Function PathCompactPath Lib "shlwapi.dll" ( _
    ByVal hdc As IntPtr, _
    ByVal lpszPath As System.Text.StringBuilder, _
    ByVal dx As Int32) As Boolean
 
''' <summary>
''' Truncates a file path to fit within a given pixel width by replacing path components with ellipses.
''' </summary>
''' <returns>Returns the path compacted to the specified width. This function will not compact the path beyond the base file name preceded by ellipses. Therefore, the base portion of the path may not always fit within the specified width.</returns>
''' <remarks>
''' <para>Works well with monospace/fixed-width fonts. Tends to compact text too much when using proportional fonts.</para>
''' </remarks>
Public Function CompactPath(ByVal path As String, ByVal control As Control) As String
    Const MAX_PATH As Integer = 260
 
    Dim oPath As New System.Text.StringBuilder(path, MAX_PATH)
    Dim oGraphics As System.Drawing.Graphics = control.CreateGraphics
 
    Try
 
        If PathCompactPath(oGraphics.GetHdc, oPath, control.ClientSize.Width) Then
            'okay
        Else
            'failure or base name too long for specified width
        End If
 
    Finally
 
        If Not (oGraphics Is Nothing) Then
 
            'if you do not release the device context then you will get
            '   An unhandled exception of type 'System.InvalidOperationException' occurred in system.drawing.dll
            '   The object is currently in use elsewhere.
 
            oGraphics.ReleaseHdc()
            oGraphics.Dispose()
 
        End If
 
    End Try
 
    Return oPath.ToString
End Function

GeneralStrange Behavior Pin
cjbarth9-Jan-09 10:59
cjbarth9-Jan-09 10:59 
QuestionHow can we get the modified string? Pin
Waleed Eissa5-Sep-08 19:10
Waleed Eissa5-Sep-08 19:10 
GeneralThis code will fail with AccessViolationException Pin
Eugene Sichkar23-Jun-08 13:44
Eugene Sichkar23-Jun-08 13:44 
GeneralTruncate to null Pin
Don Kackman11-Nov-07 3:03
Don Kackman11-Nov-07 3:03 
Questionshorten path for recent files Pin
Zach198123-Oct-07 1:52
Zach198123-Oct-07 1:52 
AnswerRe: shorten path for recent files Pin
chr.dev15-Jul-08 2:32
chr.dev15-Jul-08 2:32 
GeneralRemoved from .NET 3.0 Pin
RichardCollins224-May-07 22:58
RichardCollins224-May-07 22:58 
GeneralRe: Removed from .NET 3.0 Pin
Greg Osborne8-Jan-09 5:37
Greg Osborne8-Jan-09 5:37 
QuestionCopy? Pin
Nchantim10-Apr-07 7:11
Nchantim10-Apr-07 7:11 
AnswerRe: Copy? Pin
Nchantim10-Apr-07 7:24
Nchantim10-Apr-07 7:24 
GeneralRe: Copy? Pin
Tim Greenfield12-Apr-07 5:06
Tim Greenfield12-Apr-07 5:06 
GeneralRe: Copy? Pin
RichardCollins220-May-07 22:55
RichardCollins220-May-07 22:55 
GeneralNice Pin
Patrick Etc.8-Apr-07 14:24
Patrick Etc.8-Apr-07 14:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.