Что нового

Получение пути к файлу под курсором

CreatoR

Must AutoIt!
Команда форума
Администратор
Сообщения
8,671
Репутация
2,481
Интересует надёжный способ получить полный путь к файлу под курсором, в проводнике или на рабочем столе.

На данный момент делаю так:

Код:
#include 'UIAutomate.au3'

#include <WinAPIEx.au3>
#include <APIConstants.au3>
#include <GUIListView.au3>

HotKeySet('{ESC}', '_Exit')

While 1
	Sleep(100)
	
	$tPoint = _WinAPI_GetMousePos()
	$hWnd = _WinAPI_GetAncestor(_WinAPI_WindowFromPoint($tPoint), $GA_ROOT)
	$sFile = __Explorer_GetHovredItem($hWnd)
	
	If FileExists($sFile) Then
		ToolTip($sFile)
	Else
		ToolTip('')
	EndIf
WEnd

Func _Exit()
	Exit
EndFunc

Func __Explorer_GetHovredItem($hWnd)
	If Not IsHWnd($hWnd) Then
		Return SetError(1)
	EndIf
	
	Local $sPath, $hLV, $aHitTest, $sVal, $oElement, $oParent
	
	If StringRegExp(_WinAPI_GetClassName($hWnd), '^(Progman|WorkerW)$') Then
		$sPath = @DesktopDir
		
		$hLV = ControlGetHandle($hWnd, '', 'SysListView321')
		$aHitTest = _GUICtrlListView_HitTest($hLV)
		
		If IsArray($aHitTest) Then
			$sVal =  _GUICtrlListView_GetItemText($hLV, $aHitTest[0])
			
			;Will not work on x64 OS from x86 AutoIt process (skip to _UIA_* method)
			If $sPath And $sVal Then
				Return $sPath & '\' & $sVal
			EndIf
		EndIf
	Else
		$sPath = __Explorer_GetPath($hWnd)
	EndIf
	
	$oElement = _UIA_GetElementFromPoint(MouseGetPos(0), MouseGetPos(1))
	$oParent = _UIA_ElementGetParent($oElement)
	$sVal = _UIA_ElementGetPropertyValue($oParent, 'LegacyIAccessible.Value')
	
	If $sVal = '' Or Not FileExists($sPath & '\' & $sVal) Then
		$sVal = _UIA_ElementGetPropertyValue($oParent, 'LegacyIAccessible.Name')
		
		If $sVal = '' Or Not FileExists($sPath & '\' & $sVal) Then
			$sVal = _UIA_ElementGetPropertyValue($oElement, 'LegacyIAccessible.Value')
			
			If $sVal = '' Or Not FileExists($sPath & '\' & $sVal) Then
				$sVal = _UIA_ElementGetPropertyValue($oElement, 'LegacyIAccessible.Name')
			EndIf
		EndIf
	EndIf
	
	If $sPath And $sVal Then
		Return $sPath & '\' & $sVal
	EndIf
	
	Return SetError(2, 0, '')
EndFunc

Func __Explorer_GetPath($hWnd)
	If Not IsHWnd($hWnd) Then
		Return SetError(1, 0, '')
	EndIf
	
	If StringRegExp(_WinAPI_GetClassName($hWnd), '^(Progman|WorkerW)$') Then
		Return @DesktopDir
	EndIf
	
	Local $oShell, $oShellWindows, $oSHFolderView, $oObject, $sPath
	
	$oShell = ObjCreate("Shell.Application")
	If Not IsObj($oShell) Then Return SetError(2, 0, '')
	
	$oShellWindows = $oShell.Windows()
	If Not IsObj($oShellWindows) Then Return SetError(3, 0, '')
	
	For $oObject In $oShellWindows
		If $oObject.HWnd = $hWnd Then
			$oSHFolderView = $oObject.Document
			
			If IsObj($oSHFolderView) Then
				$sPath = $oSHFolderView.Folder.Self.Path
				
				If _WinAPI_PathIsDirectory($sPath) Then
					Return $sPath
				EndIf
			EndIf
			
			$sPath = _WinAPI_PathCreateFromUrl($oObject.LocationURL)
			
			If _WinAPI_PathIsDirectory($sPath) Then
				Return $sPath
			EndIf
			
			Return SetError(4, 0, '')
		EndIf
	Next
	
	Return SetError(5, 0, '')
EndFunc


Нужна библиотека UIAutomate.

Но проблема в том, что оно не везде работает, например в окне результатов поиска, там нет возможности получить путь к файлу под курсором, т.к проводник не задаёт путь при поиске.
 
Верх