我们编程时也许遇到过这样的问题:
在一个Label或是Panel上显示路径,当路径过长显示不下的时候,希望像某些安装程序拷贝文件的时候那样把路径缩短,其中一部分用省略号代替,比如:
C:\Program Files\Borland\Delphi5\Source\Rtl\Win-->C:\Program Files\Borland\...\Win
自己编程实现并不难,不过不知什么原因,我一直没有动手做,忽然有一天,我看到了一篇文章,于是,一切问题迎刃而解,随之而来的,竟是意想不到的收获→
那是一段Visual Basic的程序,不过,我第一眼就看到了一个函数的声明 PathCompactPath:
Private Declare Function PathCompactPath Lib "shlwapi" Alias "PathCompactPathA"(ByVal hDC As Long, ByVal lpszPath As String, ByVal dx As Long) As Long
经过几番修改,在Delphi中试验通过了,果然能够做到压缩路径的效果,但我更感兴趣的是,ShlWapi.dll中是不是还有不少可以用的好东东呢?打开MSDN,敲入"ShlWapi"一搜索,果然出现一堆(注意,是"一堆")以"Path"开头的函数,欣喜之情,不在话下。于是我便一个个查看其功能,发现我们需要的关于路径的几乎所有功能都有相应的的函数可以调用,比如:
PathAddBackslash、PathRemoveBackslash:在路径后面添加或去除"\";
PathIsDirectory、PathIsHTMLFile、PathIsPrefix、PathIsRoot、PathIsURL……
等等,随便试了几个,可以用,接着我又琢磨如何找出其中全部的函数声明,我知道很多动态链接库在MSDN上都有相应的头文件,这次也许不会例外。果然又被我猜中!ShlWapi.H确实存在。接下来的工作就比较烦了,花了些时间,以"查找、替换"大法为辅助,我把其中关于路径操作的函数声明做成了ShlWapi.pas(ShlWapi.H中包括几部分的函数声明:字符串、路径、注册表、注册表流、调色板,还有一个很有用的函数DllGetVirsion,完整的声明可以在这里下载)。
再来说说PathComact(或者叫PathEllipsis),PathCompactPath函数需要设备的HDC做参数,使用起来可能会麻烦一点,所以还有另外一个函数PathCompactPathEx,参数与设备无关,不过关于字符和显示宽度的换算也有些地方需要推敲
unit ShlWapi;
interface
uses
windows;
function PathAddBackslash(pszPath: LPSTR): LPSTR; stdcall; external 'shlwapi.dll' name 'PathAddBackslashA';
function PathAddExtension(pszPath: LPSTR; pszExt: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathAddExtensionA';
function PathAppend(pszPath: LPSTR; pMore: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathAppendA';
function PathBuildRoot(szRoot: LPSTR; iDrive: integer): LPSTR; stdcall; external 'shlwapi.dll' name 'PathBuildRootA';
function PathCanonicalize(pszBuf: LPSTR; pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathCanonicalizeA';
function PathCombine(szDest: LPSTR; lpszDir: LPCSTR; lpszFile: LPCSTR): LPSTR; stdcall; external 'shlwapi.dll' name 'PathCombineA';
function PathCompactPath(hDC: hDC; lpszPath: Pchar; dx: integer): Bool; stdcall; external 'shlwapi.dll' name 'PathCompactPathA';
function PathCompactPathEx(pszOut: LPSTR; pszSrc: LPCSTR; cchMax: Uint; dwFlags: DWORD): Bool; stdcall; external 'shlwapi.dll' name 'PathCompactPathExA';
function PathCommonPrefix(pszFile1: LPCSTR; pszFile2: LPCSTR; achPath: LPSTR): integer; stdcall; external 'shlwapi.dll' name 'PathCommonPrefixA';
function PathFileExists(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathFileExistsA';
function PathFindExtension(pszPath: LPCSTR): LPSTR; stdcall; external 'shlwapi.dll' name 'PathFindExtensionA';
function PathFindFileName(pszPath: LPCSTR): LPSTR; stdcall; external 'shlwapi.dll' name 'PathFindFileNameA';
function PathFindNextComponent(pszPath: LPCSTR): LPSTR; stdcall; external 'shlwapi.dll' name 'PathFindNextComponentA';
function PathGetArgs(pszPath: LPCSTR): LPSTR; stdcall; external 'shlwapi.dll' name 'PathGetArgsA';
function PathGetCharType(ch: UCHAR): Uint; stdcall; external 'shlwapi.dll' name 'PathGetCharTypeA';
function PathGetDriveNumber(pszPath: LPCSTR): integer; stdcall; external 'shlwapi.dll' name 'PathGetDriveNumberA';
function PathIsDirectory(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsDirectoryA';
function PathIsFileSpec(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsFileSpecA';
function PathIsPrefix(pszPrefix: LPCSTR; pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsPrefixA';
function PathIsRelative(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsRelativeA';
function PathIsRoot(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsRootA';
function PathIsSameRoot(pszPath1: LPCSTR; pszPath2: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsSameRootA';
function PathIsUNC(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsUNCA';
function PathIsUNCServer(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsUNCServerA';
function PathIsUNCServerShare(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsUNCServerShareA';
function PathIsContentType(pszPath: LPCSTR; pszContentType: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsContentTypeA';
function PathIsURL(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathIsURLA';
function PathMakePretty(pszPath: LPSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathMakePrettyA';
function PathMatchSpec(pszFile: LPCSTR; pszSpec: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathMatchSpecA';
function PathParseIconLocation(pszIconFile: LPSTR): integer; stdcall; external 'shlwapi.dll' name 'PathParseIconLocationA';
procedure PathQuoteSpaces(lpsz: LPSTR); stdcall; external 'shlwapi.dll' name 'PathQuoteSpacesA';
function PathRelativePathTo(pszPath: LPSTR; pszFrom: LPCSTR; dwAttrFrom: DWORD; pszTo: LPCSTR; dwAttrTo: DWORD): Bool; stdcall; external 'shlwapi.dll' name 'PathRelativePathToA';
procedure PathRemoveArgs(pszPath: LPSTR); stdcall; external 'shlwapi.dll' name 'PathRemoveArgsA';
function PathRemoveBackslash(pszPath: LPSTR): LPSTR; stdcall; external 'shlwapi.dll' name 'PathRemoveBackslashA';
procedure PathRemoveBlanks(pszPath: LPSTR); stdcall; external 'shlwapi.dll' name 'PathRemoveBlanksA';
procedure PathRemoveExtension(pszPath: LPSTR); stdcall; external 'shlwapi.dll' name 'PathRemoveExtensionA';
function PathRemoveFileSpec(pszPath: LPSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathRemoveFileSpecA';
function PathRenameExtension(pszPath: LPSTR; pszExt: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathRenameExtensionA';
function PathSearchAndQualify(pszPath: LPCSTR; pszBuf: LPSTR; cchBuf: Uint): Bool; stdcall; external 'shlwapi.dll' name 'PathSearchAndQualifyA';
procedure PathSetDlgItemPath(hDlg: HWND; id: integer; pszPath: LPCSTR); stdcall; external 'shlwapi.dll' name 'PathSetDlgItemPathA';
function PathSkipRoot(pszPath: LPCSTR): LPSTR; stdcall; external 'shlwapi.dll' name 'PathSkipRootA';
procedure PathStripPath(pszPath: LPSTR); stdcall; external 'shlwapi.dll' name 'PathStripPathA';
function PathStripToRoot(pszPath: LPSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathStripToRootA';
procedure PathUnquoteSpaces(lpsz: LPSTR); stdcall; external 'shlwapi.dll' name 'PathUnquoteSpacesA';
function PathMakeSystemFolder(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathMakeSystemFolderA';
function PathUnmakeSystemFolder(pszPath: LPCSTR): Bool; stdcall; external 'shlwapi.dll' name 'PathUnmakeSystemFolderA';
function PathIsSystemFolder(pszPath: LPCSTR; dwAttrb: DWORD): Bool; stdcall; external 'shlwapi.dll' name 'PathIsSystemFolderA';
implementation
end.