PathCompactPathA 函式 (shlwapi.h)

藉由以省略號取代路徑元件,截斷要符合指定圖元寬度的檔案路徑。

語法

BOOL PathCompactPathA(
  [in]      HDC   hDC,
  [in, out] LPSTR pszPath,
  [in]      UINT  dx
);

參數

[in] hDC

類型: HDC

用於字型計量之裝置內容的句柄。 此值可以是 NULL

[in, out] pszPath

類型: LPTSTR

長度為 null 之字串的指標,MAX_PATH包含要修改的路徑。 傳回時,這個緩衝區會包含修改過的字串。

[in] dx

類型: UINT

字串必須符合的寬度,以像素為單位。

傳回值

類型: BOOL

如果路徑已成功壓縮為指定的寬度,則傳回 TRUE 。 失敗時會傳回 FALSE,如果路徑的基底部分不符合指定的寬度,則傳回 FALSE

備註

此函式會使用 目前在 hDC 中選取的字型來計算文字的寬度。 此函式不會壓縮基底檔名前面加上省略號的路徑。

範例

#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

HDC hdc;  /* display DC handle to current font metrics */ 

void main( void )
{
// String path name 1.
char buffer_1[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
char *lpStr1;
lpStr1 = buffer_1;

// String path name 2.
char buffer_2[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
char *lpStr2;
lpStr2 = buffer_2;

// String path name 3.
char buffer_3[MAX_PATH] = "C:\\path1\\path2\\sample.txt";
char *lpStr3;
lpStr3 = buffer_3;

// String path name 4.
char buffer_4[MAX_PATH] = "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:
===========
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

注意

shlwapi.h 標頭會將PathCompactPath定義為別名,根據UNICODE預處理器常數的定義,自動選取此函式的ANSI或Unicode版本。 混合使用編碼中性別名與非編碼中性的程序代碼,可能會導致編譯或運行時間錯誤不符。 如需詳細資訊,請參閱 函式原型的慣例

規格需求

需求
最低支援的用戶端 Windows 2000 專業版、Windows XP [僅限傳統型應用程式]
最低支援的伺服器 Windows 2000 Server [僅限傳統型應用程式]
目標平台 Windows
標頭 shlwapi.h
程式庫 Shlwapi.lib
Dll Shlwapi.dll (4.71 版或更新版本)