本文分享了一段使用 AutoHotkey 通过 COM 对象 Shell.Application 遍历 Windows 资源管理器(Explorer)与 IE 窗口的示例代码。核心思路是利用该对象的 Windows 属性返回的 ShellWindows 集合,逐一枚举每个文件管理器/浏览器窗口,进而读取或操作窗口相关信息。
使用要点:先创建 Shell.Application 实例,再通过 obj.Windows 取得 ShellWindows 集合;遍历集合中的每个 ShellWindow 对象可访问其 Document、LocationName、LocationURL、HWND 等属性,据此判断并操纵对应窗口。需注意该集合同时涵盖资源管理器与旧版 IE 窗口,遍历时可按需过滤。本文代码基于 AHK v1 语法;AHK v2 中 COM 创建改用 ComObject,其余对象操作方式基本一致。
;~ COM Object: Shell.Application
;~ Purpose: Access Explorer & IE Windows/Tabs; Open & Manipulate Windows
;~ System Requirements: General
;~ Documentation Link: Shell Object
;~ Other Links: ShellWindows Collection
;~ Code Example:
;~ The Windows Property returns the ShellWindows Collection, which is extremely helpful for accessing Explorer/IE Windows. This example Enumerates all the Explorer & IE Windows/Tabs:
;~ Code:
SetFormat, IntegerFast, Hex ;// display HWND in Hex
for Item in ComObjCreate("Shell.Application").Windows ;// loop through the ShellWindows Collection
data .= "Name:`t" Item.LocationName "`n"
. "URL:`t" Item.LocationURL "`n"
. "Prog:`t" Item.Name "`n"
. "`t" Item.FullName "`n"
. "HWND:`t" Item.HWND "`n`n"
MsgBox %data%

