VB and VBA Users Source Code: Compact a file path to fit a control
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Compact a file path to fit a control
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, March 12, 2001
Hits:
787
Category:
Windows API
Article:
The following code autofits text in a control by replacing a portion of the text with an ellipsis (...), if the text is wider than a control. This is typically used for showing variable length paths in a label control. The code uses the PathCompactPath function in SHLWAPI. The PathCompactPath function requires three arguments. The first argument contains a device context handle. The second argument holds the address of the pathname you want to use. The third argument contains the width in pixels of the control in which you want the text: Option Explicit Private Declare Function PathCompactPath Lib "shlwapi.dll" Alias "PathCompactPathA" (ByVal hDC As Long, ByVal pszPath As String, ByVal dx As Long) As Long 'Purpose : Compacts a path to fit a control and stores the original value in the tag. 'Inputs : oForm The form the control is resides ' oControl The control to display the path on ' sFilePath The file path to display on the control 'Outputs : The compacted path. This will include "..." for paths which have been compacted 'Author : Andrew Baker (www.vbusers.com) 'Date : 17/11/2000 15:22 'Notes : 'Revisions : 'Assumptions : Function ControlFitPath(oForm As Form, oControl As Control, sFilePath As String) As String Dim lhDC As Long, lCtlWidth As Long, lRet As Long Const clBorder As Long = 5 On Error Resume Next oControl.Tag = sFilePath 'Store the original path in the control's tag If oForm.ScaleMode = vbPixels Then lCtlWidth = oControl.Width - clBorder Else lCtlWidth = (oControl.Width / Screen.TwipsPerPixelX) - clBorder End If lhDC = oForm.hDC ControlFitPath = sFilePath Call PathCompactPath(lhDC, ControlFitPath, lCtlWidth) lRet = InStr(1, ControlFitPath, vbNullChar) If lRet Then 'Trim off characters after null ControlFitPath = Left$(ControlFitPath, lRet - 1) End If End Function 'Example, uses a label on a form. Private Sub Form_Load() Label1.Caption = ControlFitPath(Me, Label1, "C:\MyPath\MyPath\MyPath\MyPath\MyPath\ENDOFPATHNAME") End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder