Rubrik: Dateisystem · Dateien allgemein | VB-Versionen: VB4, VB5, VB6 | 10.05.04 |
Pfad gekürzt anzeigen II Dieser Tipp verrät, wie sich eine Pfadangabe gekürzt anzeigen lässt, sollte der Pfad nicht in das vorgesehene Control passen. | ||
Autor: Dieter Otter | Bewertung: | Views: 10.513 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Vor wenigen Tagen haben wir Ihnen gezeigt, wie sich ein Pfad gekürzt anzeigen lässt, sollte dieser nicht das vorgesehene Control passen:
Pfad gekürzt anzeigen
Nachteil des Codes:
Es wird der Internet-Explorer ab Version 4 benötigt!
Heute möchten wir Ihnen eine Alternative vorstellen, die ohne installierten Internet Explorer auskommt, und ebenso gute Dienste leistet.
Option Explicit ' benötigte API-Deklaration Private Declare Function DrawText Lib "user32" _ Alias "DrawTextA" ( _ ByVal hdc As Long, _ ByVal lpStr As String, _ ByVal nCount As Long, _ lpRect As cRECT, _ ByVal wFormat As Long) As Long Private Type cRECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Const DT_PATH_ELLIPSIS = &H4000 Private Const DT_MODIFYSTRING = &H10000 Private Const DT_SINGLELINE = &H20&
' Pfad optimal kürzen, falls notwendig Public Function CompactPath(oForm As Form, _ ByVal sPath As String, _ oControl As Control) As String Dim nWidth As Long Dim R As cRECT ' Breite der Controls in Pixel R.Right = oControl.Width / Screen.TwipsPerPixelX ' API-Funktion aufrufen, um Pfad ggf. zu kürzen DrawText oForm.hdc, sPath, -1, R, _ DT_PATH_ELLIPSIS Or DT_SINGLELINE Or DT_MODIFYSTRING If InStr(sPath, Chr$(0)) > 0 Then sPath = Left$(sPath, InStr(sPath, Chr$(0)) - 1) End If CompactPath = sPath End Function
Aufruf der Funktion:
Dim sPath As String sPath = "C:\Programme\Gemeinsame Dateien\Microsoft Shared\DAO\DAO360.DLL" Label1.Caption = CompactPath(Me, sPath, Label1)
Hinweis:
Damit der Code korrekt funktioniert, muss die Font-Eigenschaft des Label-Controls mit der Font-Eigenschaft der Form übereinstimmen!