以下代码实现了一些针对 Windows 资源管理器的高级操作封装,尤其是文件路径的获取、选中文件以及打开文件夹等操作。
功能说明
1. 获取资源管理器的路径
- 方法:
Explorer_GetPath(hwnd="")
- 功能:
- 获取当前活动资源管理器窗口的路径。
- 如果当前窗口是桌面,返回桌面路径。
2. 获取资源管理器的所有目录项
- 方法:
Explorer_GetAll(hwnd="")
- 功能:
- 返回当前资源管理器窗口中所有文件和文件夹的完整路径列表。
3. 获取资源管理器中选中的文件或文件夹
- 方法:
Explorer_GetSelected(hwnd="")
- 功能:
- 返回资源管理器窗口中当前选中的文件或文件夹的完整路径。
4. 获取资源管理器窗口对象
- 方法:
Explorer_GetWindow(hwnd="")
- 功能:
- 获取当前资源管理器窗口的 COM 对象。
- 如果是桌面窗口,返回 "desktop"。
5. 打开目录并选中文件
- 方法:
File_OpenAndSelect(sFullPath)
- 功能:
- 打开资源管理器到指定目录并选中单个文件。
6. 打开目录并选中多个文件
- 方法:
Files_OpenAndSelect(path, selfilearr)
- 功能:
- 打开资源管理器到指定目录并选中多个文件。
代码示例:
;方法 Explorer_GetPath(hwnd="")--获取当前资源管理器路径
;方法 Explorer_GetAll(hwnd="")--获取资源管理器目录
;方法 Explorer_GetSelected(hwnd="")--获取资源管理器已选择文件目录
;方法 Explorer_GetWindow(hwnd="")获取资源管理器窗口名字
;方法 Explorer_Get(hwnd="",selection=false)获取资源管理器目录获取
;方法 File_OpenAndSelect(path, selfilearr) --打开资源管理器并选中文件
;方法 Files_OpenAndSelect(path, selfilearr) --打开资源管理器并选中一个或多个文件
;历史
;2021-01-21 修复夹带16进制的bug
;示例
myfile:=New fileplus
F1::
path := myfile.Explorer_GetPath() ; 获取当前资源管理器路径
all := myfile.Explorer_GetAll() ; 遍历当前资源管理器目录文件名
sel := myfile.Explorer_GetSelected() ; 获取资源管理器已选择文件目录名
; MsgBox % path
; MsgBox % all
MsgBox % sel
; myfile.File_OpenAndSelect("D:\Pstools\1111.png") ; 单文件选中写法
; myfile.Files_OpenAndSelect("D:\Pstools",["1111.png","Hash.exe"]) ; 多文件选中写法
Return
Class FilePlus {
;方法 Explorer_GetWindow(hwnd="")获取管理器窗口名字
;参数hwnd
Explorer_GetWindow(hwnd="") {
; thanks to jethrow for some pointers here
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process!="explorer.exe")
return
if (class ~= "(Cabinet|Explore)WClass") {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
return window
} else if (class ~= "Progman|WorkerW")
return "desktop" ; desktop found
}
;方法 Explorer_Get(hwnd="",selection=false)获取管理器目录获取
;参数hwnd
;参数selection 布尔类型
Explorer_Get(hwnd="",selection=false) {
if !(window := this.Explorer_GetWindow(hwnd))
return ErrorLevel := "ERROR"
if (window="desktop") {
ControlGet, hwWindow, HWND,, SysListView321, ahk_class Progman
if !hwWindow ; #D mode
ControlGet, hwWindow, HWND,, SysListView321, A
ControlGet, files, List, % ( selection ? "Selected":"") "Col1",,ahk_id %hwWindow%
base := SubStr(A_Desktop,0,1)=="" ? SubStr(A_Desktop,1,-1) : A_Desktop
Loop, Parse, files, `n, `r
{
path := base "\" A_LoopField
IfExist %path% ; ignore special icons like Computer (at least for now)
ret .= path "`n"
}
} else {
if selection
collection := window.document.SelectedItems
else
collection := window.document.Folder.Items
for item in collection
ret .= item.path "`n"
}
return Trim(ret,"`n")
}
;方法 Explorer_GetPath(hwnd="")--获取当前管理器路径
;参数hwnd
;示例path:=Explorer_GetPath()
Explorer_GetPath(hwnd="") {
if !(window := this.Explorer_GetWindow(hwnd))
return ErrorLevel := "ERROR"
if (window="desktop")
return A_Desktop
path := window.LocationURL
path := RegExReplace(path, "ftp://.*@","ftp://")
StringReplace, path, path, file:///
StringReplace, path, path, /,\, All
; thanks to polyethene、紧急制动(反馈)
Loop
If RegExMatch(path, "i)(?<=%)[\da-f]{1,2}", hex)
StringReplace, path, path,`%%hex%, % Chr("0x" . hex), All
Else Break
return path
}
;方法 Explorer_GetAll(hwnd="")--获取管理器目录
;参数hwnd
Explorer_GetAll(hwnd="") {
return this.Explorer_Get(hwnd)
}
;方法 Explorer_GetSelected(hwnd="")--获取管理器已选择文件目录
;参数hwnd
Explorer_GetSelected(hwnd="") {
return this.Explorer_Get(hwnd,true)
}
;方法 File_OpenAndSelect(sFullPath) --打开目录并选中文件
;参数sFullPath 类型字符串
;示例File_OpenAndSelect("E:\Documents\Desktop\ts1.ahk")
File_OpenAndSelect(sFullPath) {
SplitPath sFullPath, , sPath
FolderPidl := DllCall("shell32\ILCreateFromPath", "Str", sPath)
DllCall("shell32\SHParseDisplayName", "str", sFullPath, "Ptr", 0, "Ptr*", ItemPidl := 0, "Uint", 0, "Uint*", 0)
DllCall("shell32\SHOpenFolderAndSelectItems", "Ptr", FolderPidl, "UInt", 1, "Ptr*", ItemPidl, "Int", 0)
this.CoTaskMemFree(FolderPidl)
this.CoTaskMemFree(ItemPidl)
}
Files_OpenAndSelect(path,selfilearr) {
FolderPidl := DllCall("shell32\ILCreateFromPath", "Str", path)
VarSetCapacity(plist,selfilearr.Length() * A_PtrSize)
pidls := []
Loop % selfilearr.Length()
{
ItemPidl := DllCall("shell32\ILCreateFromPath", "Str", InStr(selfilearr[A_Index], ":") ? selfilearr[A_Index] : path "\" selfilearr[A_Index])
if (ItemPidl)
pidls.Push(ItemPidl), NumPut(ItemPidl, plist, (A_Index-1 ) * A_PtrSize,"ptr")
}
DllCall("shell32\SHOpenFolderAndSelectItems", "Ptr", FolderPidl, "UInt", pidls.Length(), "Ptr",&plist, "Int", 0)
this.CoTaskMemFree(FolderPidl)
Loop % pidls.Length()
{
this.CoTaskMemFree(pidls[A_Index])
}
}
CoTaskMemFree(pv) {
Return DllCall("ole32\CoTaskMemFree", "Ptr", pv)
}
}
;方法 Explorer_GetPath(hwnd="")--获取当前资源管理器路径
;方法 Explorer_GetAll(hwnd="")--获取资源管理器目录
;方法 Explorer_GetSelected(hwnd="")--获取资源管理器已选择文件目录
;方法 Explorer_GetWindow(hwnd="")获取资源管理器窗口名字
;方法 Explorer_Get(hwnd="",selection=false)获取资源管理器目录获取
;方法 File_OpenAndSelect(path, selfilearr) --打开资源管理器并选中文件
;方法 Files_OpenAndSelect(path, selfilearr) --打开资源管理器并选中一个或多个文件
;历史
;2021-01-21 修复夹带16进制的bug
;示例
myfile:=New fileplus
F1::
path := myfile.Explorer_GetPath() ; 获取当前资源管理器路径
all := myfile.Explorer_GetAll() ; 遍历当前资源管理器目录文件名
sel := myfile.Explorer_GetSelected() ; 获取资源管理器已选择文件目录名
; MsgBox % path
; MsgBox % all
MsgBox % sel
; myfile.File_OpenAndSelect("D:\Pstools\1111.png") ; 单文件选中写法
; myfile.Files_OpenAndSelect("D:\Pstools",["1111.png","Hash.exe"]) ; 多文件选中写法
Return
Class FilePlus {
;方法 Explorer_GetWindow(hwnd="")获取管理器窗口名字
;参数hwnd
Explorer_GetWindow(hwnd="") {
; thanks to jethrow for some pointers here
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process!="explorer.exe")
return
if (class ~= "(Cabinet|Explore)WClass") {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
return window
} else if (class ~= "Progman|WorkerW")
return "desktop" ; desktop found
}
;方法 Explorer_Get(hwnd="",selection=false)获取管理器目录获取
;参数hwnd
;参数selection 布尔类型
Explorer_Get(hwnd="",selection=false) {
if !(window := this.Explorer_GetWindow(hwnd))
return ErrorLevel := "ERROR"
if (window="desktop") {
ControlGet, hwWindow, HWND,, SysListView321, ahk_class Progman
if !hwWindow ; #D mode
ControlGet, hwWindow, HWND,, SysListView321, A
ControlGet, files, List, % ( selection ? "Selected":"") "Col1",,ahk_id %hwWindow%
base := SubStr(A_Desktop,0,1)=="" ? SubStr(A_Desktop,1,-1) : A_Desktop
Loop, Parse, files, `n, `r
{
path := base "\" A_LoopField
IfExist %path% ; ignore special icons like Computer (at least for now)
ret .= path "`n"
}
} else {
if selection
collection := window.document.SelectedItems
else
collection := window.document.Folder.Items
for item in collection
ret .= item.path "`n"
}
return Trim(ret,"`n")
}
;方法 Explorer_GetPath(hwnd="")--获取当前管理器路径
;参数hwnd
;示例path:=Explorer_GetPath()
Explorer_GetPath(hwnd="") {
if !(window := this.Explorer_GetWindow(hwnd))
return ErrorLevel := "ERROR"
if (window="desktop")
return A_Desktop
path := window.LocationURL
path := RegExReplace(path, "ftp://.*@","ftp://")
StringReplace, path, path, file:///
StringReplace, path, path, /,\, All
; thanks to polyethene、紧急制动(反馈)
Loop
If RegExMatch(path, "i)(?<=%)[\da-f]{1,2}", hex)
StringReplace, path, path,`%%hex%, % Chr("0x" . hex), All
Else Break
return path
}
;方法 Explorer_GetAll(hwnd="")--获取管理器目录
;参数hwnd
Explorer_GetAll(hwnd="") {
return this.Explorer_Get(hwnd)
}
;方法 Explorer_GetSelected(hwnd="")--获取管理器已选择文件目录
;参数hwnd
Explorer_GetSelected(hwnd="") {
return this.Explorer_Get(hwnd,true)
}
;方法 File_OpenAndSelect(sFullPath) --打开目录并选中文件
;参数sFullPath 类型字符串
;示例File_OpenAndSelect("E:\Documents\Desktop\ts1.ahk")
File_OpenAndSelect(sFullPath) {
SplitPath sFullPath, , sPath
FolderPidl := DllCall("shell32\ILCreateFromPath", "Str", sPath)
DllCall("shell32\SHParseDisplayName", "str", sFullPath, "Ptr", 0, "Ptr*", ItemPidl := 0, "Uint", 0, "Uint*", 0)
DllCall("shell32\SHOpenFolderAndSelectItems", "Ptr", FolderPidl, "UInt", 1, "Ptr*", ItemPidl, "Int", 0)
this.CoTaskMemFree(FolderPidl)
this.CoTaskMemFree(ItemPidl)
}
Files_OpenAndSelect(path,selfilearr) {
FolderPidl := DllCall("shell32\ILCreateFromPath", "Str", path)
VarSetCapacity(plist,selfilearr.Length() * A_PtrSize)
pidls := []
Loop % selfilearr.Length()
{
ItemPidl := DllCall("shell32\ILCreateFromPath", "Str", InStr(selfilearr[A_Index], ":") ? selfilearr[A_Index] : path "\" selfilearr[A_Index])
if (ItemPidl)
pidls.Push(ItemPidl), NumPut(ItemPidl, plist, (A_Index-1 ) * A_PtrSize,"ptr")
}
DllCall("shell32\SHOpenFolderAndSelectItems", "Ptr", FolderPidl, "UInt", pidls.Length(), "Ptr",&plist, "Int", 0)
this.CoTaskMemFree(FolderPidl)
Loop % pidls.Length()
{
this.CoTaskMemFree(pidls[A_Index])
}
}
CoTaskMemFree(pv) {
Return DllCall("ole32\CoTaskMemFree", "Ptr", pv)
}
}
;方法 Explorer_GetPath(hwnd="")--获取当前资源管理器路径
;方法 Explorer_GetAll(hwnd="")--获取资源管理器目录
;方法 Explorer_GetSelected(hwnd="")--获取资源管理器已选择文件目录
;方法 Explorer_GetWindow(hwnd="")获取资源管理器窗口名字
;方法 Explorer_Get(hwnd="",selection=false)获取资源管理器目录获取
;方法 File_OpenAndSelect(path, selfilearr) --打开资源管理器并选中文件
;方法 Files_OpenAndSelect(path, selfilearr) --打开资源管理器并选中一个或多个文件
;历史
;2021-01-21 修复夹带16进制的bug
;示例
myfile:=New fileplus
F1::
path := myfile.Explorer_GetPath() ; 获取当前资源管理器路径
all := myfile.Explorer_GetAll() ; 遍历当前资源管理器目录文件名
sel := myfile.Explorer_GetSelected() ; 获取资源管理器已选择文件目录名
; MsgBox % path
; MsgBox % all
MsgBox % sel
; myfile.File_OpenAndSelect("D:\Pstools\1111.png") ; 单文件选中写法
; myfile.Files_OpenAndSelect("D:\Pstools",["1111.png","Hash.exe"]) ; 多文件选中写法
Return
Class FilePlus {
;方法 Explorer_GetWindow(hwnd="")获取管理器窗口名字
;参数hwnd
Explorer_GetWindow(hwnd="") {
; thanks to jethrow for some pointers here
WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (process!="explorer.exe")
return
if (class ~= "(Cabinet|Explore)WClass") {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
return window
} else if (class ~= "Progman|WorkerW")
return "desktop" ; desktop found
}
;方法 Explorer_Get(hwnd="",selection=false)获取管理器目录获取
;参数hwnd
;参数selection 布尔类型
Explorer_Get(hwnd="",selection=false) {
if !(window := this.Explorer_GetWindow(hwnd))
return ErrorLevel := "ERROR"
if (window="desktop") {
ControlGet, hwWindow, HWND,, SysListView321, ahk_class Progman
if !hwWindow ; #D mode
ControlGet, hwWindow, HWND,, SysListView321, A
ControlGet, files, List, % ( selection ? "Selected":"") "Col1",,ahk_id %hwWindow%
base := SubStr(A_Desktop,0,1)=="" ? SubStr(A_Desktop,1,-1) : A_Desktop
Loop, Parse, files, `n, `r
{
path := base "\" A_LoopField
IfExist %path% ; ignore special icons like Computer (at least for now)
ret .= path "`n"
}
} else {
if selection
collection := window.document.SelectedItems
else
collection := window.document.Folder.Items
for item in collection
ret .= item.path "`n"
}
return Trim(ret,"`n")
}
;方法 Explorer_GetPath(hwnd="")--获取当前管理器路径
;参数hwnd
;示例path:=Explorer_GetPath()
Explorer_GetPath(hwnd="") {
if !(window := this.Explorer_GetWindow(hwnd))
return ErrorLevel := "ERROR"
if (window="desktop")
return A_Desktop
path := window.LocationURL
path := RegExReplace(path, "ftp://.*@","ftp://")
StringReplace, path, path, file:///
StringReplace, path, path, /,\, All
; thanks to polyethene、紧急制动(反馈)
Loop
If RegExMatch(path, "i)(?<=%)[\da-f]{1,2}", hex)
StringReplace, path, path,`%%hex%, % Chr("0x" . hex), All
Else Break
return path
}
;方法 Explorer_GetAll(hwnd="")--获取管理器目录
;参数hwnd
Explorer_GetAll(hwnd="") {
return this.Explorer_Get(hwnd)
}
;方法 Explorer_GetSelected(hwnd="")--获取管理器已选择文件目录
;参数hwnd
Explorer_GetSelected(hwnd="") {
return this.Explorer_Get(hwnd,true)
}
;方法 File_OpenAndSelect(sFullPath) --打开目录并选中文件
;参数sFullPath 类型字符串
;示例File_OpenAndSelect("E:\Documents\Desktop\ts1.ahk")
File_OpenAndSelect(sFullPath) {
SplitPath sFullPath, , sPath
FolderPidl := DllCall("shell32\ILCreateFromPath", "Str", sPath)
DllCall("shell32\SHParseDisplayName", "str", sFullPath, "Ptr", 0, "Ptr*", ItemPidl := 0, "Uint", 0, "Uint*", 0)
DllCall("shell32\SHOpenFolderAndSelectItems", "Ptr", FolderPidl, "UInt", 1, "Ptr*", ItemPidl, "Int", 0)
this.CoTaskMemFree(FolderPidl)
this.CoTaskMemFree(ItemPidl)
}
Files_OpenAndSelect(path,selfilearr) {
FolderPidl := DllCall("shell32\ILCreateFromPath", "Str", path)
VarSetCapacity(plist,selfilearr.Length() * A_PtrSize)
pidls := []
Loop % selfilearr.Length()
{
ItemPidl := DllCall("shell32\ILCreateFromPath", "Str", InStr(selfilearr[A_Index], ":") ? selfilearr[A_Index] : path "\" selfilearr[A_Index])
if (ItemPidl)
pidls.Push(ItemPidl), NumPut(ItemPidl, plist, (A_Index-1 ) * A_PtrSize,"ptr")
}
DllCall("shell32\SHOpenFolderAndSelectItems", "Ptr", FolderPidl, "UInt", pidls.Length(), "Ptr",&plist, "Int", 0)
this.CoTaskMemFree(FolderPidl)
Loop % pidls.Length()
{
this.CoTaskMemFree(pidls[A_Index])
}
}
CoTaskMemFree(pv) {
Return DllCall("ole32\CoTaskMemFree", "Ptr", pv)
}
}
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)