使用ShLwApi中的PathCompactPathEx进行路径压缩显示
比如将C:\My Installations\Delphi7Compent\Media\CDROM\Disk Images\Disk1\data2.cab显示为C:\My Installati...\data2.cab
API函数为ShlWApi.dll中的PathCompactPathEx,同样的,在此DLL内还有更多关于Path路径的函数。
delphi没有把这个DLL进行封装,但有人已经把它封装好了。下载地址:ftp://delphi-jedi.org/api/Shlwapi.zip
function PathCompactPathExA(pszOut: PAnsiChar; pszSrc: PAnsiChar; cchMax: UINT; dwFlags: DWORD): BOOL; stdcall;
...{$EXTERNALSYM PathCompactPathExA}
function PathCompactPathEx(pszOut: PChar; pszSrc: PChar; cchMax: UINT; dwFlags: DWORD): BOOL; stdcall;
...{$EXTERNALSYM PathCompactPathEx}
function PathCompactPathEx; external shlwapi32 name 'PathCompactPathExA';
function CompressPath(SrcPath:String;DestLength:Integer):String;
var
InBuffer, OutBuffer : array[0..MAX_PATH] of char;
begin
FillChar(InBuffer, MAX_PATH + 1, 0);
FillChar(OutBuffer, MAX_PATH + 1, 0);
StrCopy(InBuffer,PChar(SrcPath));
PathCompactPathEx(OutBuffer, InBuffer, DestLength, 0); //这里的DestLength就是设置长度
Result:=OutBuffer;
end;