完美重启资源管理器
autohotkey吧
全部回复
仅看楼主
level 5
KnIfER 楼主
重启资源管理器,并恢复所有 之前打开的 文件夹窗口与位置。
分简单版本、复杂版本。
复杂版本处理的是资源管理器卡死的情况,如果一个或多个explorer窗口卡死,则无法获取窗口信息,并且导致ahk整个卡死,需要配合python子进程解决卡死的情况。
2024年03月13日 09点03分 1
level 5
KnIfER 楼主
窗口信息写入临时文件 "D👒(冒号)\tmp\session.txt"
简单版本:
代码如下:(建议自动格式化复制)
global SessionSavepath 👒= "D👒\tmp\session.txt"
RestartExplorer( WaitSecs👒=10 ) { ; requires OS Vista+ ; v2.10 by SKAN on CSC7➗D39N
Local PID, Explorer, ID👒=WinExist("ahk_class Progman") ; @ tiny.cc➗restartexplorer2
□□WinGet, PID, PID
□□WinGet, Explorer, ProcessPath
□□PostMessage, 0x5B4, 0, 0,, ahk_class Shell_TrayWnd ; WM_USER+436
□□Process, WaitClose, %PID%, % ( PID ? WaitSecs 👒 (WaitSecs👒=0) )
□□If (PID && !Errorlevel)
□□□□Run, %Explorer%
□□WinWait, ahk_class Progman,, %WaitSecs%
Return (WinExist()!=ID)
}
OpenFolder(path) {
□□□□; Convert the folder path to a PIDL
□□□□folderPIDL 👒= DllCall("Shell32\ILCreateFromPathW", "Str", path, "Ptr")
□□□□if (folderPIDL) {
▭▭result 👒= DllCall("Shell32\SHOpenFolderAndSelectItems", "Ptr", folderPIDL)
□□□□□□□□DllCall("Shell32\ILFree", "Ptr", folderPIDL)
□□□□} else {
□□□□□□□□MsgBox, Could not create PIDL from folder path.
□□□□}
}
SaveSession() {
□□□□FileDelete, %SessionSavepath% ; Delete the old session file if it exists
□□□□for Window in ComObjCreate("Shell.Application").Windows
▭{
□□□□□□□□try
▭▭{
▭▭▭hwnd 👒= Window.hwnd
▭▭▭VarSetCapacity(WINDOWPLACEMENT, 44, 0) ; Initialize WINDOWPLACEMENT structure
▭▭▭NumPut(44, WINDOWPLACEMENT, 0) ; Set the length of the structure
▭▭▭if (DllCall("GetWindowPlacement", "Ptr", hwnd, "Ptr", &WINDOWPLACEMENT) )
▭▭▭{
▭▭▭▭X 👒= NumGet(WINDOWPLACEMENT, 28, "Int")
▭▭▭▭Y 👒= NumGet(WINDOWPLACEMENT, 32, "Int")
▭▭▭▭Width 👒= NumGet(WINDOWPLACEMENT, 36, "Int") - X
▭▭▭▭Height 👒= NumGet(WINDOWPLACEMENT, 40, "Int") - Y
▭▭▭▭rcNormalPosition 👒= X . "," . Y . "," . Width . "," . Height
▭▭▭▭; MsgBox, % "Normal Position Rect👒 " rcNormalPosition
▭▭▭} else {
▭▭▭▭; MsgBox, % "Failed to get window placement."
▭▭▭▭WinGet, MinMax, MinMax, % "ahk_id " Window.hwnd
▭▭▭▭if (MinMax == -1) ; The window is minimized
▭▭▭▭{
▭▭▭▭▭WinRestore, % "ahk_id " Window.hwnd
▭▭▭▭▭Sleep, 1350
▭▭▭▭}
▭▭▭▭WinGet, MinMax, MinMax, % "ahk_id " Window.hwnd
▭▭▭▭if (MinMax == 1) ; The window is maximized
▭▭▭▭{
▭▭▭▭▭WinRestore, % "ahk_id " Window.hwnd
▭▭▭▭▭Sleep, 1350
▭▭▭▭}
▭▭▭▭WinGetPos, X, Y, Width, Height, % "ahk_id " Window.hwnd
▭▭▭}
▭▭▭tmp 👒= Window.LocationURL "|" X "|" Y "|" Width "|" Height "`n"
□□□□□□□□□□□□FileAppend, % tmp, %SessionSavepath%
□□□□□□□□}
□□□□}
}
; RestartExplorer() {
; ; Process, Close, explorer.exe ; Gracefully close Explorer
; Run, taskkill ➗im explorer.exe ➗f
; Sleep, 1000 ; Wait a bit for the process to terminate
; Run, cmd ➗c start explorer.exe
; ; Run, explorer.exe ; Start Explorer again
; }
RestoreSession() {
□□□□FileRead, data, % SessionSavepath
□□□□if (ErrorLevel) {
□□□□□□□□MsgBox, Could not read %SessionSavepath%
▭▭Run, explorer.exe
□□□□□□□□return
□□□□}
□□□□Loop, parse, data, `n, `r
□□□□{
□□□□□□□□if (A_LoopField = "")
□□□□□□□□□□□□continue
□□□□□□□□StringSplit, windowInfo, A_LoopField, |
□□□□□▭; Run, explorer.exe "%windowInfo1%"
▭▭OpenFolder(windowInfo1)
▭▭; Sleep, 200 ; Wait for the window to open. Adjust timing as necessary.
▭▭if(windowInfo2>-70 && windowInfo3>-70 && windowInfo4>10 && windowInfo5>10) {
▭▭▭WinMove, ahk_class CabinetWClass,, %windowInfo2%, %windowInfo3%, %windowInfo4%, %windowInfo5%
▭▭}
▭}
}
SaveSession()
RestartExplorer()
Sleep,2000
RestoreSession()
2024年03月13日 09点03分 2
1