Bitte beachten:
Diese Funktion setzt voraus, dass der Datei Explorer im Moment der Abfrage das aktive Fenster ist. Den Aufruf müsstest Du dann z. B. auf einen Hotkey legen in einem AutoIt Skript, das im Hintergrund dümpelt.
Hier mal noch eine Version, in der der Datei-Explorer nicht aktiv sein muss! Es wird das topmost Explorerfenster verwendet:
EDIT: Eine Besonderheit von Windows muss man aber selbst beachten. Hat man im Explorerfenster im Tree einen Bibliotheksnamen ausgewählt, liefert Windows nicht den Speicherpfad, da es sich hier um eine Verlinkung handelt. Man bekommt stattdessen einen Leerstring. Hat man die Standardoption mit Ordneranzeige gewählt, erhält man dann einen Backslash und Dateinamen. Wählt man jedoch im Tree den unterhalb des Bibliotheksnamens stehenden Ordner, läuft alles wie gewollt.
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetSelectionFromTopMostExplorer
; Description ...: Gets an array with selected files from the top most explorer window
; Syntax ........: _GetSelectionFromTopMostExplorer([$_bDir = True])
; Parameters ....: $_bDir - [optional] Returns the full path (Default 'True'). With 'False' will only the filename returned.
; Return values .: Array with selected files. Counter at $ar[0]
; Requires.......: _GetTopmostHwndFromProcess()
; Author ........: BugFix
; ===============================================================================================================================
Func _GetSelectionFromTopMostExplorer($_bDir=True)
Local $oShell = ObjCreate("Shell.Application")
Local $oIE, $sPath = '', $oShellWindows = $oShell.Windows
Local $oDoc, $bIE = False, $oFV
For $i = 0 To $oShellWindows.Count -1 ; all opened explorer and internet explorer windows
$oIE = $oShellWindows($i)
$oDoc = $oIE.Document
If _GetTopmostHwndFromProcess('explorer.exe', 'CabinetWClass') = $oIE.Hwnd Then
If StringRegExp(ObjName($oDoc), "IShellFolderViewDual.") Then ; be sure it's the explorer object
$sPath =StringReplace(StringReplace(StringTrimLeft($oIE.LocationURL, 8), '%20', ' '), '/', '\') ; extract windows path from file-url
$bIE = True ; flag: found explorer
EndIf
EndIf
Next
Local $aSelection[500] = [0]
If $bIE Then
$oFV = $oIE.Document ; File-Browser (ShellFolderView)
If Not $_bDir Then $sPath = ''
For $oFI In $oFV.SelectedItems ; selected elements there
$aSelection[0] += 1
If $aSelection[0] > UBound($aSelection) Then ReDim $aSelection[UBound($aSelection) +500]
$aSelection[$aSelection[0]] = StringFormat('%s%s%s', $sPath, ($_bDir ? '\' : ''), $oFI.Name)
Next
EndIf
If $aSelection[0] = 0 Then
ReDim $aSelection[1]
Else
ReDim $aSelection[$aSelection[0]+1]
EndIf
Return $aSelection
EndFunc ;==>_GetSelectionFromTopMostExplorer
; #FUNCTION# ====================================================================================================================
; Name ..........: _GetTopmostHwndFromProcess
; Description ...: Returns the handle of the top most window of a specific process
; Syntax ........: _GetTopmostHwndFromProcess($_vProcess)
; Parameters ....: $_vProcess The Process name or PID
; $_sClass The classname of windows that will created by the specific process
; Return values .: Success The handle of the top most window
; Failure 0, set @error = 1 (process does not exists)
; @error = 2 (there is no window for this)
; Author ........: BugFix
; ===============================================================================================================================
Func _GetTopmostHwndFromProcess($_vProcess, $_sClass)
Local $aProc[2][2] = [[1],['',$_vProcess]]
If IsString($_vProcess) Then
$aProc = ProcessList($_vProcess)
If $aProc[0][0] = 0 Then
Return SetError(1,0,0)
EndIf
Else
If Not ProcessExists($_vProcess) Then Return SetError(1,0,0)
EndIf
Local $hWnd = 0, $aWin = WinList('[CLASS:' & $_sClass & ']')
If $aWin[0][0] = 0 Then Return SetError(2,0,0)
Local $tPID = DllStructCreate("int PID")
; check only the first window, has highest z-order
For $i = 1 To $aProc[0][0]
DllCall("user32.dll", "int", "GetWindowThreadProcessId", "hwnd", $aWin[1][1], "ptr", DllStructGetPtr($tPID))
If $tPID.PID = $aProc[$i][1] Then Return $aWin[1][1]
Next
Return 0
EndFunc ;==>_GetTopmostHwndFromProcess
Alles anzeigen