Page 104 - Open Soource Technologies 304.indd
P. 104
Event Driven Programming
‘if TextSide was neither TextLeft nor TextRight, use TextLeft.
TextSide = TextLeft
End If
If TextSide = TextLeft Then
‘if the text goes on the left, fill out the right with spaces
SizeString = Text & String(Length - Len(Text), sPadChar)
Else
‘otherwise fill on the left and put the Text on the right
SizeString = String(Length - Len(Text), sPadChar) & Text
End If
End Function
5.1.6 PathCompactPathEx
The converse of the SizeString is the PathCompactPathEx Windows API function. While SizeString
is designed to increase the length of a string by padding with a character, the PathCompactPathEx
is used shorten a string to a specified length. PathCompactPathEx is intended to trim fully
qualified filenames to a specified number of characters, omitting or truncating a folder name
component of the full file name, but it can be used with any text string. When used with a fully
qualified file name, it omits part of the file name (such as a folder name) to size the string to
fit in the specified number of characters, replacing the deleted text with the characters “...”. For
example PathCompactPathEx with a length parameter of 30 will shorten the Excel Application’s
file name to a truncated string. It will change
C:\Program Files\Office 2003\OFFICE11\Excel.exe
to
C:\Program Files...\Excel.exe
When PathCompactPathEx is used with arbitrary text without ‘\’ characters, it typically just
truncates the InputString, removing the characters on the right and replacing them with “...”. The
following function, ShortenTextToChars,is a wrapper function for PathCompactPathEx that
handles the variable and string buffer handling, and most importantly the validation of the
NumberOfCharacters parameter. If you pass an invalid NumberOfCharacters value to
PathCompactPathEx , it will cause the application to crash. PathCompactPathEx is unforgiving
of any invalid input parameters. The ShortenTextToChars function uses the
GetSystemErrorMessageText function, available here and the TrimToNull function available here.
Private Declare Function PathCompactPathEx Lib “shlwapi.dll” _
Alias “PathCompactPathExA” ( _
ByVal pszOut As String, _
ByVal pszSrc As String, _
ByVal cchMax As Long, _
ByVal dwFlags As Long) As Long
Public Function ShortenTextToChars(InputText As String, _
NumberOfCharacters As Long) As String
’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
98 LOVELY PROFESSIONAL UNIVERSITY