|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic File API
Routines PathCompactPathEx: Adding Ellipses to a File Path |
||
Posted: | Thursday May 20, 2004 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB6, Windows XP | |
OS restrictions: | See prerequisites below | |
Author: | VBnet - Randy Birch | |
Related: |
DrawText: Add Ellipses to a File Path |
Prerequisites |
Version 4.71 and later of Shlwapi.dll. Requires Windows 2000 or later (or Windows NT 4.0 with Internet Explorer 4.0 or later). Requires Windows 98/ME (or Windows 95 with Internet Explorer 4.0 or later). |
|
There
are three methods to adding ellipses to file paths ... DrawText,
PathCompactPath, and PathCompactPathEx. All three are equally effective,
and all three have specific uses.
All three create APIs strings with the ellipses placed at an approximate midpoint inside the string (when possible) to ensure the end result shows both the start and end of the path. When DT_END_ELLIPSIS is specified for DrawText, the ellipses are placed at the end of the string. Like PathCompactPath, the code to create the ellipsed string is concise. To simplify the data I used a simple calculation to create a varying number of characters to display (which is why they don't perfectly fit the text boxes in the demo illustration). As you can imagine this particular method seems most appropriate when you know precisely the number of characters you want to display, rather than relying on a GDI method to actually calculate the size of a particular control. |
BAS Module Code |
None. |
|
Form Code |
Add a text box (Text1), a second text box (Text2) with it's Index set to 0 to create a control array, and a command button (Command1) to a form. The code will create the required array of Text2 controls for the demo. Add the following code: |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const MAX_PATH As Long = 260 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 Private Declare Function lstrlenW Lib "kernel32" _ (ByVal lpString As Long) As Long Private Sub Form_Load() Dim cnt As Long 'set up a few text boxes of varying widths For cnt = 0 To 9 If cnt > 0 Then Load Text2(cnt) End If With Text2(cnt) .Text = "" .Left = Text1.Left .Top = (Text1.Top + Text1.Height) + (210 + (300 * cnt)) .Height = 285 'change the 400 to another value 'to create wider or narrower controls .Width = 1600 + (cnt * 400) .Visible = True End With Next With Command1 .Move Text1.Left, Text2(9).Top + 600, 2400, 345 .Caption = "PathCompactPathEx by Chrs" End With 'a nice long path Text1.Text = "d:\vbasic\vbnet development" & _ " projects\shell\filesystem\" & _ "paths\pathrelativepathto\" & _ "pathrelativepathto.vbp" End Sub Private Sub Command1_Click() 'Adds ellipses to a file path whose 'maximum length is specified (in characters) Dim cnt As Long Dim chrsDesired As Long Dim buff As String buff = Text1.Text For cnt = 0 To 9 'calc an arbitrary number of chrs to display chrsDesired = (cnt + 5) * 4 Text2(cnt).Text = MakeCompactedPathChrs(buff, chrsDesired) Next End Sub Private Function MakeCompactedPathChrs(ByVal sPath As String, _ ByVal cchMax As Long) As String 'Truncates a path to a specified 'number of characters by replacing 'path components with ellipses. Dim ret As Long Dim buff As String 'cchMax is the maximum number of characters 'to be contained in the new string, **including 'the terminating NULL character**. For example, 'if cchMax = 8, the resulting string would contain 'a maximum of 7 characters plus the termnating null. ' 'Because of this, we're add 1 to the value passed 'as cchMax to ensure the resulting string is 'the size requested. cchMax = cchMax + 1 buff = Space$(MAX_PATH) ret = PathCompactPathEx(buff, sPath, cchMax, 0&) MakeCompactedPathChrs = TrimNull(buff) End Function Private Function TrimNull(startstr As String) As String TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr))) End Function |
Comments |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |