Truncating Long File Names and Displaying an Ellipses | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 29, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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:path1path2sample.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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.