- Offizieller Beitrag
Hier nun eine Funktion, die einen übergebenen Pfad soweit kürzt, dass er in ein anzugebenes Label passt.
Mit Hilfe von UEZ und peethebee ist das hier entstanden:
Spoiler anzeigen
#include <GDIPlus.au3>
$iFontSize = 16
$sFontname = 'Times New Roman'
$iFontStyle = 0
;~ $sFontname = 'Courier New'
$hGui = GUICreate('Test', 600, 200)
GUISetFont($iFontSize, 400, $iFontStyle, $sFontname)
$hPath1 = GUICtrlCreateLabel('', 10, 10, 300, 50)
GUICtrlSetBkColor(-1, 0xCCCCCC)
$hPath2 = GUICtrlCreateLabel('', 10, 80, 580, 50)
GUICtrlSetBkColor(-1, 0xCCCCCC)
GUISetState()
; Beispiel 1
$sPath1 = 'C:\Users\Oscar\Pictures\Urlaub2011\Pic1045.jpg'
$iTimer = TimerInit()
$sPath1 = _FilePathTrimToLabelWidth($sPath1, $hGui, $hPath1, $iFontSize, $sFontname, $iFontStyle)
ConsoleWrite(Int(TimerDiff($iTimer)) & ' mSec.' & @CR)
GUICtrlSetData($hPath1, $sPath1)
; Beispiel 2
$sPath2 = '\\Server\d\Backup\Users\Oscar\Pictures\Urlaub2011\Pic1045.jpg'
$iTimer = TimerInit()
$sPath2 = _FilePathTrimToLabelWidth($sPath2, $hGui, $hPath2, $iFontSize, $sFontname, $iFontStyle)
ConsoleWrite(Int(TimerDiff($iTimer)) & ' mSec.' & @CR)
GUICtrlSetData($hPath2, $sPath2)
Do
Until GUIGetMsg() = -3
;===============================================================================
; Function Name: _FilePathTrimToLabelWidth($sFilepath, $hGui, $iCtrlID, $iFontSize, $sFontname[, $iFontStyle])
; Description:: Begrenzt einen Dateipfad auf die Breite eines Labels.
; Dabei wird er aber nicht vorn oder hinten abgeschnitten,
; sondern es bleibt das Device erhalten und der rechte Teil
; des Pfades.
; Parameter(s): $sFilepath = der Pfad, der gekürzt werden soll
; $hGui = das Handle des Fensters
; $iCtrlID = die Control ID des Label
; $iFontSize = Fontgröße
; $sFontname = Fontname
; $iFontStyle = Fontstyle (optional)
; Requirement(s): #include <GDIPlus.au3>
; Return Value(s): Der gekürzte Pfad
; Author(s): Oscar + UEZ + peethebee (http://www.autoit.de)
;===============================================================================
Func _FilePathTrimToLabelWidth($sFilepath, $hGui, $iCtrlID, $iFontSize, $sFontname, $iFontStyle = 0)
Local $sDevice, $iLen = StringLen($sFilepath)
_GDIPlus_Startup()
Local $iSize = _GetStringSize($sFilepath, $hGui, $iFontSize, $sFontname, $iFontStyle)
Local $aPosCtrl = ControlGetPos($hGui, '', $iCtrlID)
If $iSize < $aPosCtrl[2] Then Return $sFilepath
$sDevice = StringRegExpReplace($sFilepath, '(?i)([a-z]:\\|\\\\.+?\\[a-z]\\).+', '$1') & '...'
For $i = $iLen To 1 Step -1
$iSize = _GetStringSize($sDevice & StringRight($sFilepath, $i), $hGui, $iFontSize, $sFontname, $iFontStyle)
If $iSize < $aPosCtrl[2] Then ExitLoop
Next
_GDIPlus_Shutdown()
Return $sDevice & StringRight($sFilepath, $i)
EndFunc ;==>_FilePathTrimToLabelWidth
Func _GetStringSize($sString, $hGui, $iFontSize, $sFontname, $iFontStyle)
Local $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui)
Local $hFormat = _GDIPlus_StringFormatCreate()
Local $hFamily = _GDIPlus_FontFamilyCreate($sFontname)
Local $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize, $iFontStyle)
Local $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0)
Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_GraphicsDispose($hGraphic)
Return Int(DllStructGetData($aInfo[0], 'Width'))
EndFunc ;==>_GetStringSize
Edit: Extrem beschleunigt. Schuld waren die dauernden Aufrufe von _GDIPlus_Startup() und _GDIPlus_Shutdown().
Edit2: Es gab noch einen Bug beim RegExp-Pattern. Außerdem habe ich noch ein wenig "aufgeräumt".
Edit3: Eine bessere Lösung für das Problem gibt es hier: _GuiCtrlSetPath