Truncating Long File Names and Displaying an Ellipses

I've seen a few articles that illustrate different techniques used in order to display an ellipses in the path. However, a simpler approach might be to use the new Shell path function: PathCompactPath.

BOOL PathCompactPath(
 HDC    hDC,
 LPTSTR lpszPath,
 UINT   dx
);
This function truncates a file path to fit within a given pixel width by replacing path components with ellipses.
  • Returns TRUE if successful, or FALSE otherwise.
hDC
Handle to the device context used for font metrics.
lpszPath
Address of the string to be modified.
dx
Width, in pixels, that the string will be forced to fit within.

This 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.

Example


#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
HDC hdc;  /* display DC handle for current font metrics */ 

void main( void )
{

 // String path name 1.
 char buffer_1[] = "C:\\path1\\path2\\sample.txt";
 char *lpStr1;
 lpStr1 = buffer_1;
	 
 // String path name 2.
 char buffer_2[] = "C:\\path1\\path2\\sample.txt";
 char *lpStr2;
 lpStr2 = buffer_2;
	
 // String path name 3.
 char buffer_3[] = "C:\\path1\\path2\\sample.txt";
 char *lpStr3;
 lpStr3 = buffer_3;

 // String path name 4.
 char buffer_4[] = "C:\\path1\\path2\\sample.txt";
 char *lpStr4;
 lpStr4 = buffer_4;
	
 // Variable to get the return from "PathCompactPath".
 int retval;	
	
 cout << "The un-truncated path is                " << lpStr1 << endl;

 retval = PathCompactPath(hdc,lpStr1,125);
 cout << "The truncated path at 125 pixels is :   " << lpStr1 << endl;

 retval = PathCompactPath(hdc,lpStr2,120);
 cout << "The truncated path at 120 pixels is :   " << lpStr2 << endl;

 retval = PathCompactPath(hdc,lpStr3,110);
 cout << "The truncated path at 110 pixels is :   " << lpStr3 << endl;

 retval = PathCompactPath(hdc,lpStr4,25);
 cout << "The truncated path at  25 pixels is :   " << lpStr4 << endl;
}

Output of Example

The un-truncated path is C:\path1\path2\sample.txt
The truncated path at 125 pixels is : C:\path1\...\sample.txt
The truncated path at 120 pixels is : C:\pat...\sample.txt
The truncated path at 110 pixels is : C:\p...\sample.txt
The truncated path at 25 pixels is : ...\sample.txt


Comments

  • long path tool

    Posted by derikogay on 01/28/2014 05:47am

    Well, you can use Long Path Tool for such problems,it works good I will say.

    Reply
  • Same can be done without shlwapi.dll

    Posted by Legacy on 03/14/2000 12:00am

    Originally posted by: Peter Ritchie

    Both DrawText and DrawTextEx support the DT_PATH_ELLIPSIS flag that allows truncation of a path to fit a specific width (or rectangle) in much the same was a described in this article.

    Reply
  • am i missing something here ?

    Posted by Legacy on 08/02/1999 12:00am

    Originally posted by: Gunnar Bolle

    or isn't that just the help article for PathCompactPath from the MSDN Library ?
    IMO submitting KB or help articles from MSDN library isn't very helpfull - especially if you forget to mention that this function only works with ie 4 installed. (95/NT)

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds