After toying around with Shell objects and the like, I've shortened the code even more. Note that you'll need to add code for an AutoIt COM error handler. I removed that because it shouldn't be part of a UDF function in my opinion.
Also, with separating out the code to get the 'ShellFolderView' object, its possible to do other things like selecting, focusing, and other Shell object operations.
; ==========================================================================================================================
; Func _ObjectSHFolderViewFromWin($hWnd)
;
; Returns an 'ShellFolderView' Object for the given Window handle
;
; Author: Ascend4nt, based on code by KaFu, klaus.s
; ==========================================================================================================================
Func _ObjectSHFolderViewFromWin
($hWnd)
If Not IsHWnd($hWnd) Then Return SetError(1
,0
,0
)
Local $oShell,$oShellWindows,$oIEObject,$oSHFolderView
; Shell Object
$oShell=ObjCreate("Shell.Application")
If Not IsObj($oShell) Then Return SetError(2,0,0)
; Get a 'ShellWindows Collection' object
$oShellWindows = $oShell.Windows()
If Not IsObj($oShellWindows) Then Return SetError(3,0,0)
; Iterate through the collection - each of type 'InternetExplorer' Object
For $oIEObject In $oShellWindows
If $oIEObject.HWND = $hWnd Then
; InternetExplorer->Document = ShellFolderView object
$oSHFolderView=$oIEObject.Document
If IsObj($oSHFolderView) Then Return $oSHFolderView
Return SetError(4
,0
,0
)
EndIf
Next
Return SetError(-1
,0
,0
)
EndFunc
; ==========================================================================================================================
; Func _ExplorerWinGetSelectedItems($hWnd)
;
;
; Author: klaus.s, KaFu, Ascend4nt (consolidation & cleanup, Path name simplification)
; ==========================================================================================================================
Func _ExplorerWinGetSelectedItems
($hWnd)
If Not IsHWnd($hWnd) Then Return SetError(1,0,'')
Local $oSHFolderView
Local $iSelectedItems,$iCounter=2
,$aSelectedItems[2] = [0, ""]
$oSHFolderView=_ObjectSHFolderViewFromWin
($hWnd)
If @error Then Return SetError(@error,0,'')
; SelectedItems = FolderItems Collection object->Count
$iSelectedItems = $oSHFolderView.SelectedItems.Count
Dim $aSelectedItems[$iSelectedItems+2] ; 2 extra -> 1 for count [0], 1 for Folder path [1]
$aSelectedItems[0
]=$iSelectedItems
; ShellFolderView->Folder->Self as 'FolderItem'->Path
$aSelectedItems[1
]=$oSHFolderView.Folder.Self.Path
; ShellFolderView->SelectedItems = FolderItems Collection object
$oSelectedFolderItems = $oSHFolderView.SelectedItems
#cs
; For ALL items in an Explorer window (not just the selected ones):
$oSelectedFolderItems = $oSHFolderView.Folder.Items
ReDim $aSelectedItems[$oSelectedFolderItems.Count+2]
#ce
For $oFolderItem In $oSelectedFolderItems
$aSelectedItems[$iCounter] = $oFolderItem.Path
$iCounter += 1
Next
Return SetExtended($iCounter-2
,$aSelectedItems)
EndFunc ;==>_ExplorerWinGetSelectedItems
; ==========================================================================================================================
; ==========================================================================================================================
#include <Array
.au3>
$aWinList=WinList("[REGEXPCLASS:^(Explore|Cabinet)WClass$]")
For $i = 1
To $aWinList[0
][0
]
$aSelection = _ExplorerWinGetSelectedItems
($aWinList[$i][1
])
_ArrayDisplay($aSelection, "Explorer Instance #" & $i & " / " & $aWinList[$i][0
])
Next
Here's code to select only 1 item (and focus on it), then select all, then deselect all:
$hWin=WinGetHandle('[REGEXPCLASS:^(Explore|Cabinet)WClass$]')
$hCtrl=ControlGetHandle($hWin,'','[CLASS:SysListView32; INSTANCE:1]')
; Windows 7
If $hCtrl='' Then $hCtrl=ControlGetHandle($hWin,'','[CLASS:DirectUIHWND]')
$sFileToSelect=''
$oSHFolderView=_ObjectSHFolderViewFromWin
($hWin)
If Not @error Then
; Comment this out to choose your own file here:
$sFileToSelect=$oSHFolderView.FocusedItem.Name
ConsoleWrite("Focusing on : "&$sFileToSelect&@CRLF)
WinActivate($hWin)
; Select ONLY the item
$oSHFolderView.SelectItem($oSHFolderView.Folder.ParseName($sFileToSelect),4
+1
+8
+16
)
Sleep(1000)
; Select ALL items (easiest way):
ControlSend($hWin,'',$hCtrl,'^a')
Sleep(1000)
; Deselect ALL items, while maintaining 'focused item'
$oSHFolderView.SelectItem($oSHFolderView.FocusedItem,4
)
EndIf
*Edit: Some helpful links:
Shell Objects
InternetExplorer Object
Edited by Ascend4nt, 03 October 2011 - 09:46 PM.